mongoDB .toArray() 在某些聚合上速度较慢

2024-04-06

我有一个包含 n 个文档的集合。这些文件看起来像这样:

{
  "_id": "https://www.spacex.com/vehicles/dragon/",
  "meta": {
    "title": "SpaceX - Dragon",
    "description": "SpaceX designs, manufactures and launches advanced rockets and spacecraft. The company was founded in 2002 to revolutionize space technology, with the ultimate goal of enabling people to live on other planets.",
    "keywords": []
  },
  "rank": 1.073810573484715,
}

我搜索包含以下内容的文档:


main('apple');

async function main(searchTerm) {

    const agg = [{
        '$match': {
            '$text': {
                '$search': searchTerm
            }
        }
    }, {
        '$addFields': {
            'rank': {
                '$add': [{
                    '$meta': 'textScore'
                }, '$rank']
            }
        }
    }, {
        '$sort': {
            'rank': -1
        }
    }, {
        '$limit': 7
    }, {
        '$project': {
            '_id': 1,
            'meta': 1,
            'rank': 1
        }
    }];

    const client = await MongoClient.connect(
        'mongodb://localhost:8080', {
            useNewUrlParser: true,
            useUnifiedTopology: true
        }
    );

    const coll = client.db('coll').collection('docs');
    const cursor = coll.aggregate(agg);
    const result = await cursor.toArray();
    
    return result;

}

现在,这对于某些搜索非常有用,例如:spacex,所有这些都在不到一秒的时间内完成,实际聚合根本不需要时间。对于其他搜索,例如“苹果”,这个过程花费的时间太长,所以我最终放弃了。实际上是 .toArray() 部分占用了时间,其余部分完全按预期工作。为什么 .toArray() 在某些搜索上花费这么长时间,但不是全部搜索?

这是“mongodb”的解释显示:

{ stages: 
   [ { '$cursor': 
        { queryPlanner: 
           { plannerVersion: 1,
             namespace: 'geezeIt.docs',
             indexFilterSet: false,
             parsedQuery: 
              { '$text': 
                 { '$search': 'mongodb',
                   '$language': 'english',
                   '$caseSensitive': false,
                   '$diacriticSensitive': false } },
             queryHash: '3F7B44B0',
             planCacheKey: 'AC2F8FA3',
             winningPlan: 
              { stage: 'PROJECTION_SIMPLE',
                transformBy: { _id: 1, imgs: 1, meta: 1, rank: 1 },
                inputStage: 
                 { stage: 'TEXT',
                   indexPrefix: {},
                   indexName: 'geezeDex',
                   parsedTextQuery: 
                    { terms: [ 'mongodb' ],
                      negatedTerms: [],
                      phrases: [],
                      negatedPhrases: [] },
                   textIndexVersion: 3,
                   inputStage: 
                    { stage: 'TEXT_MATCH',
                      inputStage: 
                       { stage: 'TEXT_OR',
                         inputStage: 
                          { stage: 'IXSCAN',
                            keyPattern: { _fts: 'text', _ftsx: 1 },
                            indexName: 'geezeDex',
                            isMultiKey: true,
                            isUnique: false,
                            isSparse: false,
                            isPartial: false,
                            indexVersion: 2,
                            direction: 'backward',
                            indexBounds: {} } } } } },
             rejectedPlans: [] },
          executionStats: 
           { executionSuccess: true,
             nReturned: 253,
             executionTimeMillis: 11,
             totalKeysExamined: 253,
             totalDocsExamined: 253,
             executionStages: 
              { stage: 'PROJECTION_SIMPLE',
                nReturned: 253,
                executionTimeMillisEstimate: 9,
                works: 509,
                advanced: 253,
                needTime: 255,
                needYield: 0,
                saveState: 1,
                restoreState: 1,
                isEOF: 1,
                transformBy: { _id: 1, imgs: 1, meta: 1, rank: 1 },
                inputStage: 
                 { stage: 'TEXT',
                   nReturned: 253,
                   executionTimeMillisEstimate: 0,
                   works: 509,
                   advanced: 253,
                   needTime: 255,
                   needYield: 0,
                   saveState: 1,
                   restoreState: 1,
                   isEOF: 1,
                   indexPrefix: {},
                   indexName: 'geezeDex',
                   parsedTextQuery: 
                    { terms: [ 'mongodb' ],
                      negatedTerms: [],
                      phrases: [],
                      negatedPhrases: [] },
                   textIndexVersion: 3,
                   inputStage: 
                    { stage: 'TEXT_MATCH',
                      nReturned: 253,
                      executionTimeMillisEstimate: 0,
                      works: 509,
                      advanced: 253,
                      needTime: 255,
                      needYield: 0,
                      saveState: 1,
                      restoreState: 1,
                      isEOF: 1,
                      docsRejected: 0,
                      inputStage: 
                       { stage: 'TEXT_OR',
                         nReturned: 253,
                         executionTimeMillisEstimate: 0,
                         works: 509,
                         advanced: 253,
                         needTime: 255,
                         needYield: 0,
                         saveState: 1,
                         restoreState: 1,
                         isEOF: 1,
                         docsExamined: 253,
                         inputStage: 
                          { stage: 'IXSCAN',
                            nReturned: 253,
                            executionTimeMillisEstimate: 0,
                            works: 254,
                            advanced: 253,
                            needTime: 0,
                            needYield: 0,
                            saveState: 1,
                            restoreState: 1,
                            isEOF: 1,
                            keyPattern: { _fts: 'text', _ftsx: 1 },
                            indexName: 'geezeDex',
                            isMultiKey: true,
                            isUnique: false,
                            isSparse: false,
                            isPartial: false,
                            indexVersion: 2,
                            direction: 'backward',
                            indexBounds: {},
                            keysExamined: 253,
                            seeks: 1,
                            dupsTested: 253,
                            dupsDropped: 0 } } } } },
             allPlansExecution: [] } },
       nReturned: 253,
       executionTimeMillisEstimate: 9 },
     { '$addFields': { rank: { '$add': [ { '$meta': 'textScore' }, '$rank' ] } },
       nReturned: 253,
       executionTimeMillisEstimate: 9 },
     { '$sort': { sortKey: { rank: -1 }, limit: 7 },
       nReturned: 7,
       executionTimeMillisEstimate: 9 },
     { '$project': { _id: true, imgs: true, meta: true, rank: true },
       nReturned: 7,
       executionTimeMillisEstimate: 9 } ],
  serverInfo: 
   { host: 'ubuntu',
     port: 27017,
     version: '4.4.15',
     gitVersion: 'bc17cf2c788c5dda2801a090ea79da5ff7d5fac9' },
  ok: 1 }

对于“苹果”等其他术语,我无法获得解释计划,因为它需要太长时间......


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mongoDB .toArray() 在某些聚合上速度较慢 的相关文章

随机推荐