我有一个用户的以下模型
{
"TenantId":"abc",
"ContainerId": "xyz",
"Name": "yyy",
"Tags": [
{
"Key":"key1",
"Value":"val1".
}
.....
]
}
TenantId 和 containerId 是 1:n 的关系,因此对于一个 Tenant,可能有多个 ContainerId 项。
我读过,为了获得最大的索引使用率,索引中的第一个字段应该是最具选择性的,以尽量减少 MongoDb 需要扫描的文档。更重要的是,一些人建议最好在该字段上使用“平等”过滤器,而不是范围过滤器。现在,我添加了 2 个索引:
{
"ContanerId":1,
"Tags.Key": 1
}
{
"TenantId":1,
"Tags.Key": 1
}
现在执行 2 个查询,它们返回的结果相等:
find({"TenantId":"abc", "Tags.Key": {$in : ["1","2"]}})
find({"ContainerId":{$in:["abc","xyz",.....]}, "Tags.Key": {$in : ["1","2"]}})
我假设第一个会运行得更快,因为它需要在扫描索引时检查更少的元素。但是,结果似乎几乎相同。有人可以帮我解释一下为什么两个查询的性能相似吗?
第一次查询统计:
{
"executionSuccess":true,
"nReturned":171240,
"executionTimeMillis":602,
"totalKeysExamined":171240,
"totalDocsExamined":171240,
"executionStages":{
"stage":"FETCH",
"nReturned":171240,
"executionTimeMillisEstimate":579,
"works":171241,
"advanced":171240,
"needTime":0,
"needYield":0,
"saveState":1338,
"restoreState":1338,
"isEOF":1,
"invalidates":0,
"docsExamined":171240,
"alreadyHasObj":0,
"inputStage":{
"stage":"IXSCAN",
"nReturned":171240,
"executionTimeMillisEstimate":200,
"works":171241,
"advanced":171240,
"needTime":0,
"needYield":0,
"saveState":1338,
"restoreState":1338,
"isEOF":1,
"invalidates":0,
"keyPattern":{
"PyraTenantId":1,
"IsEmpty":1,
"Tags.KeyValue":1
},
"indexName":"PyraTenantIsEmptyTagsKeyValue",
"isMultiKey":true,
"multiKeyPaths":{
"PyraTenantId":[
],
"IsEmpty":[
],
"Tags.KeyValue":[
"Tags"
]
},
"isUnique":false,
"isSparse":false,
"isPartial":false,
"indexVersion":2,
"direction":"forward",
"indexBounds":{
"PyraTenantId":[
"[\"01fbe4f9-77a5-48a2-8a3d-6b105808f6ac\", \"01fbe4f9-77a5-48a2-8a3d-6b105808f6ac\"]"
],
"IsEmpty":[
"[false, false]"
],
"Tags.KeyValue":[
"[\"01fbe4f9-77a5-48a2-8a3d-6b105808f6ackey6_ddd_01fbe4f9-77a5-48a2-8a3d-6b105808f6acvalue6\", \"01fbe4f9-77a5-48a2-8a3d-6b105808f6ackey6_ddd_01fbe4f9-77a5-48a2-8a3d-6b105808f6acvalue6\"]"
]
},
"keysExamined":171240,
"seeks":1,
"dupsTested":171240,
"dupsDropped":0,
"seenInvalidated":0
}
}
}
第二次查询统计:
{
"executionSuccess" : true,
"nReturned" : 171240,
"executionTimeMillis" : 456,
"totalKeysExamined" : 171412,
"totalDocsExamined" : 171240,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 171240,
"executionTimeMillisEstimate" : 410,
"works" : 171412,
"advanced" : 171240,
"needTime" : 171,
"needYield" : 0,
"saveState" : 1339,
"restoreState" : 1339,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 171240,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 171240,
"executionTimeMillisEstimate" : 290,
"works" : 171412,
"advanced" : 171240,
"needTime" : 171,
"needYield" : 0,
"saveState" : 1339,
"restoreState" : 1339,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"ContainerId" : 1,
"IsEmpty" : 1,
"Tags.KeyValue" : 1
},
"indexName" : "ContainerIdIsEmptyTagsKeyValue",
"isMultiKey" : true,
"multiKeyPaths" : {
"ContainerId" : [],
"IsEmpty" : [],
"Tags.KeyValue" : [
"Tags"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"ContainerId" : [
"[\"01ba96f4-2b21-4c66-ba8d-84fab56ab398\", \"01ba96f4-2b21-4c66-ba8d-84fab56ab398\"]",
"[\"0295187c-5cbe-490d-ad04-83f51f890aec\", \"0295187c-5cbe-490d-ad04-83f51f890aec\"]",
"[\"059ccb9c-0f50-45d7-a126-ad440e3488a2\", \"059ccb9c-0f50-45d7-a126-ad440e3488a2\"]",
"[\"05a887dc-7b2f-4fa5-9256-2220d1d8158f\", \"05a887dc-7b2f-4fa5-9256-2220d1d8158f\"]",
"[\"0d080dce-5d88-4a9e-bf4a-2f03c3aaa7e0\", \"0d080dce-5d88-4a9e-bf4a-2f03c3aaa7e0\"]",
"[\"0d3e9a6a-fa96-40b4-b0ec-23f792c9abcf\", \"0d3e9a6a-fa96-40b4-b0ec-23f792c9abcf\"]",
"[\"10fcff07-12aa-4034-b03f-15e6e73cf310\", \"10fcff07-12aa-4034-b03f-15e6e73cf310\"]",
"[\"137997b0-edde-4898-80dd-090f49905d17\", \"137997b0-edde-4898-80dd-090f49905d17\"]",
"[\"1d663037-28b0-4f37-b1ed-ffdaa237b1b9\", \"1d663037-28b0-4f37-b1ed-ffdaa237b1b9\"]",
"[\"25f51039-f3ef-46d8-94b8-a90aa9c8a6d2\", \"25f51039-f3ef-46d8-94b8-a90aa9c8a6d2\"]",
"[\"298837cc-889b-4fd8-931f-6b47665158e6\", \"298837cc-889b-4fd8-931f-6b47665158e6\"]",
"[\"2ba1fb3c-7d8a-4a28-ba94-631324fc164c\", \"2ba1fb3c-7d8a-4a28-ba94-631324fc164c\"]",
"[\"2d96c883-e24d-4566-bc25-19e5a11417d2\", \"2d96c883-e24d-4566-bc25-19e5a11417d2\"]",
"[\"349d6cd3-ac21-4a4f-a0af-7706bb7c3022\", \"349d6cd3-ac21-4a4f-a0af-7706bb7c3022\"]",
"[\"34fec690-ee08-4eaf-b270-a872972bafe4\", \"34fec690-ee08-4eaf-b270-a872972bafe4\"]",
"[\"35814c64-2619-4513-9b0e-f30814b11117\", \"35814c64-2619-4513-9b0e-f30814b11117\"]",
"[\"40cd26f4-6fd2-477e-91fa-d2542315b30e\", \"40cd26f4-6fd2-477e-91fa-d2542315b30e\"]",
"[\"41a162db-7ba7-4ad9-ace9-1d9bb0536ead\", \"41a162db-7ba7-4ad9-ace9-1d9bb0536ead\"]",
"[\"44e2deb1-ad2c-46d0-bed2-5b23b71ddbeb\", \"44e2deb1-ad2c-46d0-bed2-5b23b71ddbeb\"]",
"[\"524481fc-d949-4371-b179-63e76d949979\", \"524481fc-d949-4371-b179-63e76d949979\"]",
"[\"5ad11ecd-93cf-4166-96c9-6e152ac58667\", \"5ad11ecd-93cf-4166-96c9-6e152ac58667\"]",
"[\"5aea2cbe-33da-48fa-a90d-31da2b108c2d\", \"5aea2cbe-33da-48fa-a90d-31da2b108c2d\"]",
"[\"5d265d15-a16c-4683-9276-66c592e09ba5\", \"5d265d15-a16c-4683-9276-66c592e09ba5\"]",
"[\"6795fd6d-364f-4e63-b656-98b2cf0eed91\", \"6795fd6d-364f-4e63-b656-98b2cf0eed91\"]",
"[\"6a1352a7-5986-4fda-8bc6-98630ffbd967\", \"6a1352a7-5986-4fda-8bc6-98630ffbd967\"]",
"[\"6d5abb48-5413-4347-8d1d-af722098d803\", \"6d5abb48-5413-4347-8d1d-af722098d803\"]",
"[\"702a1754-bc75-4cc8-81bd-d03598b21292\", \"702a1754-bc75-4cc8-81bd-d03598b21292\"]",
"[\"71dd628d-e3eb-46e8-ace4-b102cde0e94f\", \"71dd628d-e3eb-46e8-ace4-b102cde0e94f\"]",
"[\"76b67664-5fee-45b0-ae4f-4b7f53d69779\", \"76b67664-5fee-45b0-ae4f-4b7f53d69779\"]",
"[\"79227dcb-9930-4734-8b8e-efbd4f7ba396\", \"79227dcb-9930-4734-8b8e-efbd4f7ba396\"]",
"[\"7d323987-b873-441d-8e5e-9e9124628468\", \"7d323987-b873-441d-8e5e-9e9124628468\"]",
"[\"807ebe50-8369-4a8d-8176-4b449cdb2932\", \"807ebe50-8369-4a8d-8176-4b449cdb2932\"]",
"[\"8092cc86-1b99-4547-9221-86f97c85bbe7\", \"8092cc86-1b99-4547-9221-86f97c85bbe7\"]",
"[\"819dab0e-ba20-4f4e-80c3-c16f4f9a69a7\", \"819dab0e-ba20-4f4e-80c3-c16f4f9a69a7\"]",
"[\"84329c2c-3d81-48a8-a879-d376b0141b58\", \"84329c2c-3d81-48a8-a879-d376b0141b58\"]",
"[\"88e95e56-861f-4ed3-a745-73f678769a6e\", \"88e95e56-861f-4ed3-a745-73f678769a6e\"]",
"[\"9281aea6-8e7a-4f42-8715-2f185c07894d\", \"9281aea6-8e7a-4f42-8715-2f185c07894d\"]",
"[\"96d1c8c9-d2e4-47d4-a0da-391fe94a1213\", \"96d1c8c9-d2e4-47d4-a0da-391fe94a1213\"]",
"[\"9ed56f80-e34a-4447-b550-6e0f03f809a5\", \"9ed56f80-e34a-4447-b550-6e0f03f809a5\"]",
"[\"a41c232f-6014-417f-b321-e66fc2f306a5\", \"a41c232f-6014-417f-b321-e66fc2f306a5\"]",
"[\"aab965ce-e656-49c7-b66d-2f13cf26df3f\", \"aab965ce-e656-49c7-b66d-2f13cf26df3f\"]",
"[\"af4eb4ec-f8c0-40cb-bc70-09db103c1eb7\", \"af4eb4ec-f8c0-40cb-bc70-09db103c1eb7\"]",
"[\"b8825d8b-a662-4c4c-9b38-7f855e795500\", \"b8825d8b-a662-4c4c-9b38-7f855e795500\"]",
"[\"bc88917c-1145-4e47-b06b-a15f111a7792\", \"bc88917c-1145-4e47-b06b-a15f111a7792\"]",
"[\"bf264dc4-85c8-49db-b43e-cf9d6cc759b0\", \"bf264dc4-85c8-49db-b43e-cf9d6cc759b0\"]",
"[\"cc4bc22b-ce9c-4c56-8760-d36ccd5ac446\", \"cc4bc22b-ce9c-4c56-8760-d36ccd5ac446\"]",
"[\"ce48aaeb-6f6d-4bd7-b9eb-dc45b9a58032\", \"ce48aaeb-6f6d-4bd7-b9eb-dc45b9a58032\"]",
"[\"d0456d39-b3e0-4b86-af46-12d4cbcc7dcd\", \"d0456d39-b3e0-4b86-af46-12d4cbcc7dcd\"]",
"[\"d41b7f5e-3ebb-40c6-9a3e-80e925ded2bb\", \"d41b7f5e-3ebb-40c6-9a3e-80e925ded2bb\"]",
"[\"d842e6c3-0161-47fa-a725-2fd23bfb3cc8\", \"d842e6c3-0161-47fa-a725-2fd23bfb3cc8\"]",
"[\"da991efa-4c25-489f-b813-caed1fb9a202\", \"da991efa-4c25-489f-b813-caed1fb9a202\"]",
"[\"db2a6c16-66ba-4de4-8d9b-47817cf1259b\", \"db2a6c16-66ba-4de4-8d9b-47817cf1259b\"]",
"[\"e0e81bb6-2972-4ae5-ae1c-cbcc886221ae\", \"e0e81bb6-2972-4ae5-ae1c-cbcc886221ae\"]",
"[\"e80403d1-15e7-4bbf-86db-85148b67baa8\", \"e80403d1-15e7-4bbf-86db-85148b67baa8\"]",
"[\"f2667a05-b80b-491e-8f56-b0a17f62ff09\", \"f2667a05-b80b-491e-8f56-b0a17f62ff09\"]",
"[\"f2e00229-cc20-4201-9548-912ed7d53299\", \"f2e00229-cc20-4201-9548-912ed7d53299\"]",
"[\"f4fbd56d-84e5-4fa8-9610-286e779160ff\", \"f4fbd56d-84e5-4fa8-9610-286e779160ff\"]",
"[\"fae208a5-97de-465c-8b12-fb7532edadec\", \"fae208a5-97de-465c-8b12-fb7532edadec\"]"
],
"IsEmpty" : [
"[false, false]"
],
"Tags.KeyValue" : [
"[\"01fbe4f9-77a5-48a2-8a3d-6b105808f6ackey6_ddd_01fbe4f9-77a5-48a2-8a3d-6b105808f6acvalue6\", \"01fbe4f9-77a5-48a2-8a3d-6b105808f6ackey6_ddd_01fbe4f9-77a5-48a2-8a3d-6b105808f6acvalue6\"]"
]
},
"keysExamined" : 171412,
"seeks" : 172,
"dupsTested" : 171240,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}