Couchbase 在执行参数化 N1QL 查询时不使用带过滤器的索引
Couchbase doesn't use index with filter when execute parametized N1QL query
我在我的 Couchbase 集群中创建了具有 where
条件的全局二级索引:
CREATE INDEX commentAuthorSecondaryIndex ON Bucket(author) WHERE (_class = "com.company.Comment") USING GSI;
然后我尝试通过 N1QL (REST API) 执行两个类似的查询:
curl --header "Content-Type:application/json" http://localhost:8093/query/service -d '
{
"$class" : "com.company.Comment",
"statement" : "select count(*) from Bucket WHERE _class = $class and author = $author",
"$author" : "author455",
"scan_consistency" : "statement_plus"
}'
和:
curl --header "Content-Type:application/json" http://localhost:8093/query/service -d '
{
"statement" : "select count(*) from Bucket WHERE _class = \"com.company.Comment\" and author = $author",
"$author" : "author455",
"scan_consistency" : "statement_plus"
}
第一个查询执行速度快 4 秒,而第二个只用了 20 毫秒。
当我使用 explain
关键字时,我意识到在第一个查询中 Couchbase 不使用索引。
我假设 Couchbase 首先创建查询执行计划,然后才使用参数评估查询。
因此,当创建查询计划时,Couchbase 不知道可以使用索引 commentAuthorSecondaryIndex
。
有什么办法可以解决这个问题吗?
两个选项 -- 从索引中删除过滤器,或者不将 _class 作为查询中的参数。参见 https://forums.couchbase.com/t/whats-the-best-approach-to-prevent-sql-injection-with-n1ql/11636/8
我在我的 Couchbase 集群中创建了具有 where
条件的全局二级索引:
CREATE INDEX commentAuthorSecondaryIndex ON Bucket(author) WHERE (_class = "com.company.Comment") USING GSI;
然后我尝试通过 N1QL (REST API) 执行两个类似的查询:
curl --header "Content-Type:application/json" http://localhost:8093/query/service -d '
{
"$class" : "com.company.Comment",
"statement" : "select count(*) from Bucket WHERE _class = $class and author = $author",
"$author" : "author455",
"scan_consistency" : "statement_plus"
}'
和:
curl --header "Content-Type:application/json" http://localhost:8093/query/service -d '
{
"statement" : "select count(*) from Bucket WHERE _class = \"com.company.Comment\" and author = $author",
"$author" : "author455",
"scan_consistency" : "statement_plus"
}
第一个查询执行速度快 4 秒,而第二个只用了 20 毫秒。
当我使用 explain
关键字时,我意识到在第一个查询中 Couchbase 不使用索引。
我假设 Couchbase 首先创建查询执行计划,然后才使用参数评估查询。
因此,当创建查询计划时,Couchbase 不知道可以使用索引 commentAuthorSecondaryIndex
。
有什么办法可以解决这个问题吗?
两个选项 -- 从索引中删除过滤器,或者不将 _class 作为查询中的参数。参见 https://forums.couchbase.com/t/whats-the-best-approach-to-prevent-sql-injection-with-n1ql/11636/8