select with low-cardinality column with allow filtering抛出错误:受限列上的辅助索引不支持提供的运算符

时间:2016-08-30 08:31:06

标签: database indexing cassandra cql nosql

CREATE TABLE test (
    type text,
    scope text,
    name text,
    version text,
    alias text,
    deleted boolean,
    PRIMARY KEY ((type, scope, name), version)
) WITH read_repair_chance = 0.0
   AND dclocal_read_repair_chance = 0.1
   AND gc_grace_seconds = 864000
   AND bloom_filter_fp_chance = 0.01
   AND caching = { 'keys' : 'ALL', 'rows_per_partition' : 'NONE' }
   AND comment = ''
   AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'min_threshold' : 4, 'max_threshold' : 32 }
   AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.LZ4Compressor' }
   AND default_time_to_live = 0
   AND speculative_retry = '99.0PERCENTILE'
   AND min_index_interval = 128
   AND max_index_interval = 2048;
CREATE INDEX test_alias ON test (alias);
CREATE INDEX test_type_index ON test (type);

此选择不起作用:

select * 
from test 
where type = 'type' 
    and scope='scope' 
    and name='name'
    and deleted = false 
allow filtering;

并给我:

  

受限列上没有辅助索引支持提供的   运算符:com.datastax.driver.core.exceptions.InvalidQueryException:   受限列上没有辅助索引支持提供的   运算符。

此选择有效:

select * 
from test 
where type = 'type' 
    and scope='scope' 
    and deleted = false 
allow filtering;

此选择也有效:

select * 
from test 
where type = 'type' 
    and scope='scope' 
    and name='name' 
allow filtering;

此选择也有效:

select * 
from test 
where type = 'type' 
    and scope='scope' 
    and name='name'
    and version='version' 
allow filtering;

有什么想法吗?我不想在低基数列上创建索引,我不明白为什么在某些情况下查询有效(当我从主键过滤2个字段时,另外字段:已删除)

Cassandra版本:2.1.14

如果我理解正确,则不可能在复合分区键和另一个字段内的所有键一起使用查询条件。但我没有找到任何解释......

1 个答案:

答案 0 :(得分:2)

无论需要查询什么(WHERE子句的一部分),都需要将其作为主键的一部分或单独的索引编制索引。

对于复合键,需要包含键的最左侧部分才能搜索键的最右侧部分。

例如,如果复合键为(type, scope, name, deleted)并且想要在deleted上进行查询,则需要提供type, scope, name。要查询name需要提供至少type, scope等等。

deleted作为键的最后一部分的第二部分使其可以搜索,而不会产生额外的开销,因为删除的基数很低,所以会产生额外的索引效率(或不同)。

CREATE TABLE test (
     type text,
     scope text,
     name text,
     version text,
     alias text,
     deleted boolean,
     PRIMARY KEY (type, scope, name, deleted, version)
 );
select *  from test
  where type = 'type'
      and scope='scope'
      and name='name'
      and deleted = false;

select *  from test
  where type = 'type'
  and scope='scope'
  and name='name'
  and version='version'
  and deleted in (true,false);

请注意删除双and type='type',包含已删除的所有可能值,以便能够搜索版本并删除允许过滤(效果不佳)。

通常,制作适合您查询的架构。首先想一想'我将如何查询',以帮助您决定放置什么作为主键以及按什么顺序排列。忘记关系数据库语义,它们不适用。