【问题标题】:PouchDb find : why is my index not used?PouchDb 发现:为什么我的索引没有被使用?
【发布时间】:2016-07-29 23:30:30
【问题描述】:

我正在使用 PouchDb 和插件 PouchDb-find 在离子网络应用程序中查询我的本地数据库。在几乎每个用例中,我在创建索引时都会收到以下警告:

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

从插件文档的示例开始,我收到此警告,所以我想知道我做错了什么。

这是一个例子:

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));

为什么在使用$eq 时使用索引而不使用$ne

然后,我有以下查询的更复杂的情况(假设'a.deep.property' 始终存在并且是一个数组):

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

我已经使用字段'a.deep.property' 创建了一个索引。也不使用索引。我需要创建什么索引?

我也尝试手动创建索引并在前一种情况下使用query(),但这比使用 PouchDb-find 慢。

有什么想法吗?

【问题讨论】:

    标签: pouchdb


    【解决方案1】:

    $ne 目前不使用任何索引。您必须改用 $gt$lt 之类的东西。例如。而不是$ne: 0 你会做{$gt: 0, $lt: 0}

    【讨论】:

    • 感谢您的回答。使用{$gt: 0, $lt: 0} 非常聪明。我想我的第二个用例$not,一个深层属性和$size 也不使用任何索引,对吧?顺便说一句,我不知道哪些运营商正在使用或不使用索引,但我认为项目自述文件中可能会提到它以避免此类问题。等待您在此评论中回答我的问题以接受您的初步回答。
    猜你喜欢
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2013-02-22
    • 2020-05-26
    相关资源
    最近更新 更多