以下查询非常慢:
select meta(d).id, timestamp_utc, d.headline, d.text from data_01 d where type="news" and topic="some_news" and timestamp_utc is not null order by timestamp_utc asc limit 1;
我创建了以下索引/索引:
CREATE INDEX `idx_timestamp_utc` ON `data_01`((-str_to_millis(`timestamp_utc`)))
CREATE INDEX `idx_timestamp_utc_some_news` ON `data_01`(`timestamp_utc`) WHERE ((`type` = "news") and (`topic` = "some_news"))
CREATE INDEX `idx_topic_and_timestamp_utc` ON `data_01`(`topic`,(-str_to_millis(`timestamp_utc`)))
CREATE INDEX `idx_type_news_topic_some_news_timestamp_utc` ON `data_01`((-`timestamp_utc`)) WHERE ((`type` = "news") and (`topic` = "some_news"))
我发现了类似的问题,但没有给出修复和建议。
感谢您回复解决此问题!
我正在运行Couchbase 4.5.1企业版(build-2844)
答案 0 :(得分:2)
在CB 4.5.0及更高版本中,请尝试以下索引之一并在下面修改查询。 该索引可用于任何主题 关于主题的查询谓词是相等更改顺序,包括主题不会更改结果,并且匹配查询顺序与索引键顺序相同,查询将避免排序。
CREATE INDEX `idx_some_news_timestamp_utc` ON `data_01`(`topic`,`timestamp_utc`) WHERE type = "news";
CREATE INDEX `idx_some_news_timestamp_utc_headline_text` ON `data_01`(`topic`,`timestamp_utc`,headline, text) WHERE type = "news";
SELECT meta(d).id, d.timestamp_utc, d.headline, d.text FROM data_01 d
WHERE type="news" AND topic="some_news" AND timestamp_utc IS NOT NULL
ORDER BY topic, timestamp_utc ASC LIMIT 1;