PouchDb find : why is my index not used?

醉酒当歌 提交于 2019-12-11 07:29:27

问题


I'm using PouchDb and the plugin PouchDb-find to query my local DB in an ionic web app. In almost every use case, I get the following warning when querying whereas I've created an index:

{
    "docs": {
        ...
    },
    "warning": "no matching index found, create an index to optimize query time"
}

Starting from an example of the plugin documentation, I get this warning so I am wondering what I am doing wrong.

Here is an example:

var db = new PouchDB('test');

var docs = [];
for (var i = 0; i < 10; i++) {
  docs.push({title: 'Lorem ipsum ' + i, _id: 'doc' + (i + 1)});
}

db.bulkDocs(docs)
  .then(
    function () {
      return db.createIndex({index: {fields: ['title']}});
    })
  .then(
    function () {
      // return db.find({selector: {title: {$eq: 0}}}); // No warning
      return db.find({selector: {title: {$ne: 0}}}); // Warning
    })
  .then(
    function (res) {
      console.log(res);
    })
  .catch(console.error.bind(console));

Why the index is used when using $eq and is not with $ne ?

Then, I have a more complex case with the following query (assuming 'a.deep.property' always exists and is an Array) :

db.find(
    {
        selector: {
            $not:{
                "a.deep.property": {$size:0}
            }
        }
    }
);

I've created an index with the field 'a.deep.property'. The index is not used either. What index do I need to create ?

I also tried to create an index manually and to use query() in the previous case but this was slower than using PouchDb-find.

Any idea ?


回答1:


$ne currently does not use any index. You would have to use something like $gt or $lt instead. E.g. instead of $ne: 0 you would do {$gt: 0, $lt: 0}.



来源:https://stackoverflow.com/questions/38497985/pouchdb-find-why-is-my-index-not-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!