0

同一个SQL,就是把字段的条件由IsDraft: 2变成IsDraft: 1,会变得特别慢。IsDraft: 2的查询结果有20万条。IsDraft: 2的查询结果是0条。

查询快的SQL,IsDraft: 2特别快
db.getCollection(gather).aggregate([
    {
        $match: {
            BatchID: 'ce1001b7-75b8-4476-9678-894fe4856796',
            SchoolID: '8a2d38bc-4aff-424f-9520-30c1cf8ec50c',
            GiveScore: { '$gte': 0, '$lte': 200 }
        }

    },
    {
        $match: { IsDraft: 2, IsStart: true }
    },
    {
        $count: "count"
    }
], {allowDiskUse: true}).toArray();

查询快的SQL,IsDraft: 1特别慢
db.getCollection(gather).aggregate([
    {
        $match: {
            BatchID: 'ce1001b7-75b8-4476-9678-894fe4856796',
            SchoolID: '8a2d38bc-4aff-424f-9520-30c1cf8ec50c',
            GiveScore: { '$gte': 0, '$lte': 200 }
        }

    },
    {
        $match: { IsDraft: 1, IsStart: true }
    },
    {
        $count: "count"
    }
], {allowDiskUse: true}).toArray();
更改状态以发布