【问题标题】:spring-data-cassandra: InvalidQueryException: Cannot execute this query ... use ALLOW FILTERINGspring-data-cassandra:InvalidQueryException:无法执行此查询...使用 ALLOW FILTERING
【发布时间】:2018-03-16 01:32:06
【问题描述】:

我有以下代码

  @Indexed
  @PrimaryKeyColumn(name = "x", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
  @Column(value="x")
  private String x;

  @Indexed
  @PrimaryKeyColumn(name = "code", ordinal = 2, type = PrimaryKeyType.PARTITIONED)
  @Column(value="code")
  private String code;

@Query(value = "select * from customers where code = ?0")
Optional<Customer> findByCode(String code);

执行此操作时,我得到Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING

有没有办法从spring-data-cassandra 中避免这种情况?我不想在我的查询中添加ALLOW FILTERING。我尝试在 code 列上创建单独的索引,但这并没有解决问题。我认为它在 spring 数据配置中停止。如果我在cqlsh 中执行相同的查询,它会起作用。

【问题讨论】:

    标签: spring cassandra spring-data spring-data-jpa spring-data-cassandra


    【解决方案1】:

    使用 no-sql 数据库时,您需要正确设计数据以避免过滤。您可以添加二级索引来优化特定字段的检索。更多细节在这里:https://docs.datastax.com/en/archived/cql/3.3/cql/cql_using/useSecondaryIndex.html

    如果您确定查询是您所需要的,您可以使用@Query 注释上的allowFiltering 参数来明确指示使用ALLOW FILTERING

    @Query(value = "select * from customers where code = ?0", allowFiltering = true)
    Optional<Customer> findOneByCode(String code);
    

    【讨论】:

      【解决方案2】:

      您必须在查询中指定分区键,除非您创建索引或使用 ALLOW FILTERING

      使用允许过滤执行查询可能不是一个好主意,因为它会占用大量计算资源,并且可能由于超时而不会返回任何结果。不要在生产中使用允许过滤阅读有关使用 ALLOW FILTERING 的 datastax 文档

      https://docs.datastax.com/en/cql/3.3/cql/cql_reference/select_r.html?hl=allow,filter

      【讨论】:

      • 在实践中“在查询中指定分区键”是什么意思?如何在代码中做到这一点?
      • 对于简单的主键,第一个字段是分区键。更多关于这里stackoverflow.com/a/24953331/2320144
      猜你喜欢
      • 2016-06-29
      • 2013-10-24
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2018-09-24
      • 1970-01-01
      相关资源
      最近更新 更多