Question

i have a column family use counter as create table command below: (KEY i use bigin to filter when query ).

CREATE TABLE BannerCount (
KEY bigint PRIMARY KEY
) WITH
comment='' AND
comparator=text AND
read_repair_chance=0.100000 AND
gc_grace_seconds=864000 AND
default_validation=counter AND
min_compaction_threshold=4 AND
max_compaction_threshold=32 AND
replicate_on_write='true' AND
compaction_strategy_class='SizeTieredCompactionStrategy' AND
compression_parameters:sstable_compression='SnappyCompressor';

But when i insert data to this column family , and select using Where command to filter data results i retrived very strange :( like that:

use Query:

select count(1) From BannerCount where KEY > -1

count
-------
71

use Query:

select count(1) From BannerCount where KEY > 0;
count
-------
3

use Query:

select count(1) From BannerCount ;
count
-------
122

What happen with my query , who any tell me why i get that :( :(

Was it helpful?

Solution

To understand the reason for this, you should understand Cassandra's data model. You're probably using RandomPartitioner here, so each of these KEY values in your table are being hashed to token values, so they get stored in a distributed way around your ring.

So finding all rows whose key has a higher value than X isn't the sort of query Cassandra is optimized for. You should probably be keying your rows on some other value, and then using either wide rows for your bigint values (since columns are sorted) or put them in a second column, and create an index on it.

To explain in a little more detail why your results seem strange: CQL 2 implicitly turns "KEY >= X" into "token(KEY) >= token(X)", so that a querier can iterate through all the rows in a somewhat-efficient way. So really, you're finding all the rows whose hash is greater than the hash of X. See CASSANDRA-3771 for how that confusion is being resolved in CQL 3. That said, the proper fix for you is to structure your data according to the queries you expect to be running on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top