可能有替代方案。
如果超过阈值,您可以创建一个在插入期间设置为 1 或 0 的“虚拟”列。然后,像这样创建一个复合列索引:
CREATE TABLE mdefparts (
part int,
name text,
val double,
time timeuuid,
exceeded boolean,
PRIMARY KEY ((part, name), exceeded, time)
);
这将像这样工作:
cqlsh:test> CREATE TABLE mdefparts (
... part int,
... name text,
... val double,
... time timeuuid,
... exceeded boolean,
... PRIMARY KEY ((part, name), exceeded, time)
... );
cqlsh:test>
cqlsh:test> insert into mdefparts (part, name, val, time, exceeded) values (0, 'y', 100, 37a5de5c-efb3-11e2-99d0-f23c91aec05e, true);
cqlsh:test> insert into mdefparts (part, name, val, time, exceeded) values (0, 'x', 100, 37a5de6c-efb3-11e2-99d0-f23c91aec05e, true);
cqlsh:test> insert into mdefparts (part, name, val, time, exceeded) values (0, 'y', 10, 37a5de7c-efb3-11e2-99d0-f23c91aec05e, false);
cqlsh:test> insert into mdefparts (part, name, val, time, exceeded) values (1, 'y', 1, 37a5de8c-efb3-11e2-99d0-f23c91aec05e, false);
cqlsh:test> select * from mdefparts;
part | name | exceeded | time | val
------+------+----------+--------------------------------------+-----
0 | y | False | 37a5de7c-efb3-11e2-99d0-f23c91aec05e | 10
0 | y | True | 37a5de5c-efb3-11e2-99d0-f23c91aec05e | 100
1 | y | False | 37a5de8c-efb3-11e2-99d0-f23c91aec05e | 1
0 | x | True | 37a5de6c-efb3-11e2-99d0-f23c91aec05e | 100
cqlsh:test> select * from mdefparts where name in ('x','y') and part = 0 and exceeded = true;
part | name | exceeded | time | val
------+------+----------+--------------------------------------+-----
0 | x | True | 37a5de6c-efb3-11e2-99d0-f23c91aec05e | 100
0 | y | True | 37a5de5c-efb3-11e2-99d0-f23c91aec05e | 100
这里的核心思想是将查询建模到列族中。我添加了一个 timeuuid 以允许随着时间的推移进行多次测量。