如何正确地使用弹簧索引的二级索引

时间:2019-01-06 20:14:36

标签: spring-boot spring-data couchbase n1ql spring-data-couchbase

我有一个名为order的沙发基础存储桶,其中包含将近2000个文档,我为此存储桶创建了辅助索引(idx_customer),以优化查询:

查询在沙发床查询监视器中执行时,会命中索引。

但我认为从Spring数据存储库播放时,它缺少索引。 主要是因为存储库中的排序和分页使我的查询翻译成其他缺少索引的查询。

CREATE INDEX idx_customer ON `order` (
buyer.contact.firstName , 
buyer.contact.lastName , 
ALL DISTINCT ARRAY aoc.`communicationValue` FOR aoc IN buyer.contact.communicationChannel
WHEN aoc.`communicationChannelCode`= "EMAIL" END)
WHERE _class = "com.lbk.entities.OrderEntity"

我使用带有此存储库的spring数据在spring boot应用程序内部查询订单:

import com.lbk.entities.OrderEntity;
import com.lbk.entities.OrderMetadataEntity;
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
import org.springframework.data.couchbase.core.query.N1qlSecondaryIndexed;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.core.query.ViewIndexed;
import org.springframework.data.couchbase.repository.CouchbasePagingAndSortingRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

@ViewIndexed(designDoc = "orderEntity")
@N1qlSecondaryIndexed(indexName = "idx_customer")
@N1qlPrimaryIndexed
public interface OrdersRepository extends CouchbasePagingAndSortingRepository<OrderEntity, String> {

    List<OrderMetadataEntity> findAllBy();

    Page<OrderMetadataEntity> findAllBy(Pageable page);

    @Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} "
                    + "AND orderTypeCode = $1 "
                    + "AND (orderCategory != $2 OR orderCategory is not valued ) "
                    + "AND buyer.contact.firstName is not null"
    )
    Page<OrderMetadataEntity> findOrders(String orderTypeCode, String excludedOrderCategory, Pageable page);

    @Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} "
                    + "AND creationDateTime >= STR_TO_MILLIS($1) AND creationDateTime <= STR_TO_MILLIS($2) "
                    + "AND orderTypeCode = $3 "
                    + "AND (orderCategory != $4 OR orderCategory is not valued )"

+“ AND Buyer.contact.firstName不为空”         )         页面findOrdersByCreationDateTimeBetween(                         字符串开始                         弦尾                         字符串orderTypeCode,                         字符串excludeOrderCategory,                         可分页         );

}

但是性能和日志记录表明,对于我而言,我没有使用索引 怎么了?以及如何从我的存储库中正确查询二级索引?

此分页的N1QL查询:

@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} "
                                + "AND orderTypeCode = $1 "
                                + "AND (orderCategory != $2 OR orderCategory is not valued ) "
                                + "AND buyer.contact.firstName is not null")
Page<OrderMetadataEntity> findOrders(String orderTypeCode, String excludedOrderCategory, Pageable page);

我在日志中有三个正在流动的查询:

  1. 查询计数文件。
  2. 查询以获取文档的第一页。
  3. 查询以获取子文档(SELECT ARRAY_LENGTH(orderLineItem))(我想知道为什么!)。

以下是弹簧数据日志:

Executing N1QL query: {\"args\":[\"%mohammed%\",\"%mohammed%\",null,\"220\",\"EXECLUDED_CATEGORY\"],\"statement\":\"SELECT COUNT(*) AS count FROM `order` WHERE `_class` = \\"com.lbk.entities.OrderEntity\\" AND ( LOWER(buyer.contact.firstName) LIKE $1 OR LOWER(buyer.contact.lastName) LIKE $2 OR ANY communicationChannel IN buyer.contact.communicationChannel SATISFIES ( communicationChannel.communicationChannelCode = 'EMAIL' AND communicationChannel.communicationValue  = $3 ) END )  AND orderTypeCode = $4 AND (orderCategory != $5 OR O2.orderCategory is not valued )AND creationDateTime in (select RAW max(O2.creationDateTime) from `order` O2 WHERE ( LOWER(O2.buyer.contact.firstName) LIKE $1 OR LOWER(O2.buyer.contact.lastName) LIKE $2 OR ANY communicationChannel IN O2.buyer.contact.communicationChannel SATISFIES ( communicationChannel.communicationChannelCode = 'EMAIL' AND communicationChannel.communicationValue  = $3 ) END )  AND ANY communicationChannel IN O2.buyer.contact.communicationChannel SATISFIES ( communicationChannel.communicationChannelCode = 'EMAIL' AND communicationChannel.communicationValue is not null ) END AND O2.orderTypeCode = $4 AND (O2.orderCategory != $5 OR O2.orderCategory is not valued) group by ( ARRAY item.communicationValue FOR item IN O2.buyer.contact.communicationChannel WHEN item.communicationChannelCode = 'EMAIL' END ) )AND buyer.contact.firstName IS NOT NULL\",\"scan_consistency\":\"statement_plus\"}
Executing N1QL query: {\"args\":[\"%mohammed%\",\"%mohammed%\",null,\"220\",\"EXECLUDED_CATEGORY\"],\"statement\":\"SELECT META(`order`).id AS _ID, META(`order`).cas AS _CAS, `order`.* FROM `order` WHERE `_class` = \\"com.lbk.entities.OrderEntity\\" AND ( LOWER(buyer.contact.firstName) LIKE $1 OR LOWER(buyer.contact.lastName) LIKE $2 OR ANY communicationChannel IN buyer.contact.communicationChannel SATISFIES ( communicationChannel.communicationChannelCode = 'EMAIL' AND communicationChannel.communicationValue  = $3 ) END )  AND orderTypeCode = $4 AND (orderCategory != $5 OR O2.orderCategory is not valued )AND creationDateTime in (select RAW max(O2.creationDateTime) from `order` O2 WHERE ( LOWER(O2.buyer.contact.firstName) LIKE $1 OR LOWER(O2.buyer.contact.lastName) LIKE $2 OR ANY communicationChannel IN O2.buyer.contact.communicationChannel SATISFIES ( communicationChannel.communicationChannelCode = 'EMAIL' AND communicationChannel.communicationValue  = $3 ) END )  AND ANY communicationChannel IN O2.buyer.contact.communicationChannel SATISFIES ( communicationChannel.communicationChannelCode = 'EMAIL' AND communicationChannel.communicationValue is not null ) END AND O2.orderTypeCode = $4 AND (O2.orderCategory != $5 OR O2.orderCategory is not valued) group by ( ARRAY item.communicationValue FOR item IN O2.buyer.contact.communicationChannel WHEN item.communicationChannelCode = 'EMAIL' END ) )AND buyer.contact.firstName IS NOT NULL ORDER BY `creationDateTime` DESC LIMIT 18 OFFSET 0\",\"scan_consistency\":\"statement_plus\"}
Executing N1QL query: {\"args\":[\"069cf983-8ed7-4b8f-845d-175593d4ca49\"],\"statement\":\"SELECT ARRAY_LENGTH(orderLineItem) FROM `order` WHERE `_class` = \\"com.lbk.entities.OrderEntity\\" AND META().id = $1\",\"scan_consistency\":\"statement_plus\"}

谢谢

1 个答案:

答案 0 :(得分:2)

查询将无法使用idx_customer索引,因为它不符合条件。请 在https://blog.couchbase.com/n1ql-practical-guide-second-edition

中签出“在Couchbase N1QL中设计查询查询索引”

索引是部分索引,并且仅包含具有_class =“ com.lbk.entities.OrderEntity”的文档的条目,但是您的查询都没有该谓词,因此它无法使用该索引。另外,查询谓词(谓词的每个OR部分)必须具有前导索引键作为查询谓词才能使用索引。日志中的查询不是这种情况。您可以根据需要尝试使用非部分索引。

CREATE INDEX idx_customer1 ON `order` (_class, 
buyer.contact.firstName , 
buyer.contact.lastName , 
ALL DISTINCT ARRAY aoc.`communicationValue` FOR aoc IN buyer.contact.communicationChannel
WHEN aoc.`communicationChannelCode`= "EMAIL" END);