Cassandra数据库无法同时使用IN和order by
当使用 IN 和 ORDER BY 时,Cassandra 不支持分页。datastax 文档 -使用 IN 关键字进行检索中也说明了这一点。
一、问题场景:
报错如下:
Cannot page queries with both ORDER BY and a IN restriction on the partition key; you must either remove the ORDER BY or the IN and sort client side, or disable paging for this query
具体SQL如下:
select * from AXWS where v IN ('LX2H, 'L5') and ct>=17375 order by ct desc
二、解决方法
①取消分页限制(代码层面):
import com.datastax.driver.core.Statement;
ResultSet rows = null;
Statement statement = new SimpleStatement(cql);
statement.setFetchSize(Integer.MAX_VALUE);//设置最大值
rows = this.session.execute(statement);
②取消分页限制(命令):
- 关闭分页:
cqlsh> PAGING OFF
③取消IN或者order by,使用批量查询,归到Java中进行排序
ResultSet rows1 = this.session.execute(sql1);
ResultSet rows2 = this.session.execute(sql2);
ResultSet rows3 = this.session.execute(sql3);
...合并处理时间排序操作