В моей коллекции MongoDB у меня 28 миллионов документов.Машина имеет 32 ГБ оперативной памяти и 100 ГБ SSD.Машина изолирована для запуска только содержимого MongoDB (без других перехватчиков ресурсов).
У меня есть следующие индексы:
producer.requestInTs # regular (field type: Int64)
client.requestInTs # regular (field type: Int64)
producer.requestInTs, client.requestInTs # compound
Мне нужно выполнить следующие запросы из моего MongoDB:
# 17 million documents, ~5 sec
cursor = collection.find({"producer.requestInTs": {"$gte": start_time, "$lte": end_time}}).count()
# 23 million documents, ~7 sec
cursor = collection.find({"client.requestInTs": {"$gte": start_time, "$lte": end_time}}).count()
# 11 million documents, ~100 sec
cursor = collection.find({"producer.requestInTs": {"$gte": start_time, "$lte": end_time}, "client.requestInTs": {"$gte": start_time, "$lte": end_time}}).count()
Вот вывод db.clean_data.getIndexes()
запроса:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "query_db_test.clean_data"
},
{
"v" : 2,
"key" : {
"producer.requestInTs" : 1,
"client.requestInTs" : 1
},
"name" : "performance",
"ns" : "query_db_test.clean_data"
},
{
"v" : 2,
"key" : {
"client.requestInTs" : 1
},
"name" : "client.requestInTs_1",
"ns" : "query_db_test.clean_data"
},
{
"v" : 2,
"key" : {
"producer.requestInTs" : 1
},
"name" : "producer.requestInTs_1",
"ns" : "query_db_test.clean_data"
}
]
Вот вывод explain
запроса:
{
'ok':1.0,
'serverInfo':{
'port':27017,
'gitVersion':'52e5b5fbaa3a2a5b1a217f5e647b5061817475f9',
'version':'3.4.15',
'host':''
},
'queryPlanner':{
'rejectedPlans':[
{
'inputStage':{
'direction':'forward',
'multiKeyPaths':{
'producer.requestInTs':[
]
},
'isMultiKey':False,
'stage':'IXSCAN',
'isSparse':False,
'indexName':'producer.requestInTs_1',
'isPartial':False,
'indexBounds':{
'producer.requestInTs':[
'[0, 2527594134000]'
]
},
'isUnique':False,
'indexVersion':2,
'keyPattern':{
'producer.requestInTs':1
}
},
'stage':'FETCH',
'filter':{
'$and':[
{
'client.requestInTs':{
'$lte':2527594134000
}
},
{
'client.requestInTs':{
'$gte':0
}
}
]
}
},
{
'inputStage':{
'direction':'forward',
'multiKeyPaths':{
'producer.requestInTs':[
],
'client.requestInTs':[
]
},
'isMultiKey':False,
'stage':'IXSCAN',
'isSparse':False,
'indexName':'performance',
'isPartial':False,
'indexBounds':{
'producer.requestInTs':[
'[0, 2527594134000]'
],
'client.requestInTs':[
'[0, 2527594134000]'
]
},
'isUnique':False,
'indexVersion':2,
'keyPattern':{
'producer.requestInTs':1,
'client.requestInTs':1
}
},
'stage':'FETCH'
}
],
'plannerVersion':1,
'winningPlan':{
'inputStage':{
'direction':'forward',
'multiKeyPaths':{
'client.requestInTs':[
]
},
'isMultiKey':False,
'stage':'IXSCAN',
'isSparse':False,
'indexName':'client.requestInTs_1',
'isPartial':False,
'indexBounds':{
'client.requestInTs':[
'[0, 2527594134000]'
]
},
'isUnique':False,
'indexVersion':2,
'keyPattern':{
'client.requestInTs':1
}
},
'stage':'FETCH',
'filter':{
'$and':[
{
'producer.requestInTs':{
'$lte':2527594134000
}
},
{
'producer.requestInTs':{
'$gte':0
}
}
]
}
},
'parsedQuery':{
'$and':[
{
'client.requestInTs':{
'$lte':2527594134000
}
},
{
'producer.requestInTs':{
'$lte':2527594134000
}
},
{
'client.requestInTs':{
'$gte':0
}
},
{
'producer.requestInTs':{
'$gte':0
}
}
]
},
'namespace':'query_db_test.clean_data',
'indexFilterSet':False
},
'executionStats':{
'nReturned':11712728,
'executionSuccess':True,
'totalKeysExamined':23086735,
'allPlansExecution':[
{
'nReturned':73,
'totalKeysExamined':111,
'executionStages':{
'restoreState':2,
'filter':{
'$and':[
{
'client.requestInTs':{
'$lte':2527594134000
}
},
{
'client.requestInTs':{
'$gte':0
}
}
]
},
'inputStage':{
'direction':'forward',
'restoreState':2,
'multiKeyPaths':{
'producer.requestInTs':[
]
},
'needTime':0,
'indexBounds':{
'producer.requestInTs':[
'[0, 2527594134000]'
]
},
'nReturned':111,
'dupsDropped':0,
'stage':'IXSCAN',
'seenInvalidated':0,
'needYield':0,
'invalidates':0,
'keyPattern':{
'producer.requestInTs':1
},
'isPartial':False,
'isEOF':0,
'isSparse':False,
'indexVersion':2,
'indexName':'producer.requestInTs_1',
'isUnique':False,
'keysExamined':111,
'saveState':2,
'seeks':1,
'isMultiKey':False,
'works':111,
'dupsTested':0,
'advanced':111,
'executionTimeMillisEstimate':0
},
'needTime':38,
'alreadyHasObj':0,
'saveState':2,
'nReturned':73,
'docsExamined':111,
'executionTimeMillisEstimate':0,
'needYield':0,
'works':111,
'invalidates':0,
'isEOF':0,
'advanced':73,
'stage':'FETCH'
},
'executionTimeMillisEstimate':0,
'totalDocsExamined':111
},
{
'nReturned':73,
'totalKeysExamined':111,
'executionStages':{
'restoreState':2,
'inputStage':{
'direction':'forward',
'restoreState':2,
'multiKeyPaths':{
'producer.requestInTs':[
],
'client.requestInTs':[
]
},
'needTime':38,
'indexBounds':{
'producer.requestInTs':[
'[0, 2527594134000]'
],
'client.requestInTs':[
'[0, 2527594134000]'
]
},
'nReturned':73,
'dupsDropped':0,
'stage':'IXSCAN',
'seenInvalidated':0,
'needYield':0,
'invalidates':0,
'keyPattern':{
'producer.requestInTs':1,
'client.requestInTs':1
},
'isPartial':False,
'isEOF':0,
'isSparse':False,
'indexVersion':2,
'indexName':'performance',
'isUnique':False,
'keysExamined':111,
'saveState':2,
'seeks':38,
'isMultiKey':False,
'works':111,
'dupsTested':0,
'advanced':73,
'executionTimeMillisEstimate':0
},
'needTime':38,
'alreadyHasObj':0,
'saveState':2,
'nReturned':73,
'docsExamined':73,
'stage':'FETCH',
'needYield':0,
'works':111,
'invalidates':0,
'isEOF':0,
'advanced':73,
'executionTimeMillisEstimate':0
},
'executionTimeMillisEstimate':0,
'totalDocsExamined':73
},
{
'nReturned':101,
'totalKeysExamined':111,
'executionStages':{
'restoreState':2,
'filter':{
'$and':[
{
'producer.requestInTs':{
'$lte':2527594134000
}
},
{
'producer.requestInTs':{
'$gte':0
}
}
]
},
'inputStage':{
'direction':'forward',
'restoreState':2,
'multiKeyPaths':{
'client.requestInTs':[
]
},
'needTime':0,
'indexBounds':{
'client.requestInTs':[
'[0, 2527594134000]'
]
},
'nReturned':111,
'dupsDropped':0,
'stage':'IXSCAN',
'seenInvalidated':0,
'needYield':0,
'invalidates':0,
'keyPattern':{
'client.requestInTs':1
},
'isPartial':False,
'isEOF':0,
'isSparse':False,
'indexVersion':2,
'indexName':'client.requestInTs_1',
'isUnique':False,
'keysExamined':111,
'saveState':2,
'seeks':1,
'isMultiKey':False,
'works':111,
'dupsTested':0,
'advanced':111,
'executionTimeMillisEstimate':0
},
'needTime':10,
'alreadyHasObj':0,
'saveState':2,
'nReturned':101,
'docsExamined':111,
'executionTimeMillisEstimate':0,
'needYield':0,
'works':111,
'invalidates':0,
'isEOF':0,
'advanced':101,
'stage':'FETCH'
},
'executionTimeMillisEstimate':0,
'totalDocsExamined':111
}
],
'executionStages':{
'restoreState':181031,
'filter':{
'$and':[
{
'producer.requestInTs':{
'$lte':2527594134000
}
},
{
'producer.requestInTs':{
'$gte':0
}
}
]
},
'inputStage':{
'direction':'forward',
'restoreState':181031,
'multiKeyPaths':{
'client.requestInTs':[
]
},
'needTime':0,
'indexBounds':{
'client.requestInTs':[
'[0, 2527594134000]'
]
},
'nReturned':23086735,
'dupsDropped':0,
'stage':'IXSCAN',
'seenInvalidated':0,
'needYield':0,
'invalidates':0,
'keyPattern':{
'client.requestInTs':1
},
'isPartial':False,
'isEOF':1,
'isSparse':False,
'indexVersion':2,
'indexName':'client.requestInTs_1',
'isUnique':False,
'keysExamined':23086735,
'saveState':181031,
'seeks':1,
'isMultiKey':False,
'works':23086736,
'dupsTested':0,
'advanced':23086735,
'executionTimeMillisEstimate':8609
},
'needTime':11374007,
'alreadyHasObj':0,
'saveState':181031,
'nReturned':11712728,
'docsExamined':23086735,
'executionTimeMillisEstimate':98716,
'needYield':0,
'works':23086736,
'invalidates':0,
'isEOF':1,
'advanced':11712728,
'stage':'FETCH'
},
'executionTimeMillis':102699,
'totalDocsExamined':23086735
}
}
Вотвывод для запроса db.collection.stats()
:
{
"ns": "query_db_test.clean_data",
"size": 56227937613,
"count": 28302965,
"avgObjSize": 1986,
"storageSize": 16116736000,
"capped": false,
"wiredTiger": {
"metadata": {
"formatVersion": 1
},
"creationString": "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),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=true),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_max=15,merge_min=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-5--7054136916324318927",
"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": 0,
"blocks allocated": 0,
"blocks freed": 0,
"checkpoint size": 16116039680,
"file allocation unit size": 4096,
"file bytes available for reuse": 679936,
"file magic number": 120897,
"file major version number": 1,
"file size in bytes": 16116736000,
"minor version number": 0
},
"btree": {
"btree checkpoint generation": 8,
"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 internal pages": 0,
"row-store leaf pages": 0
},
"cache": {
"bytes currently in the cache": 12792138927,
"bytes read into cache": 250345438600,
"bytes written from cache": 0,
"checkpoint blocked page eviction": 0,
"data source pages selected for eviction unable to be evicted": 105,
"hazard pointer blocked page eviction": 96,
"in-memory page passed criteria to be split": 0,
"in-memory page splits": 0,
"internal pages evicted": 930,
"internal pages split during eviction": 0,
"leaf pages split during eviction": 0,
"modified pages evicted": 0,
"overflow pages read into cache": 0,
"overflow values cached in memory": 0,
"page split during eviction deepened the tree": 0,
"page written requiring lookaside records": 0,
"pages read into cache": 9082966,
"pages read into cache requiring lookaside entries": 0,
"pages requested from the cache": 199777277,
"pages written from cache": 0,
"pages written requiring in-memory restoration": 0,
"tracked dirty bytes in the cache": 0,
"unmodified pages evicted": 8620195
},
"cache_walk": {
"Average difference between current eviction generation when the page was last considered": 0,
"Average on-disk page image size seen": 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,
"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 pages read": 9059006,
"compressed pages written": 0,
"page written failed to compress": 0,
"page written was too small to compress": 0,
"raw compression call failed, additional data available": 0,
"raw compression call failed, no additional data available": 0,
"raw compression call succeeded": 0
},
"cursor": {
"bulk-loaded cursor-insert calls": 0,
"create calls": 8,
"cursor-insert key and value bytes inserted": 0,
"cursor-remove key bytes removed": 0,
"cursor-update value bytes updated": 0,
"insert calls": 0,
"next calls": 28302986,
"prev calls": 1,
"remove calls": 0,
"reset calls": 1526364,
"restarted searches": 0,
"search calls": 85076270,
"search near calls": 223128,
"truncate calls": 0,
"update calls": 0
},
"reconciliation": {
"dictionary matches": 0,
"fast-path pages deleted": 0,
"internal page key bytes discarded using suffix compression": 0,
"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": 0,
"overflow values written": 0,
"page checksum matches": 0,
"page reconciliation calls": 0,
"page reconciliation calls for eviction": 0,
"pages deleted": 0
},
"session": {
"object compaction": 0,
"open cursor count": 8
},
"transaction": {
"update conflicts": 0
}
},
"nindexes": 4,
"totalIndexSize": 1292075008,
"indexSizes": {
"_id_": 297906176,
"client.requestInTs_1": 301621248,
"producer.requestInTs_1": 255086592,
"producer.requestInTs_1_client.requestInTs_1": 437460992
},
"ok": 1
}
Вот пример документа:
{
"_id": ObjectId("59ebc4e84f413c70b03b50c2"),
"clientResponseSize": null,
"producerRequestSize": null,
"producer": {
"messageProtocolVersion":"string_here",
"requestAttachmentCount":0,
"serviceXRoadInstance":"string_here",
"serviceMemberCode":"string_here",
"representedPartyClass":null,
"_id":ObjectId("59e6595d97b2ca507adc8383"),
"responseMimeSize":1712,
"serviceSecurityServerAddress":"string_here",
"clientMemberClass":"string_here",
"responseOutTs":NumberLong("1494334810378"),
"soapFaultString":null,
"representedPartyCode":null,
"requestInTs":NumberLong("1494334807497"),
"succeeded":true,
"messageIssue":null,
"requestOutTs":NumberLong("1494334808900"),
"securityServerInternalIp":"string_here",
"clientSecurityServerAddress":"string_here",
"requestMimeSize":null,
"serviceMemberClass":"string_here",
"insertTime":1508268360.8759887,
"requestSoapSize":1293,
"responseSoapSize":1684,
"clientMemberCode":"string_here",
"serviceCode":"string_here",
"messageId":"string_here",
"clientXRoadInstance":"string_here",
"monitoringDataTs":1494334810,
"clientSubsystemCode":"string_here",
"soapFaultCode":null,
"responseAttachmentCount":1,
"securityServerType":"string_here",
"responseInTs":NumberLong("1494334808946"),
"serviceSubsystemCode":"string_here",
"messageUserId":"string_here",
"serviceVersion":"string_here"
},
"producerIsDuration": null,
"matchingType": "string_here",
"requestNwDuration": null,
"responseNwDuration": null,
"client": {
"messageProtocolVersion": "string_here",
"requestAttachmentCount": null,
"serviceXRoadInstance": "string_here",
"serviceMemberCode": "string_here",
"representedPartyClass": null,
"_id": ObjectId("59e659a497b2ca50907e9a1a"),
"responseMimeSize": null,
"serviceSecurityServerAddress": "string_here",
"clientMemberClass": "string_here",
"responseOutTs": NumberLong("1494351723876"),
"soapFaultString": "string_here",
"representedPartyCode": null,
"requestInTs": NumberLong("1494351723762"),
"succeeded": false,
"messageIssue": null,
"requestOutTs": NumberLong("1494351723869"),
"securityServerInternalIp": "string_here",
"clientSecurityServerAddress": "string_here",
"requestMimeSize": null,
"serviceMemberClass": "string_here",
"insertTime": 1508268421.6967819,
"requestSoapSize": 1056,
"responseSoapSize": null,
"clientMemberCode": "string_here",
"serviceCode": "string_here",
"messageId": "string_here",
"clientXRoadInstance": "string_here",
"monitoringDataTs": 1494351723,
"clientSubsystemCode": "string_here",
"soapFaultCode": "string_here",
"responseAttachmentCount": null,
"securityServerType": "string_here",
"responseInTs": null,
"serviceSubsystemCode": "string_here",
"messageUserId": "string_here",
"serviceVersion": "string_here"
},
"producerSsRequestDuration": null,
"producerDurationClientView": null,
"correctorStatus": "done",
"totalDuration": 114,
"correctorTime": 1509006319.0692048,
"clientRequestSize": 1056,
"messageId": "string_here",
"producerSsResponseDuration": null,
"clientSsRequestDuration": 107,
"producerDurationProducerView": null,
"producerResponseSize": null,
"producerHash": null,
"clientSsResponseDuration": null,
"clientHash": "string_here"
}
Может кто-нибудь объяснить, почему последний запрос занимает столько времени?