Querying data from collection that's contains array in document taking to much time to display records

We have around 150M document in collection.
The document structure is like,

{
“Client”:“XYZ”,
“ClientId”:‘12345’,
“keyword”: [
{
“keyword”: “Keyword1”,
“keyid”: “34”,
“keytype”: “Industry1”
},
{
“keyword”: “Keyword2”,
“keyid”: “35”,
“keytpe”: “Industry2”
}
],



}

When I putt the condition like keyword :{$elementMatch :{keytype}} its takes to much time to fetch documents.

Please help?

The first thing to do when queries do not perform is to look at the explain plan so see if an index is used and how selective is the index.

1 Like

Perform all things but unable to do so.

Hello @Shahnawaz_Haider ,

There could be several reason to queries responding slow, most common reasons are resource crunch and in-efficient indexing. Please take a look at Best Practices for Query Performance to make sure you are following the best practices for faster query processing. To learn more about your use case, can you please share below details?

  • As @steevej suggested, please run your query with explain in executionStats mode (e.g. `db.collection.explain(‘executionStats’).aggregate(…) or db.collection.explain(‘executionStats’).find(…)) just in case you are using find and not aggregate) and share the output.
  • Also share db.collection.stats() output.
  • MongoDB Version
  • Deployment Topology and the hardware configuration

Can you please provide more details on what have you tried till now and what is not working?

Regards,
Tarun

Dear Tarun,

Thanks for your reply, I am sharing the details:

  1. Execution Stats:
    {
    “queryPlanner” : {
    “plannerVersion” : 1,
    “namespace” : “impact.article_beta”,
    “indexFilterSet” : false,
    “parsedQuery” : {
    “$and” : [
    {
    “keyword” : {
    “$elemMatch” : {
    “$and” : [
    {
    “keytpe” : {
    “$eq” : “My Competitor Keyword”
    }
    },
    {
    “companys” : {
    “$in” : [
    “CapGemini”,
    “Cognizant”,
    “Deloitte”,
    “Infosys”
    ]
    }
    }
    ]
    }
    }
    },
    {
    “clientid” : {
    “$eq” : “A0003”
    }
    },
    {
    “rejected” : {
    “$eq” : 0
    }
    },
    {
    “pubdate” : {
    “$lte” : “2022-12-31”
    }
    },
    {
    “pubdate” : {
    “$gte” : “2022-01-01”
    }
    },
    {
    “type” : {
    “$in” : [
    “PRINT”,
    “WEB”
    ]
    }
    }
    ]
    },
    “winningPlan” : {
    “stage” : “FETCH”,
    “filter” : {
    “keyword” : {
    “$elemMatch” : {
    “$and” : [
    {
    “keytpe” : {
    “$eq” : “My Competitor Keyword”
    }
    },
    {
    “companys” : {
    “$in” : [
    “CapGemini”,
    “Cognizant”,
    “Deloitte”,
    “Infosys”
    ]
    }
    }
    ]
    }
    }
    },
    “inputStage” : {
    “stage” : “IXSCAN”,
    “keyPattern” : {
    “rejected” : 1,
    “clientid” : 1,
    “pubdate” : -1,
    “type” : 1,
    “publication” : 1,
    “city” : 1,
    “language” : 1,
    “keyword.keytpe” : 1,
    “keyword.keyword” : 1,
    “keyword.companys” : 1,
    “keyword.companyissue” : 1
    },
    “indexName” : “SearchI4All”,
    “isMultiKey” : true,
    “multiKeyPaths” : {
    “rejected” : ,
    “clientid” : ,
    “pubdate” : ,
    “type” : ,
    “publication” : ,
    “city” : ,
    “language” : ,
    “keyword.keytpe” : [
    “keyword”
    ],
    “keyword.keyword” : [
    “keyword”
    ],
    “keyword.companys” : [
    “keyword”
    ],
    “keyword.companyissue” : [
    “keyword”
    ]
    },
    “isUnique” : false,
    “isSparse” : false,
    “isPartial” : false,
    “indexVersion” : 2,
    “direction” : “forward”,
    “indexBounds” : {
    “rejected” : [
    “[0.0, 0.0]”
    ],
    “clientid” : [
    “["A0003", "A0003"]”
    ],
    “pubdate” : [
    “["2022-12-31", "2022-01-01"]”
    ],
    “type” : [
    “["PRINT", "PRINT"]”,
    “["WEB", "WEB"]”
    ],
    “publication” : [
    “[MinKey, MaxKey]”
    ],
    “city” : [
    “[MinKey, MaxKey]”
    ],
    “language” : [
    “[MinKey, MaxKey]”
    ],
    “keyword.keytpe” : [
    “["My Competitor Keyword", "My Competitor Keyword"]”
    ],
    “keyword.keyword” : [
    “[MinKey, MaxKey]”
    ],
    “keyword.companys” : [
    “["CapGemini", "CapGemini"]”,
    “["Cognizant", "Cognizant"]”,
    “["Deloitte", "Deloitte"]”,
    “["Infosys", "Infosys"]”
    ],
    “keyword.companyissue” : [
    “[MinKey, MaxKey]”
    ]
    }
    }
    },
    “rejectedPlans” : [
    {
    “stage” : “FETCH”,
    “filter” : {
    “$and” : [
    {
    “keyword” : {
    “$elemMatch” : {
    “$and” : [
    {
    “keytpe” : {
    “$eq” : “My Competitor Keyword”
    }
    },
    {
    “companys” : {
    “$in” : [
    “CapGemini”,
    “Cognizant”,
    “Deloitte”,
    “Infosys”
    ]
    }
    }
    ]
    }
    }
    },
    {
    “clientid” : {
    “$eq” : “A0003”
    }
    },
    {
    “rejected” : {
    “$eq” : 0
    }
    },
    {
    “pubdate” : {
    “$lte” : “2022-12-31”
    }
    },
    {
    “pubdate” : {
    “$gte” : “2022-01-01”
    }
    },
    {
    “type” : {
    “$in” : [
    “PRINT”,
    “WEB”
    ]
    }
    }
    ]
    },
    “inputStage” : {
    “stage” : “IXSCAN”,
    “keyPattern” : {
    “keyword.keytpe” : 1
    },
    “indexName” : “KeyType”,
    “isMultiKey” : true,
    “multiKeyPaths” : {
    “keyword.keytpe” : [
    “keyword”
    ]
    },
    “isUnique” : false,
    “isSparse” : false,
    “isPartial” : false,
    “indexVersion” : 2,
    “direction” : “forward”,
    “indexBounds” : {
    “keyword.keytpe” : [
    “["My Competitor Keyword", "My Competitor Keyword"]”
    ]
    }
    }
    }
    ]
    },
    “executionStats” : {
    “executionSuccess” : true,
    “nReturned” : 118422,
    “executionTimeMillis” : 10319,
    “totalKeysExamined” : 295933,
    “totalDocsExamined” : 118422,
    “executionStages” : {
    “stage” : “FETCH”,
    “filter” : {
    “keyword” : {
    “$elemMatch” : {
    “$and” : [
    {
    “keytpe” : {
    “$eq” : “My Competitor Keyword”
    }
    },
    {
    “companys” : {
    “$in” : [
    “CapGemini”,
    “Cognizant”,
    “Deloitte”,
    “Infosys”
    ]
    }
    }
    ]
    }
    }
    },
    “nReturned” : 118422,
    “executionTimeMillisEstimate” : 5288,
    “works” : 295933,
    “advanced” : 118422,
    “needTime” : 177510,
    “needYield” : 0,
    “saveState” : 2388,
    “restoreState” : 2388,
    “isEOF” : 1,
    “docsExamined” : 118422,
    “alreadyHasObj” : 0,
    “inputStage” : {
    “stage” : “IXSCAN”,
    “nReturned” : 118422,
    “executionTimeMillisEstimate” : 332,
    “works” : 295933,
    “advanced” : 118422,
    “needTime” : 177510,
    “needYield” : 0,
    “saveState” : 2388,
    “restoreState” : 2388,
    “isEOF” : 1,
    “keyPattern” : {
    “rejected” : 1,
    “clientid” : 1,
    “pubdate” : -1,
    “type” : 1,
    “publication” : 1,
    “city” : 1,
    “language” : 1,
    “keyword.keytpe” : 1,
    “keyword.keyword” : 1,
    “keyword.companys” : 1,
    “keyword.companyissue” : 1
    },
    “indexName” : “SearchI4All”,
    “isMultiKey” : true,
    “multiKeyPaths” : {
    “rejected” : ,
    “clientid” : ,
    “pubdate” : ,
    “type” : ,
    “publication” : ,
    “city” : ,
    “language” : ,
    “keyword.keytpe” : [
    “keyword”
    ],
    “keyword.keyword” : [
    “keyword”
    ],
    “keyword.companys” : [
    “keyword”
    ],
    “keyword.companyissue” : [
    “keyword”
    ]
    },
    “isUnique” : false,
    “isSparse” : false,
    “isPartial” : false,
    “indexVersion” : 2,
    “direction” : “forward”,
    “indexBounds” : {
    “rejected” : [
    “[0.0, 0.0]”
    ],
    “clientid” : [
    “["A0003", "A0003"]”
    ],
    “pubdate” : [
    “["2022-12-31", "2022-01-01"]”
    ],
    “type” : [
    “["PRINT", "PRINT"]”,
    “["WEB", "WEB"]”
    ],
    “publication” : [
    “[MinKey, MaxKey]”
    ],
    “city” : [
    “[MinKey, MaxKey]”
    ],
    “language” : [
    “[MinKey, MaxKey]”
    ],
    “keyword.keytpe” : [
    “["My Competitor Keyword", "My Competitor Keyword"]”
    ],
    “keyword.keyword” : [
    “[MinKey, MaxKey]”
    ],
    “keyword.companys” : [
    “["CapGemini", "CapGemini"]”,
    “["Cognizant", "Cognizant"]”,
    “["Deloitte", "Deloitte"]”,
    “["Infosys", "Infosys"]”
    ],
    “keyword.companyissue” : [
    “[MinKey, MaxKey]”
    ]
    },
    “keysExamined” : 295933,
    “seeks” : 170182,
    “dupsTested” : 125751,
    “dupsDropped” : 7329
    }
    }
    },
    “serverInfo” : {
    “host” : “irmpl-shard-00-01-zame7.mongodb.net”,
    “port” : 27017,
    “version” : “4.2.23”,
    “gitVersion” : “f4e6602d3a4c5b22e9d8bcf0722d0afd0ec01ea2”
    },
    “ok” : 1,
    “$clusterTime” : {
    “clusterTime” : Timestamp(1675844580, 1),
    “signature” : {
    “hash” : BinData(0,“iyo2aXSJddeSlwAseC2bSt22Ky8=”),
    “keyId” : NumberLong(“7155578345936125954”)
    }
    },
    “operationTime” : Timestamp(1675844580, 1)
    }

continue…

  1. db.collection.stats():

{
“ns” : “impact.article_beta”,
“size” : 166806089712,
“count” : 125215566,
“avgObjSize” : 1332,
“storageSize” : 46065573888,
“capped” : false,
“wiredTiger” : {
“metadata” : {
“formatVersion” : 1
},
“creationString” : “access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=false),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_custom=(prefix=,start_generation=0,suffix=),merge_max=15,merge_min=0),memory_page_image_max=0,memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u”,
“type” : “file”,
“uri” : “statistics:table:collection-30-3242645060345021432”,
“LSM” : {
“bloom filter false positives” : 0,
“bloom filter hits” : 0,
“bloom filter misses” : 0,
“bloom filter pages evicted from cache” : 0,
“bloom filter pages read into cache” : 0,
“bloom filters in the LSM tree” : 0,
“chunks in the LSM tree” : 0,
“highest merge generation in the LSM tree” : 0,
“queries that could have benefited from a Bloom filter that did not exist” : 0,
“sleep for LSM checkpoint throttle” : 0,
“sleep for LSM merge throttle” : 0,
“total size of bloom filters” : 0
},
“block-manager” : {
“allocations requiring file extension” : 2,
“blocks allocated” : 12,
“blocks freed” : 6,
“checkpoint size” : 45762641920,
“file allocation unit size” : 4096,
“file bytes available for reuse” : 302383104,
“file magic number” : 120897,
“file major version number” : 1,
“file size in bytes” : 46065573888,
“minor version number” : 0
},
“btree” : {
“btree checkpoint generation” : 17822,
“btree clean tree checkpoint expiration time” : NumberLong(“9223372036854775807”),
“column-store fixed-size leaf pages” : 0,
“column-store internal pages” : 0,
“column-store variable-size RLE encoded values” : 0,
“column-store variable-size deleted values” : 0,
“column-store variable-size leaf pages” : 0,
“fixed-record size” : 0,
“maximum internal page key size” : 368,
“maximum internal page size” : 4096,
“maximum leaf page key size” : 2867,
“maximum leaf page size” : 32768,
“maximum leaf page value size” : 67108864,
“maximum tree depth” : 5,
“number of key/value pairs” : 0,
“overflow pages” : 0,
“pages rewritten by compaction” : 0,
“row-store empty values” : 0,
“row-store internal pages” : 0,
“row-store leaf pages” : 0
},
“cache” : {
“bytes currently in the cache” : 2935955061,
“bytes dirty in the cache cumulative” : 319634,
“bytes read into cache” : NumberLong(“7904056170608”),
“bytes written from cache” : 248169,
“checkpoint blocked page eviction” : 0,
“data source pages selected for eviction unable to be evicted” : 50839,
“eviction walk passes of a file” : 980797,
“eviction walk target pages histogram - 0-9” : 129141,
“eviction walk target pages histogram - 10-31” : 103840,
“eviction walk target pages histogram - 128 and higher” : 0,
“eviction walk target pages histogram - 32-63” : 124451,
“eviction walk target pages histogram - 64-128” : 623365,
“eviction walks abandoned” : 65842,
“eviction walks gave up because they restarted their walk twice” : 1806,
“eviction walks gave up because they saw too many pages and found no candidates” : 106482,
“eviction walks gave up because they saw too many pages and found too few candidates” : 9699,
“eviction walks reached end of tree” : 158406,
“eviction walks started from root of tree” : 183830,
“eviction walks started from saved location in tree” : 796967,
“hazard pointer blocked page eviction” : 9959,
“in-memory page passed criteria to be split” : 0,
“in-memory page splits” : 0,
“internal pages evicted” : 877222,
“internal pages split during eviction” : 0,
“leaf pages split during eviction” : 0,
“modified pages evicted” : 3,
“overflow pages read into cache” : 0,
“page split during eviction deepened the tree” : 0,
“page written requiring cache overflow records” : 0,
“pages read into cache” : 71737010,
“pages read into cache after truncate” : 0,
“pages read into cache after truncate in prepare state” : 0,
“pages read into cache requiring cache overflow entries” : 0,
“pages requested from the cache” : 2893810983,
“pages seen by eviction walk” : 112950453,
“pages written from cache” : 8,
“pages written requiring in-memory restoration” : 0,
“tracked dirty bytes in the cache” : 0,
“unmodified pages evicted” : 71709813
},
“cache_walk” : {
“Average difference between current eviction generation when the page was last considered” : 0,
“Average on-disk page image size seen” : 0,
“Average time in cache for pages that have been visited by the eviction server” : 0,
“Average time in cache for pages that have not been visited by the eviction server” : 0,
“Clean pages currently in cache” : 0,
“Current eviction generation” : 0,
“Dirty pages currently in cache” : 0,
“Entries in the root page” : 0,
“Internal pages currently in cache” : 0,
“Leaf pages currently in cache” : 0,
“Maximum difference between current eviction generation when the page was last considered” : 0,
“Maximum page size seen” : 0,
“Minimum on-disk page image size seen” : 0,
“Number of pages never visited by eviction server” : 0,
“On-disk page image sizes smaller than a single allocation unit” : 0,
“Pages created in memory and never written” : 0,
“Pages currently queued for eviction” : 0,
“Pages that could not be queued for eviction” : 0,
“Refs skipped during cache traversal” : 0,
“Size of the root page” : 0,
“Total number of pages currently in cache” : 0
},
“compression” : {
“compressed page maximum internal page size prior to compression” : 4096,
"compressed page maximum leaf page size prior to compression " : 131072,
“compressed pages read” : 70851721,
“compressed pages written” : 2,
“page written failed to compress” : 0,
“page written was too small to compress” : 6
},
“cursor” : {
“bulk loaded cursor insert calls” : 0,
“cache cursors reuse count” : 32157,
“close calls that result in cache” : 0,
“create calls” : 939,
“insert calls” : 0,
“insert key and value bytes” : 0,
“modify” : 42,
“modify key and value bytes affected” : 72904,
“modify value bytes modified” : 492,
“next calls” : 789697062,
“open cursor count” : 0,
“operation restarted” : 0,
“prev calls” : 0,
“remove calls” : 0,
“remove key bytes removed” : 0,
“reserve calls” : 0,
“reset calls” : 24513035,
“search calls” : 1411150593,
“search near calls” : 6167418,
“truncate calls” : 0,
“update calls” : 0,
“update key and value bytes” : 0,
“update value size change” : 609
},
“reconciliation” : {
“dictionary matches” : 0,
“fast-path pages deleted” : 0,
“internal page key bytes discarded using suffix compression” : 4,
“internal page multi-block writes” : 0,
“internal-page overflow keys” : 0,
“leaf page key bytes discarded using prefix compression” : 0,
“leaf page multi-block writes” : 0,
“leaf-page overflow keys” : 0,
“maximum blocks required for a page” : 1,
“overflow values written” : 0,
“page checksum matches” : 0,
“page reconciliation calls” : 8,
“page reconciliation calls for eviction” : 0,
“pages deleted” : 0
},
“session” : {
“object compaction” : 0
},
“transaction” : {
“update conflicts” : 0
}
},
“nindexes” : 4,
“indexBuilds” : ,
“totalIndexSize” : 9004122112,
“indexSizes” : {
id” : 1383550976,
“articleid_1_clientid_1” : 2106404864,
“KeyType” : 618872832,
“SearchI4All” : 4895293440
},
“scaleFactor” : 1,
“ok” : 1,
“$clusterTime” : {
“clusterTime” : Timestamp(1675846924, 2),
“signature” : {
“hash” : BinData(0,“GWOnqVGCNARShtiUU7I5uAFSyOQ=”),
“keyId” : NumberLong(“7155578345936125954”)
}
},
“operationTime” : Timestamp(1675846924, 2)
}

  1. Mongodb Version: 4.2

Thanks!
Shahnaawaz