我正在通過learn.coucbase.com上提供的N1QL免費CB110課程。Couchbase DISTINCT非常慢
在課程的隨行工作簿下面的查詢需要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\";"
}
]
爲什麼查詢這麼慢?我如何加快查詢速度?