{"id":2483,"date":"2025-03-09T09:28:17","date_gmt":"2025-03-09T01:28:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2483"},"modified":"2025-03-09T13:05:53","modified_gmt":"2025-03-09T05:05:53","slug":"mongodb-%e6%9f%a5%e8%af%a2%e5%88%86%e6%9e%90%ef%bc%88query-analysis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/09\/mongodb-%e6%9f%a5%e8%af%a2%e5%88%86%e6%9e%90%ef%bc%88query-analysis%ef%bc%89\/","title":{"rendered":"MongoDB \u67e5\u8be2\u5206\u6790\uff08Query Analysis\uff09"},"content":{"rendered":"\n<p>\u5728 MongoDB \u4e2d\uff0c<strong>\u67e5\u8be2\u5206\u6790<\/strong>\u662f\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u7684\u5173\u952e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>explain()<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\uff0c\u5206\u6790\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5\u3001\u626b\u63cf\u7684\u6587\u6863\u6570\u91cf\u7b49\uff0c\u4ee5\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 <code>explain()<\/code> \u8fdb\u884c\u67e5\u8be2\u5206\u6790<\/strong><\/h2>\n\n\n\n<p>MongoDB \u63d0\u4f9b <code>explain()<\/code> \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\uff0c\u786e\u5b9a\u67e5\u8be2\u662f\u5426\u4f7f\u7528\u4e86\u7d22\u5f15\uff0c\u662f\u5426\u8fdb\u884c\u4e86\u5168\u8868\u626b\u63cf\uff08COLLSCAN\uff09\uff0c\u4ee5\u53ca\u67e5\u8be2\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>db.collection.find({ \u67e5\u8be2\u6761\u4ef6 }).explain()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4e09\u79cd\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"queryPlanner\"<\/code>\uff08\u9ed8\u8ba4\uff09\uff1a\u4ec5\u8fd4\u56de\u67e5\u8be2\u8ba1\u5212\uff0c\u4e0d\u6267\u884c\u67e5\u8be2\u3002<\/li>\n\n\n\n<li><code>\"executionStats\"<\/code>\uff1a\u6267\u884c\u67e5\u8be2\u5e76\u8fd4\u56de\u7edf\u8ba1\u4fe1\u606f\uff08\u63a8\u8350\uff09\u3002<\/li>\n\n\n\n<li><code>\"allPlansExecution\"<\/code>\uff1a\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6267\u884c\u8ba1\u5212\uff0c\u5e76\u6267\u884c\u67e5\u8be2\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>db.collection.find({ name: \"Laptop\" }).explain(\"executionStats\")<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. <code>explain()<\/code> \u7ed3\u679c\u89e3\u6790<\/strong><\/h2>\n\n\n\n<p>\u6267\u884c <code>explain(\"executionStats\")<\/code> \u540e\uff0c\u8fd4\u56de\u7684 JSON \u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"queryPlanner\": {\n    \"winningPlan\": {\n      \"stage\": \"COLLSCAN\"\n    }\n  },\n  \"executionStats\": {\n    \"nReturned\": 1,\n    \"totalKeysExamined\": 0,\n    \"totalDocsExamined\": 1000,\n    \"executionTimeMillis\": 12\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5173\u952e\u5b57\u6bb5\u89e3\u6790<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>\"stage\"<\/code><\/td><td>\u67e5\u8be2\u6267\u884c\u7684\u65b9\u5f0f\uff0c\u5982 <code>COLLSCAN<\/code>\uff08\u5168\u8868\u626b\u63cf\uff09\u3001<code>IXSCAN<\/code>\uff08\u7d22\u5f15\u626b\u63cf\uff09<\/td><\/tr><tr><td><code>\"nReturned\"<\/code><\/td><td>\u8fd4\u56de\u7684\u6587\u6863\u6570\u91cf<\/td><\/tr><tr><td><code>\"totalKeysExamined\"<\/code><\/td><td>\u67e5\u8be2\u4f7f\u7528\u7684\u7d22\u5f15\u6570\u91cf<\/td><\/tr><tr><td><code>\"totalDocsExamined\"<\/code><\/td><td>\u67e5\u8be2\u626b\u63cf\u7684\u6587\u6863\u6570\u91cf\uff08\u5e94\u5c3d\u91cf\u51cf\u5c11\uff09<\/td><\/tr><tr><td><code>\"executionTimeMillis\"<\/code><\/td><td>\u67e5\u8be2\u6267\u884c\u65f6\u95f4\uff08\u5e94\u5c3d\u91cf\u4f18\u5316\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u5982\u679c <code>\"stage\": \"COLLSCAN\"<\/code>\uff08\u5168\u8868\u626b\u63cf\uff09\uff0c\u8bf4\u660e\u67e5\u8be2\u6ca1\u6709\u4f7f\u7528\u7d22\u5f15\uff0c\u5e94\u4f18\u5316\u7d22\u5f15\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5982\u4f55\u4f18\u5316\u67e5\u8be2<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2460 \u521b\u5efa\u7d22\u5f15<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u67e5\u8be2\u662f <code>COLLSCAN<\/code>\uff0c\u53ef\u4ee5\u521b\u5efa\u7d22\u5f15\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.createIndex({ name: 1 })<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u518d\u6b21\u6267\u884c <code>explain(\"executionStats\")<\/code>\uff0c\u5982\u679c <code>\"stage\"<\/code> \u53d8\u4e3a <code>IXSCAN<\/code>\uff0c\u8bf4\u660e\u7d22\u5f15\u5df2\u751f\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2461 \u590d\u5408\u7d22\u5f15\uff08Compound Index\uff09<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u67e5\u8be2\u6d89\u53ca\u591a\u4e2a\u5b57\u6bb5\uff0c\u521b\u5efa\u590d\u5408\u7d22\u5f15\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.createIndex({ category: 1, price: -1 })<\/code><\/pre>\n\n\n\n<p>MongoDB \u53ea\u80fd\u4f7f\u7528<strong>\u4e00\u4e2a\u7d22\u5f15<\/strong>\u6765\u4f18\u5316\u67e5\u8be2\uff0c\u6700\u597d\u8ba9\u7d22\u5f15\u7684\u5b57\u6bb5\u987a\u5e8f\u5339\u914d\u67e5\u8be2\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2462 \u8986\u76d6\u7d22\u5f15<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c <code>totalDocsExamined<\/code> \u4ecd\u7136\u8f83\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>\u8986\u76d6\u7d22\u5f15<\/strong>\u6765\u51cf\u5c11\u6587\u6863\u626b\u63cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ category: \"Electronics\" }, { category: 1, price: 1, _id: 0 }).explain(\"executionStats\")<\/code><\/pre>\n\n\n\n<p>\u5982\u679c <code>\"stage\": \"PROJECTION_COVERED\"<\/code>\uff0c\u8bf4\u660e\u67e5\u8be2\u662f\u8986\u76d6\u7d22\u5f15\uff0c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2463 \u4f7f\u7528 <code>hint()<\/code> \u5f3a\u5236\u7d22\u5f15<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c MongoDB \u6ca1\u6709\u81ea\u52a8\u4f7f\u7528\u7d22\u5f15\uff0c\u53ef\u4ee5\u7528 <code>hint()<\/code> \u624b\u52a8\u6307\u5b9a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ category: \"Electronics\" }).hint({ category: 1 })<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u67e5\u8be2\u4f18\u5316\u6848\u4f8b<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6848\u4f8b 1\uff1a\u672a\u4f7f\u7528\u7d22\u5f15<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.find({ category: \"Electronics\" }).explain(\"executionStats\")<\/code><\/pre>\n\n\n\n<p>\u8fd4\u56de\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"queryPlanner\": {\n    \"winningPlan\": { \"stage\": \"COLLSCAN\" }\n  },\n  \"executionStats\": {\n    \"totalDocsExamined\": 10000,\n    \"executionTimeMillis\": 50\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u4f18\u5316\u65b9\u6848<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.products.createIndex({ category: 1 })<\/code><\/pre>\n\n\n\n<p>\u518d\u6b21\u6267\u884c <code>explain()<\/code>\uff0c\u67e5\u8be2\u4f18\u5316\u6210\u529f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"queryPlanner\": {\n    \"winningPlan\": { \"stage\": \"IXSCAN\" }\n  },\n  \"executionStats\": {\n    \"totalDocsExamined\": 5,\n    \"executionTimeMillis\": 2\n  }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>explain(\"executionStats\")<\/code> \u5206\u6790\u67e5\u8be2\u8ba1\u5212\uff0c\u68c0\u67e5\u662f\u5426\u4f7f\u7528\u7d22\u5f15\u3002<\/li>\n\n\n\n<li>\u5173\u6ce8 <code>stage<\/code> \u662f\u5426\u662f <code>COLLSCAN<\/code>\uff0c\u5982\u679c\u662f\uff0c\u5219\u9700\u8981\u4f18\u5316\u7d22\u5f15\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>createIndex()<\/code> \u521b\u5efa\u7d22\u5f15\uff0c\u51cf\u5c11 <code>totalDocsExamined<\/code> \u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u8986\u76d6\u7d22\u5f15\u53ef\u4ee5\u907f\u514d\u8bfb\u53d6\u6587\u6863\uff0c\u51cf\u5c11\u67e5\u8be2\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>hint()<\/code> \u5f3a\u5236\u4f7f\u7528\u7279\u5b9a\u7d22\u5f15\uff0c\u786e\u4fdd\u67e5\u8be2\u6700\u4f18\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MongoDB \u4e2d\uff0c\u67e5\u8be2\u5206\u6790\u662f\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u7684\u5173\u952e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 explain() \u65b9\u6cd5\u6765\u83b7\u53d6\u67e5\u8be2\u7684\u6267\u884c\u8ba1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2545,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-2483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2483"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2483\/revisions"}],"predecessor-version":[{"id":2484,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2483\/revisions\/2484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2545"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}