$in / $or 查询可以使用索引吗?

Can $in / $or queries use indexes?

我正在 CouchDB 2.0 实例上使用 Mango 查询,通过奇妙的 pouchdb-find

有几次我收到可怕的 no matching index found, create an index to optimize query time 警告,即使我 使用索引字段。

刚才我在选择 "type": {"$in": ["a", "b"]} 或等效的 "$or": [{"type": "a"}, {"type": "b"}] 时得到了它,即使存在 type 上的索引。

谷歌搜索 (cloudant query docs, pouchdb-find docs, ) 没有帮助,在后者中@nlawson 说一些谓词 ($ne 在上述问题中,但也许我的 $in / $or同框?)"currently do not use any index".

谢谢!

回答我的问题:不,$in/or 查询不能使用索引。我在 this user@couchdb mailing list thread, where Garren Smith answered and pointed to Understanding Mango View-Based Indexes vs. Search-Based Indexes and A look under the covers of PouchDB-find 中问了这个问题。引用加伦的话,

The reason that adding "_id": {"$gt": 0} works is because pouchdb-find/mango fetches all the docs using the _all_docs index and then processes the $in operator in memory.

If you have a large database this will hurt. But you can use a better value than 0 to reduce the number of documents that need to be sorted in memory, which is a good thing.

所以,小心,"_id": {"$gt": 0}绝不是一种使用索引的方法(这是我在@markwatsonatx 建议时得到的印象),它只是一种抑制 pouchdb-find 的方法警告,告诉它 "I know this won't fit a map/reduce, I'll be running in-memory operations on allDocs, and I'm aware of the perf. consequences"。另外,

The warning is just to help anyone new to using Mango that what they are doing isn't the best way on a large database but will be fine on a small database. It's a fine way to experiment but once you start noticing performance issues, creating an index is the way forward.

我将补充我所做的一个小基准测试,比较从包含 [10000 个案例的数据库中获取 {10, 100, 1000, 10000} "cases"(在索引字段上使用选择器)的不同方法, 100000 噪音文件]

|number of cases fetched|10 |100 |1000 |10000 | |-----------------------|------|-------|--------|------| |$in |2452ms|2539ms |2474ms |5032ms| |$in + $gt |905ms |784ms |1014ms |3805ms| |$in + $gt + $lt |5ms |13ms |100ms |3854ms| |$or |2638ms|11763ms|101279ms|- |