问题
I have a Cassandra table,
CREATE TABLE read_locks (
parent_path text,
filename text,
instance text,
PRIMARY KEY ((parent_path, filename), instance)
);
Logically I want to check the existence of any locks on a file by the following statement:
select count(*)>0 as result from read_locks where parent_path='...' and filename='...';
Of course, I have at least 2 implementations.
select count(*) as result from read_locks where parent_path='...' and filename='...';
and then to use other code, i.e. C++, to check the value of result.
Or
select * from read_locks where parent_path='...' and filename='...';
and then to use other code, i.e. C++, to check the bool value of the following statement:
cass_iterator_next(rows)
I am not sure which is better.
And I guess there is a user-defined aggregate function to do so, but I couldn't figure out.
Please share your comments.
Thank you in advance, Ying
回答1:
If you only care if there are any locks, and now how many locks there are, then it's probably more efficient to add a limit clause like this:
SELECT * FROM read_locks WHERE parent_path='...' and filename='...' LIMIT 1;
If that returns a row, then you know there is at least one lock on the file, and if it returns nothing, then there are no locks on the file.
来源:https://stackoverflow.com/questions/39140089/the-best-way-to-check-existence-of-filtered-rows-in-cassandra-by-user-defined-a