我正在学习在learn.coucbase.com上提供的关于N1QL的免费CB110课程。
在课程中查询附带的练习册需要1分钟:
SELECT DISTINCT address.countryCode
FROM couchmusic2
WHERE email LIKE "%hotmail.com";
我在电子邮件上有一个gsi。
以下查询需要几毫秒:
SELECT COUNT(*)
FROM couchmusic2
WHERE email LIKE "%hotmail.com";
让我相信DISTINCT就是问题所在。
EXPLAIN显示了这一点:
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "idx_email",
"index_id": "c2e612a0d697d8b6",
"keyspace": "couchmusic2",
"namespace": "default",
"spans": [
{
"Range": {
"High": [
"[]"
],
"Inclusion": 1,
"Low": [
"\"\""
]
}
}
],
"using": "gsi"
},
{
"#operator": "Fetch",
"keyspace": "couchmusic2",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "((`couchmusic2`.`email`) like \"%hotmail.com\")"
},
{
"#operator": "InitialProject",
"distinct": true,
"result_terms": [
{
"expr": "((`couchmusic2`.`address`).`countryCode`)"
}
]
},
{
"#operator": "Distinct"
},
{
"#operator": "FinalProject"
}
]
}
},
{
"#operator": "Distinct"
}
]
},
"text": "\nSELECT DISTINCT address.countryCode \nFROM couchmusic2 \nWHERE email LIKE \"%hotmail.com\";"
}
]
为什么查询这么慢?如何加快查询速度?
答案 0 :(得分:0)
计数查询使用覆盖索引。 请为DISTINCT Query尝试以下索引。
CREATE INDEX ix1 ON couchmusic2(email,address.countryCode);
LIKE与领先的%需要完成indexScan。看看https://dzone.com/articles/a-couchbase-index-technique-for-like-predicates-wi
答案 1 :(得分:0)
对于所有字符串ENDING with hotmail.com的模式匹配,请执行以下操作:
CREATE INDEX ix ON couchmusic2(SUBSTR(email,-11,11),address.countryCode);
将LIKE谓词修改为:WHERE SUBSTR(email,-11,11)=" hotmail.com&#34 ;; 显然,这仅适用于hotmail.com,您需要另一个索引。
Checkout TOKENS()函数可以更灵活地对此进行索引。
要获得不同的值(当您有非常多的项目与不同值的数量相比时),请尝试MIN()优化。 https://dzone.com/articles/count-amp-group-faster-using-n1ql