{"id":962,"date":"2022-03-22T13:41:44","date_gmt":"2022-03-22T05:41:44","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=962"},"modified":"2022-03-22T21:32:37","modified_gmt":"2022-03-22T13:32:37","slug":"8-mysql%e7%b4%a2%e5%bc%95%e5%8f%8a%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=962","title":{"rendered":"8.MySQL\u7d22\u5f15\u53ca\u6267\u884c\u8ba1\u5212"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.\u7d22\u5f15\u4ecb\u7ecd<\/h2>\n\n\n\n<p>\u7d22\u5f15\u76f8\u5f53\u4e8e\u4e00\u672c\u4e66\u4e2d\u7684\u76ee\u5f55\uff0c\u53ef\u4ee5\u52a0\u901f\u67e5\u8be2\uff08select,update,delete\uff09\uff0c\u4f46\u662f\u8fc7\u591a\u7684\u7d22\u5f15\u4f1a\u5bfc\u81f4insert\u53d8\u5f97\u5f88\u6162\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.\u7d22\u5f15\u7684\u79cd\u7c7b<\/h2>\n\n\n\n<p>Btree\uff08\u5e73\u8861\u591a\u53c9\u6811\uff09\uff1ab-tree b+tree\uff08b*tree\uff09\uff0c\u4f18\u70b9\uff1a\u64c5\u957f\u8303\u56f4\u67e5\u627e<br>HASH\uff1a\u4f18\u70b9\uff0c\u9002\u5408\u968f\u673a\u7684\u7b49\u503c\u67e5\u8be2<br>Rtree\u542c\u542c\u8fd9\u4e2a\u540d\u8bcd\u5c31\u884c\uff0c\u4e0d\u5fc5\u4e86\u89e3<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">3.Btree\u7684\u7ec6\u5206<\/h2>\n\n\n\n<p>\u805a\u7c07\u7d22\u5f15\uff1a\u4e3b\u952e\u7d22\u5f15<br>\u8f85\u52a9\u7d22\u5f15\uff1a\u9664\u4e86\u4e3b\u952e\u4e4b\u5916\u7684\u7d22\u5f15<br>           \u5355\u5217<br>           \u8054\u5408<br>           \u552f\u4e00<br>           \u524d\u7f00<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">4.\u7d22\u5f15\u7684\u7ba1\u7406<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>##\u7d22\u5f15\u5efa\u7acb\u4e4b\u524d\u538b\u6d4b\u770b\u770b\u6548\u679c\uff1a<\/strong>\nmysqlslap --defaults-file=\/etc\/my.cnf --concurrency=100 --iterations=1 --create-schema='test' --query=\"select * from test.t100w where k2='VWLM'\" engine=innodb --number-of-queries=2000 -verbose\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"118\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479305471-1024x118.png\" alt=\"\" class=\"wp-image-971\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479305471-1024x118.png 1024w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479305471-300x35.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479305471-768x89.png 768w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479305471-1536x177.png 1536w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479305471.png 1909w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u53ef\u4ee5\u770b\u5230\u67e5\u8be2\u7684\u5f88\u6162<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6dfb\u52a0\u7d22\u5f15\u540e\u538b\u6d4b\u8bd5\u8bd5\nmysql&gt; alter table test.t100w add index idx_k2(k2);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"116\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479307081-1024x116.png\" alt=\"\" class=\"wp-image-972\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479307081-1024x116.png 1024w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479307081-300x34.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479307081-768x87.png 768w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479307081-1536x174.png 1536w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/16479307081.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u53ef\u4ee5\u770b\u5230\u53ea\u7528\u4e866\u79d2\u591a\u7684\u65f6\u95f4\uff0c\u6548\u679c\u5341\u5206\u660e\u663e<\/figcaption><\/figure>\n\n\n\n<p><strong><em>1.\u67e5\u8be2\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; desc world.city;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"261\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-87.png\" alt=\"\" class=\"wp-image-965\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-87.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-87-300x91.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-87-768x232.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><figcaption>PRI\uff1a\u4e3b\u952e\u7d22\u5f15<br>MUL\uff1a\u666e\u901a\u7d22\u5f15<br>NUI\uff1a\u552f\u4e00\u7d22\u5f15<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\u67e5\u770b\u66f4\u8be6\u7ec6\u7684\u7d22\u5f15\nmysql&gt; show index from world.city;\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"83\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-88.png\" alt=\"\" class=\"wp-image-966\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-88.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-88-300x29.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-88-768x74.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><strong>PS:\u5efa\u7acb\u7d22\u5f15\u524d\u5982\u4f55\u8ba1\u7b97\u7d22\u5f15\u7684\u57fa\u6570Cardinality<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b9e\u9645\u4e0a\u5c31\u662f\u7edf\u8ba1\u53bb\u91cd\u540e\u7684\u5217\u6570\nselect count(distinct countrycode) from city;\ndistinct \u662f\u53bb\u91cd\u7684\u56fa\u5b9a\u5199\u6cd5 countrycode\u662f\u8981\u5bf9\u90a3\u5217\u53bb\u91cd\n\u7136\u540e\u7edf\u8ba1\u8868\u7684\u603b\u884c\u6570\nselect count(*) from city;\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"261\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-89.png\" alt=\"\" class=\"wp-image-967\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-89.png 627w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-89-300x125.png 300w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><figcaption>\u5982\u56fe\uff0c\u4f7f\u7528232\/4079\u6570\u503c\u8d8a\u5927\u8d8a\u9002\u5408\u505a\u7d22\u5f15<\/figcaption><\/figure>\n\n\n\n<p><strong><em>2.\u5efa\u7acb\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<p>1.\u5355\u5217\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; alter table world.city add index i_name(name);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"207\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-90.png\" alt=\"\" class=\"wp-image-968\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-90.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-90-300x72.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-90-768x184.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p>2.\u8054\u5408\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; alter table world.city add index i_d_p(district,population);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"223\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-91.png\" alt=\"\" class=\"wp-image-969\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-91.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-91-300x77.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-91-768x198.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p>3.\u524d\u7f00\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; alter table city add index i_n(name (10));<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"119\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-92.png\" alt=\"\" class=\"wp-image-970\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-92.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-92-300x41.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-92-768x106.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p>4.\u552f\u4e00\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; alter table tt add unique index ui(telnum);<\/code><\/pre>\n\n\n\n<p><strong><em>3.\u5220\u9664\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u53ef\u4ee5\u5148\u67e5\u4e00\u4e0b\u7d22\u5f15\u7684\u540d\u5b57\nmysql&gt; show index from world.city;\nmysql&gt; alter table world.city drop index i_d_p;<\/code><\/pre>\n\n\n\n<p><strong><em>4.\u5f69\u86cb 8.0\u65b0\u7279\u6027\u4e0d\u53ef\u89c1\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>invisible index \u4e0d\u53ef\u89c1\u7d22\u5f15\uff08\u9690\u85cf\u7d22\u5f15\uff0c\u4e0d\u77e5\u9053\u7d22\u5f15\u8fd8\u6709\u6ca1\u6709\u7528\uff0c\u5c31\u5148\u628a\u7d22\u5f15\u9690\u85cf\u8d77\u6765\uff0c\u4e0d\u8ba9\u4e1a\u52a1\u770b\u5230\uff09\n\n<strong>\u67e5\u8be2\u4e00\u4e0b\u7d22\u5f15\u662f\u5426\u53ef\u89c1<\/strong>\nmysql&gt; select INDEX_NAME,IS_VISIBLE FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='world' AND table_name='city';\n<strong>\u53c2\u8003\u7f51\u5740\uff1a<\/strong>https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/invisible-indexes.html\n<strong>\u4fee\u6539\u4e3a\u4e0d\u53ef\u89c1<\/strong>\nALTER TABLE t1 ALTER INDEX i_idx INVISIBLE;\n<strong>\u4fee\u6539\u4e3a\u53ef\u89c1<\/strong>\nALTER TABLE t1 ALTER INDEX i_idx VISIBLE;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">5.B+tree\u7d22\u5f15\u67e5\u627e\u7b97\u6cd5\u4ecb\u7ecd<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"293\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-93.png\" alt=\"\" class=\"wp-image-979\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-93.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-93-300x102.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-93-768x260.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><strong><em>1.\u5e73\u8861<\/em><\/strong><\/p>\n\n\n\n<p>\u4e0d\u7ba1\u67e5\u627e\u54ea\u4e2a\u6570\uff0c\u9700\u8981\u67e5\u627e\u6b21\u6570\u7406\u8bba\u4e0a\u662f\u76f8\u540c\u7684\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e09\u5c42\u7684b\u6811\u6765\u8bb2\uff0c\u7406\u8bba\u4e0a\u67e5\u627e\u6bcf\u4e2a\u503c\u90fd\u662f\u4e09\u6b21IO<\/p>\n\n\n\n<p><strong><em>2.\u64c5\u957f\u8303\u56f4\u67e5\u627e<\/em><\/strong><\/p>\n\n\n\n<p>\u8bb2\u7a76\u5feb\u901f\u9501\u5b9a\u8303\u56f4\u3002<br>B+tree\uff0c\u52a0\u5165\u4e86\u53cc\u5411\u6307\u9488\uff08\u5934\u5c3e\u76f8\u63a5\uff09\uff0c\u8fdb\u4e00\u6b65\u589e\u5f3a\u8303\u56f4\u67e5\u627e\uff0c\u51cf\u5c11\u5bf9\u4e8eROOT\u548cNON-LEAF\u7684\u8bbf\u95ee\u6b21\u6570\u3002<\/p>\n\n\n\n<p><strong><em>3.\u6784\u5efa\u8fc7\u7a0b<\/em><\/strong><\/p>\n\n\n\n<p>\u53f6\u5b50\uff1a\u5148\u5c06\u6570\u636e\u6392\u5e8f\uff0c\u751f\u6210\u53f6\u5b50\u8282\u70b9\u3002<br>\u679d\uff1a\u4fdd\u5b58\u4e0b\u5c42\u53f6\u5b50\u8282\u70b9\u7684\u8303\u56f4\uff08&gt;=1 &lt;5\uff09+\u6307\u9488\uff08\uf0e0\uff09<br>\u6839\uff1a\u4fdd\u5b58\u679d\u8282\u70b9\u8303\u56f4+\u6307\u9488<br>\u53f6\u5b50\u8282\u70b9\u548c\u679d\u8282\u70b9\u90fd\u6709\u53cc\u5411\u7684\u6307\u9488\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">6.MySQL\u4e2d\u5982\u4f55\u5e94\u7528B+TREE<\/h2>\n\n\n\n<p><strong><em>1.\u540d\u8bcd\u89e3\u91ca<\/em><\/strong><\/p>\n\n\n\n<p>\u533a\uff08\u7c07\uff09\uff1a\u8fde\u7eed\u768464pages\uff0c\u9ed8\u8ba4\u662f1M\u5b58\u50a8\u7a7a\u95f4\u3002<br>page\u9875\uff1a16KB\u5927\u5c0f\uff0cMySQL\u4e2d\u6700\u5c0f\u7684IO\u5355\u5143\u3002<\/p>\n\n\n\n<p><strong><em>2.IOT \u7ec4\u7ec7\u8868<\/em><\/strong><\/p>\n\n\n\n<p>\u6570\u636e\u5e94\u8be5\u5b89\u88c5\u7d22\u5f15\u7ed3\u6784\u6709\u5e8f\uff08\u987a\u5e8f\uff09\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u3002<br>MySQL\u4e2d\u4f7f\u7528\u805a\u7c07\u7d22\u5f15\u7ec4\u7ec7\u5b58\u50a8\u6570\u636e\u3002<\/p>\n\n\n\n<p><strong><em>3.\u805a\u7c07\uff08\u533a\uff09\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<p>1.\u6784\u5efa\u6761\u4ef6<\/p>\n\n\n\n<p>1.\u5982\u679c\u8868\u4e2d\u6709\u4e3b\u952e\uff0c\u4e3b\u952e\u5c31\u88ab\u4f5c\u4e3a\u805a\u7c07\u7d22\u5f15<br>2.\u5982\u679c\u6ca1\u6709\u4e3b\u952e\uff0c\u7b2c\u4e00\u4e2a\u4e0d\u4e3a\u7a7a\u7684\u552f\u4e00\u952e<br>3.\u4ec0\u4e48\u90fd\u6ca1\u6709\u600e\u4e48\u529e\uff0c\u81ea\u52a8\u751f\u6210\u4e00\u4e2a6\u5b57\u8282\u7684\u9690\u85cf\u5217\uff0c\u4f5c\u4e3a\u805a\u7c07\u7d22\u5f15<\/p>\n\n\n\n<p>2.\u5982\u4f55\u5f62\u6210B\u6811\u7ed3\u6784<\/p>\n\n\n\n<p>\u53f6\u5b50\u8282\u70b9\uff1a\u805a\u7c07\u7d22\u5f15\u7ec4\u7ec7\u8868\uff0c\u5b58\u6570\u636e\u65f6\uff0c\u5df2\u7ecf\u662f\u6309\u7167ID\u5217\u6709\u5e8f\u5b58\u50a8\u6570\u636e\u5230\u5404\u4e2a\u8fde\u7eed\u7684\u6570\u636e\u9875\u4e2d\uff0c\u539f\u8868\u6570\u636e\u5b58\u50a8\u7ed3\u6784\u5c31\u662f\u53f6\u5b50\u8282\u70b9\u3002<br>\u679d\u8282\u70b9\uff1a\u53f6\u5b50\u8282\u70b9\u4e2dID\u8303\u56f4+\u6307\u9488<br>\u6839\u8282\u70b9\uff1a\u679d\u8282\u70b9ID\u8303\u56f4+\u6307\u9488<\/p>\n\n\n\n<p>3.\u4f18\u5316\u4e86\u54ea\u4e9b\u67e5\u8be2<\/p>\n\n\n\n<p>\u53ea\u80fd\u4f18\u5316\u57fa\u4e8eID\u4f5c\u4e3a\u6761\u4ef6\uff0c\u7d22\u5f15\u5355\u7eaf\u4f7f\u7528ID\u5217\u67e5\u8be2\uff0c\u5f88\u5c40\u9650<\/p>\n\n\n\n<p><strong><em>4.\u8f85\u52a9\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<p>1.\u6784\u5efa\u6761\u4ef6<\/p>\n\n\n\n<p>\u9700\u8981\u4eba\u4e3a\u7684\u6309\u7167\u9700\u6c42\u521b\u5efa\u8f85\u52a9\u7d22\u5f15<\/p>\n\n\n\n<p>2.\u5982\u4f55\u5f62\u6210B\u6811\u7ed3\u6784<\/p>\n\n\n\n<p>\u53f6\u5b50\u8282\u70b9\uff1a\u5c06\u8f85\u52a9\u7d22\u5f15\u7684\u5217\u503c+ID\u5217\u63d0\u53d6\u51fa\u6765\uff0c\u6309\u7167\u5217\u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u5b58\u50a8\u81f3\u53f6\u5b50\u8282\u70b9<br>\u679d\u8282\u70b9\uff1a\u5b58\u50a8\u53f6\u5b50\u8282\u70b9\u7684\u5217\u503c\u8303\u56f4+\u6307\u9488<br>\u6839\u8282\u70b9\uff1a\u5b58\u50a8\u679d\u8282\u70b9\u7684\u8303\u56f4+\u6307\u9488<\/p>\n\n\n\n<p>3.\u4f18\u5316\u4e86\u54ea\u4e9b\u67e5\u8be2<\/p>\n\n\n\n<p>\u5982\u679c\u67e5\u8be2\u6761\u4ef6\u4f7f\u7528\u4e86name\u5217\uff0c\u90fd\u4f1a\u5148\u626b\u63cf\u8f85\u52a9\u7d22\u5f15\uff0c\u83b7\u5f97ID\uff0c\u518d\u56de\u5230\u805a\u7c07\u7d22\u5f15\uff08\u56de\u8868\uff09\uff0c\u6309\u7167ID\u8fdb\u884c\u805a\u7c07\u7d22\u5f15\u626b\u63cf\uff0c\u6700\u7ec8\u83b7\u53d6\u5230\u6570\u636e\u884c<\/p>\n\n\n\n<p><strong><em>5.\u8054\u5408\u8f85\u52a9\u7d22\u5f15<\/em><\/strong><\/p>\n\n\n\n<p>1.\u5982\u4f55\u5f62\u6210\u8f85\u52a9\u8054\u5408\u7d22\u5f15<\/p>\n\n\n\n<p>\u53f6\u5b50\u8282\u70b9\uff1a\u6240\u6709\u8f85\u52a9\u7d22\u5f15+ID \u5e76\u6392\u5e8f\uff0c\u751f\u6210\u53f6\u5b50\u8282\u70b9<br>\u679d\u8282\u70b9\uff1a\u5b58\u50a8\u6700\u5de6\u5217\u53f6\u5b50\u8282\u70b9\u8f85\u52a9\u7d22\u5f15\u8303\u56f4+\u6307\u9488<br>\u6839\u8282\u70b9\uff1a\u5b58\u50a8\u679d\u8282\u70b9\u8303\u56f4+\u6307\u9488<\/p>\n\n\n\n<p>2.\u4f18\u5316\u4e86\u54ea\u4e9b\u67e5\u8be2<\/p>\n\n\n\n<p>\u67e5\u8be2\u6761\u4ef6\u4e2d\u5fc5\u987b\u5305\u542b\u6700\u5de6\u5217\u6761\u4ef6\uff0c\u5148\u901a\u8fc7\u5de6\u5217\u6761\u4ef6\u626b\u63cf\u8054\u5408\u7d22\u5f15\u7684\u6839\u8282\u70b9\u548c\u679d\u8282\u70b9\uff0c\u5f97\u5230\u53f6\u5b50\u8282\u70b9\u8303\u56f4\uff0c\u518d\u62ff\u53f3\u4fa7\u8f85\u52a9\u7d22\u5f15\u8fc7\u6ee4\u4e00\u6b21\u3002\u5f97\u5230\u66f4\u7cbe\u786e\u7684ID\u503c\uff0c\u7406\u8bba\u4e0a\u51cf\u5c11\u56de\u8868\u6b21\u6570<\/p>\n\n\n\n<p>3.\u7ed3\u8bba<\/p>\n\n\n\n<p>\u5efa\u7acb\u8054\u5408\u7d22\u5f15\u65f6\u3002\u9009\u62e9\u57fa\u6570\u5927\uff08\u91cd\u590d\u503c\u5c11\uff09\u4f5c\u4e3a\u6700\u5de6\u5217\uff0c<br>\u67e5\u8be2\u6761\u4ef6\u4e2d\u5fc5\u987b\u8981\u5305\u542b\u6700\u5de6\u5217\u6761\u4ef6<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">7.\u7d22\u5f15\u6811\u9ad8\u5ea6<\/h2>\n\n\n\n<p>\u4e00\u822c\u5efa\u8bae3-4\u5c42\u4e3a\u4f73\uff0c3\u5c42B\u6811\uff0c2000w+\u884c\u6570\u636e<br><strong>a.\u6570\u636e\u884c\u591a\u4f1a\u5bfc\u81f4\u9ad8\u5ea6\u8fc7\u9ad8<\/strong><br>\u8868\u5206\u533a<br>\u5b9a\u671f\u5f52\u6863\uff1a\u4e00\u822c\u6309\u7167\u65f6\u95f4\u5b57\u6bb5\uff0c\u5b9a\u671f\u5f52\u6863\u5230\u5386\u53f2\u5e93\u4e2d\u3002pt-archiver\u5de5\u5177<br>\u5206\u5e93\u5206\u8868\uff1a\u5206\u5e03\u5f0f<br><strong>b.\u7d22\u5f15\u5217\u957f\u5ea6\u8fc7\u957f\u4f1a\u5bfc\u81f4\u9ad8\u5ea6\u8fc7\u9ad8<\/strong><br>\u524d\u7f00\u7d22\u5f15<br><strong>c.\u6570\u636e\u7c7b\u578b\u4f1a\u5bfc\u81f4\u9ad8\u5ea6\u8fc7\u9ad8<\/strong><br>\u8db3\u591f<br>\u7b80\u77ed<br>\u5408\u9002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">8.\u56de\u8868\u95ee\u9898<\/h2>\n\n\n\n<p><strong><em>1.\u56de\u8868\u662f\u4ec0\u4e48<\/em><\/strong><\/p>\n\n\n\n<p>\u8f85\u52a9\u7d22\u5f15\u626b\u63cf\u4e4b\u540e\uff0c\u5f97\u5230ID\uff0c\u518d\u56de\u5230\u805a\u7c07\u7d22\u5f15\u67e5\u627e\u7684\u8fc7\u7a0b<\/p>\n\n\n\n<p><strong><em>2.\u56de\u8868\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898<\/em><\/strong><\/p>\n\n\n\n<p>IO\uff1a\u6b21\u6570\u548c\u91cf\u4f1a\u589e\u52a0\u3002<br>IOPS\uff1a1000\u6b21\/s,\u8fbe\u5230\u540e\u4f1a\u5f71\u54cd\u6027\u80fd<br>\u541e\u5410\u91cf\uff1a300M\/s,\u8fbe\u5230\u540e\u4f1a\u5f71\u54cd\u6027\u80fd<\/p>\n\n\n\n<p><strong><em>3.\u600e\u4e48\u51cf\u5c11\u56de\u8868<\/em><\/strong><\/p>\n\n\n\n<p>\u5efa\u7d22\u5f15\u4f7f\u7528\u8054\u5408\u7d22\u5f15\uff08\u8986\u76d6\uff09\uff0c\u5c3d\u53ef\u80fd\u5c06\u67e5\u8be2\u6761\u4ef6\u4e2d\u7684\u6570\u636e\u5305\u542b\u5728\u8054\u5408\u7d22\u5f15\u4e2d\u3002<br>\u7cbe\u7ec6\u5316\u67e5\u8be2\u6761\u4ef6\uff08\u4e1a\u52a1\u65b9\u9762\u4f7f\u7528&gt;and&lt;,limit\u51cf\u5c11\u67e5\u8be2\u8303\u56f4\uff09<br>\u67e5\u8be2\u6761\u4ef6\u8981\u7b26\u5408\u8054\u5408\u7d22\u5f15\u7684\u89c4\u5219\uff0c\u8986\u76d6\u7684\u5217\u8d8a\u591a\u8d8a\u597d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">9.\u7d22\u5f15\u81ea\u4f18\u5316AHI\uff08\u81ea\u9002\u5e94hash\u7d22\u5f15\uff09\\change buffer<\/h2>\n\n\n\n<p>AHI:\u7d22\u5f15\u7684\u7d22\u5f15\uff0c\u4e3a\u5185\u5b58\u4e2d\u7684\u70ed\u70b9\u7d22\u5f15\u9875\uff0c\u505a\u4e86\u4e00\u4e2aHASH\u7d22\u5f15\u8868\uff0c\u80fd\u591f\u5feb\u901f\u627e\u5230\u9700\u8981\u7684\u7d22\u5f15\u9875\u5730\u5740<br>change buffer\uff1a\u5bf9\u4e8e\u8f85\u52a9\u7d22\u5f15\u7684\u53d8\u5316\uff0c\u4e0d\u4f1a\u7acb\u5373\u66f4\u65b0\u5230\u7d22\u5f15\u4e2d\u3002\u6682\u5b58\u81f3change buffer\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">10.\u6267\u884c\u8ba1\u5212\u5206\u6790<\/h2>\n\n\n\n<p><strong><em>1.\u6267\u884c\u8ba1\u5212\u662f\u4ec0\u4e48<\/em><\/strong><\/p>\n\n\n\n<p>\u4f18\u5316\u5668\uff08\u7b97\u6cd5\uff09\u6700\u7ec8\u5f97\u51fa\u7684\uff0c\u4ee3\u4ef7\u6700\u4f4e\u7684\uff0cSQL\u8bed\u53e5\u7684\u6267\u884c\u65b9\u6848<\/p>\n\n\n\n<p><em><strong>2.\u4e3a\u4ec0\u4e48\u8981\u5206\u6790\u6267\u884c\u8ba1\u5212<\/strong><\/em><\/p>\n\n\n\n<p>\u573a\u666f\u4e00\uff1a\u5206\u6790\u6bd4\u8f83\u6162\u7684\u8bed\u53e5\u3002<br>\u573a\u666f\u4e8c\uff1a\u65b0\u4e0a\u7ebf\u4e1a\u52a1\uff0c\u53ef\u80fd\u4f1a\u5305\u542b\u5f88\u591aselect update delete\u2026,\u63d0\u524d\u53d1\u73b0\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong><em>3.\u5982\u4f55\u6293\u53d6\u6267\u884c\u8ba1\u5212<\/em><\/strong><\/p>\n\n\n\n<p><strong>a.\u6293\u53d6\u76ee\u6807<\/strong><br>select update delete<br><strong>b.\u65b9\u6cd5<\/strong><br>desc select * from world.city where countrycode='CHN';<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"100\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-94.png\" alt=\"\" class=\"wp-image-992\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-94.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-94-300x35.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-94-768x89.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><figcaption>\u4f7f\u7528desc\u53ef\u4ee5\u4f7fSQL\u8bed\u53e5\u505c\u6b62\u5728\u6267\u884c\u8ba1\u5212\u5206\u6790\u90a3\u4e00\u6b65<\/figcaption><\/figure>\n\n\n\n<p><strong><em>4.\u5982\u4f55\u5206\u6790\u6267\u884c\u8ba1\u5212<\/em><\/strong><\/p>\n\n\n\n<p>table\uff1a\u64cd\u4f5c\u7684\u8868<br>type\uff1a\u64cd\u4f5c \u7c7b\u578b\uff08\u5168\u8868\\\u7d22\u5f15\uff09\uff0cALL index range ref eq_ref const(system)<br>possible_key:\u6709\u53ef\u80fd\u7528\u7684\u7d22\u5f15<br>key:\u771f\u6b63\u8981\u7528\u662f\u54ea\u4e2a\u7d22\u5f15<br>key_len:\u7d22\u5f15\u8986\u76d6\u957f\u5ea6\uff08\u8054\u5408\u7d22\u5f15\uff09<br>rows\uff1a\u9884\u4f30\u9700\u8981\u626b\u63cf\u7684\u884c\u6570<br>Extra:using where using index using index condition using filesort sort using temp<\/p>\n\n\n\n<p><strong><em>5.\u6267\u884c\u8ba1\u5212type\u8be6\u8ff0<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a.ALL \u5168\u8868\u626b\u63cf<\/strong>\nmysql&gt; desc select * from world.city;\nmysql&gt; explain select * from world.city where countrycode!='chn';\nmysql&gt; explain select * from world.city where countrycode like '%ch';\nmysql&gt;explain select * from world.city where countrycode not in ('chn');\n\u4ee5\u4e0a\u8bed\u53e5\u5728\u751f\u4ea7\u4e2d\u5c3d\u91cf\u4e0d\u8981\u51fa\u73b0\n<strong>b.index \u5168\u7d22\u5f15\u626b\u63cf<\/strong>\n\u9700\u8981\u626b\u63cf\u6574\u9897\u7d22\u5f15\u6811\uff0c\u624d\u80fd\u5f97\u5230\u60f3\u8981\u7684\u7ed3\u679c\u3002\u6548\u7387\u4e5f\u6bd4\u8f83\u4f4e\nmysql&gt; explain select id ,countrycode from world.city;\n<strong>c.range\u7d22\u5f15\u8303\u56f4<\/strong>\n\u662f\u6211\u4eec\u5e94\u7528\u7d22\u5f15\u4f18\u5316\u7684\u5e95\u7ebf\uff0c\u4e5f\u662f\u5e94\u7528\u6700\u591a\u7684\nmysql&gt; explain select id from world.city where id &gt; 10;\nmysql&gt; explain select id from world.city where countrycode in ('chn','usa');\n<strong>d.ref \u8f85\u52a9\u7d22\u5f15\u7b49\u503c\u67e5\u8be2<\/strong>\nmysql&gt; desc select * from world.city where countrycode in ('chn') union all select * from world.city where countrycode in ('usa');\n\u7406\u8bba\u4e0aref\u6bd4range\u597d\uff0c\u4f46\u662f\u5b9e\u9645\u6548\u679c\u5dee\u522b\u4e0d\u662f\u592a\u5927\n\u4e3b\u8981\u770b\u7d22\u5f15\u5217\u7684\u91cd\u590d\u503c\u591a\u5c11\uff0c\u57fa\u6570\u5c11\uff08\u91cd\u590d\u503c\u591a\uff09\u4e0d\u5efa\u8bae\u5c06in\u4fee\u6539\u4e3aunion all\uff0c\u52a0\u4e0a\u538b\u6d4b\u7ed3\u679c\uff0c\u6700\u7ec8\u8bc4\u4f30\u662f\u5426\u9700\u8981\u4fee\u6539union all\n<strong>e.eq_ref:\u975e\u9a71\u52a8\u8868\uff0c\u8fde\u63a5\u6761\u4ef6\u662f\u4e3b\u952e\u6216\u552f\u4e00\u952e<\/strong>\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"107\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-95.png\" alt=\"\" class=\"wp-image-994\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-95.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-95-300x37.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-95-768x95.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; alter table world.city add index idx_p(population);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"84\" src=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-96.png\" alt=\"\" class=\"wp-image-995\" srcset=\"https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-96.png 865w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-96-300x29.png 300w, https:\/\/www.buyao007.icu\/wp-content\/uploads\/2022\/03\/image-96-768x75.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\u52a0\u5b8c\u7d22\u5f15\u540e\u53ef\u4ee5\u770b\u5230type\u5df2\u7ecf\u53d8\u6210\u4e86range\uff0c\u662f\u6700\u4f4e\u53ef\u63a5\u53d7\u7684\u7c7b\u578b\n###\u8865\u5145\uff1a\n\u591a\u8868\u8fde\u63a5\u65f6\uff0c\u5c0f\u7ed3\u679c\u96c6\u7684\u8868\u9a71\u52a8\u5927\u8868\uff0c\n\u4f18\u5316\u4f1a\u81ea\u52a8\u5224\u65ad\u67e5\u8be2\u8bed\u53e5\u4e2d\u7684\u8c01\u4f5c\u4e3a\u9a71\u52a8\u8868\u66f4\u5408\u9002\uff0c\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u9009\u62e9\u9519\u8bef\n\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7left join \u5f3a\u5236\u9a71\u52a8\u8868\u5e72\u9884\u6267\u884c\u8ba1\u5212\u3002\n####\u5982\u4f55\u5224\u65ad\u4e00\u6761\u8bed\u53e5\u4e2d\uff0c\u8c01\u662f\u9a71\u52a8\u8868\n#1\u4f18\u5316\u5668\u5224\u65ad\uff1a\u4f7f\u7528explain\u6216\u8005desc\n#2\u4eba\u4e3a\u5224\u65ad\uff0c\u7edf\u8ba1\u7ed3\u679c\u884c\u6570\n#3\u538b\u6d4b\nf.const(system)\n\u6548\u7387\u6700\u9ad8\u51e0\u4e4e\u770b\u4e0d\u5230\uff0c\u4f7f\u7528\u4e3b\u952e\u7d22\u5f15\u505a\u7b49\u503c\u67e5\u627e\nmysql&gt; explain select * from world.city where id =1;\n<\/code><\/pre>\n\n\n\n<p><strong><em>6.key_len\u8be6\u89e3<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>a.\u4ecb\u7ecd<\/strong>\n\uff08\u8054\u5408\uff09\u7d22\u5f15\u8986\u76d6\u957f\u5ea6\nidx(a,b,c)---&gt;\u5047\u8bbea(10),b(20),c(30)\nexplain 10 \u8bf4\u660e\u53ea\u7528\u4e86a\u5217\u7684\u7d22\u5f15\uff0c\u5982\u679c\u662f60\u5c31\u8bf4\u660e3\u5217\u7684\u7d22\u5f15\u90fd\u7528\u5230\u4e86\n<strong>b.\u5982\u4f55\u8ba1\u7b97\u7d22\u5f15\u5217\u7684key_len<\/strong>\nkey_len\u548c\u6bcf\u4e2a\u5217\u7684\u6700\u5927\u9884\u7559\u957f\u5ea6\u6709\u5173.\n\u6570\u636e\u7c7b\u578b \u5b57\u7b26\u96c6utf8mb4 \u6ca1not null\ntinyint      1             1\nint          4             1\nchar\uff0810\uff09   4*10          1\nvarchar\uff0810\uff094*10+2        1\n<strong>c\uff0e\u8054\u5408\u7d22\u5f15\u5e94\u7528\u7ec6\u8282<\/strong>\nidx(a,b,c)\n\u5b8c\u5168\u8986\u76d6\uff1a\n\t\ta= and b= and c=\n\t\ta= and c= and b= \u7b49\u503c\u987a\u5e8f\u6253\u4e71\u7684\n\t\ta= and b= and c\u8303\u56f4\n\u90e8\u5206\u8986\u76d6\uff1a\n\t\ta= and b= \u8d70ab\u7d22\u5f15\n\t\ta=   \u8d70a\u7d22\u5f15\n\t\ta= and c= \u8d70a\u7d22\u5f15\n\t\ta= and b\u8303\u56f4 and c= \u53ea\u80fd\u8d70\u5230b\u8fd9\u4e2a\u7d22\u5f15\n\t\ta\u8303\u56f4 and b= and c= \u53ea\u80fd\u8d70\u5230a\u8fd9\u4e2a\u7d22\u5f15\n\u5b8c\u5168\u4e0d\u8986\u76d6\nb\nc\nbc\ncb\u90fd\u4e0d\u8d70\u7d22\u5f15\uff0c\u56e0\u4e3a\u4ece\u5de6\u5230\u53f3\u7684\u539f\u5219\n\u4f18\u5316\u6848\u4f8b\uff1a\nidx(k1,num,k2)\nmysql&gt;desc select * from t100w where k1=\u2019Vs\u2019 and k2=\u2019mnij\u2019 and num &lt;27779\n\u4f18\u5316\u65b9\u5f0f\uff1a\u4fee\u6539\u7d22\u5f15\u4e3aidx(k1,k2,num)\n<\/code><\/pre>\n\n\n\n<p><strong><em>7.extra\u8be6\u89e3<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>Using index<\/strong> \u6b63\u5e38\u72b6\u6001\uff0c\u6700\u4f18\u8d28\u7684\uff0c\u4f7f\u7528\u4e86\u7d22\u5f15\u8986\u76d6\u626b\u63cf\n   <strong>Using where<\/strong> \u4f7f\u7528where\u56de\u8868\u626b\u63cf\u6570\u636e\u884c\uff0c\u8bf4\u660e\u76ee\u6807\u8868\u7684\u7d22\u5f15\u6ca1\u6709\u8bbe\u8ba1\u597d\n\u89e3\u51b3\u529e\u6cd5\uff1a\na.table---\u300b\u83b7\u53d6\u5230\u51fa\u95ee\u9898\u7684\u8868\nb.\u770b\u539f\u59cb\u67e5\u8be2\u8bed\u53e5\u4e2d\u7684\u6761\u4ef6\u5217\nc.\u67e5\u8be2\u5217\u7684\u7d22\u5f15\u60c5\u51b5\nd.\u6309\u9700\u4f18\u5316\u7d22\u5f15\n\n   <strong>Using index condition<\/strong>\n\n<strong>Using filesort<\/strong>###\u4f7f\u7528\u4e86\u989d\u5916\u7684\u6392\u5e8f\uff08\u6ca1\u6709\u7d22\u5f15\uff09\na.table---\u300b\u83b7\u53d6\u5230\u51fa\u95ee\u9898\u7684\u8868\nb.\u770b\u539f\u59cb\u67e5\u8be2\u8bed\u53e5\u4e2dorder by group by distinct\nc.\u67e5\u8be2\u5217\u7684\u7d22\u5f15\u60c5\u51b5\nd.\u6309\u9700\u4f18\u5316\u7d22\u5f15\n\u67e5\u770b\u5197\u4f59\u7d22\u5f15\nmysql&gt; select table_schema,table_name ,redundant_index_name,redundant_index_columns from sys.schema_redundant_indexes;\n\n   <strong>Using temp<\/strong>\na \u6761\u4ef6\u8303\u56f4\u662f\u4e0d\u662f\u8fc7\u5927\nb.having order by \u989d\u5916\u6392\u5e8f\nc.\u5b50\u67e5\u8be2\n\u5927\u51e0\u7387\u5f00\u53d1\u9700\u8981\u6539\u5199\u8bed\u53e5\u4e86\n<\/code><\/pre>\n\n\n\n<p><strong><em>PS:\u5173\u4e8e\u7d22\u5f15\u7684\u4f18\u5316\u5668\u7b97\u6cd5\uff1aICP\\MRR<\/em><\/strong><\/p>\n\n\n\n<p>ICP \uff1aindex condition pushdown<\/p>\n\n\n\n<p>\u4f18\u5316\u5668\u7b97\u6cd5\uff1a<br>a.\u67e5\u8be2\u4f18\u5316\u5668\u7b97\u6cd5<br>select @@optimizer_switch;<br>\u4fee\u6539\u7b97\u6cd5\uff08\u4e0d\u5efa\u8bae\u4fee\u6539\uff09<br>set global optimizer_switch=\u2019xxx\u7b97\u6cd5\uff08\u4ece\u4e0a\u4e00\u4e2a\u8bed\u53e5\u83b7\u53d6\uff09=off\u2019<br>ICP\u5168\u79f0\uff1aindex_condition_pushdown<\/p>\n\n\n\n<p>MRR<\/p>\n\n\n\n<p>\u53c2\u8003\u5b98\u7f51\uff1a<br>https\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/mrr-optimization.html<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">11.\u7d22\u5f15\u5e94\u7528\u89c4\u8303<\/h2>\n\n\n\n<p><strong><em>1.\u5efa\u7acb\u7d22\u5f15\u7684\u539f\u5219\uff08DBA\u8fd0\u7ef4\u89c4\u8303\uff09<\/em><\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u5fc5\u987b\u8981\u6709\u4e3b\u952e\uff0c\u65e0\u5173\u5217\u3002<br>\uff082\uff09\u7ecf\u5e38\u505a\u4e3awhere\u6761\u4ef6\u5217 order by group by join on \uff0cdistinct\u7684\u6761\u4ef6\uff08\u4e1a\u52a1\uff1a\u4ea7\u54c1\u529f\u80fd+\u7528\u6237\u884c\u4e3a\uff09<br>\uff083\uff09\u6700\u597d\u4f7f\u7528\u552f\u4e00\u503c\u591a\u7684\u5217\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5982\u679c\u7d22\u5f15\u5217\u91cd\u590d\u503c\u8f83\u591a\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8054\u5408\u7d22\u5f15<br>\uff084\uff09\u5217\u503c\u957f\u5ea6\u8f83\u957f\u7684\u7d22\u5f15\u5217\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u524d\u7f00\u7d22\u5f15###\u8ba1\u7b97\u57fa\u6570\u7684\u547d\u4ee4mysql> select count(distinct left(name,19)) from world.city;<br>\uff085\uff09\u964d\u4f4e\u7d22\u5f15\u6761\u76ee\uff0c\u4e00\u65b9\u9762\u4e0d\u8981\u521b\u5efa\u6ca1\u7528\u7d22\u5f15\uff0c\u4e0d\u5e38\u4f7f\u7528\u7684\u7d22\u5f15\u6e05\u7406<br>\uff086\uff09\u7d22\u5f15\u7ef4\u62a4\u8981\u907f\u5f00\u4e1a\u52a1\u7e41\u5fd9\u671f\uff0c\u5efa\u8bae\u7528pt-osc<br>\uff087\uff09\u8054\u5408\u7d22\u5f15\u6700\u5de6\u539f\u5219<\/p>\n\n\n\n<p><strong><em>2.\u4e0d\u8d70\u7d22\u5f15\u7684\u60c5\u51b5\uff08\u5f00\u53d1\u89c4\u8303\uff09<\/em><\/strong><\/p>\n\n\n\n<p>1\u6ca1\u6709\u67e5\u8be2\u6761\u4ef6\uff0c\u6216\u8005\u67e5\u8be2\u6761\u4ef6\u6ca1\u6709\u5efa\u7acb\u7d22\u5f15<br>select * from t1\uff1b<br>select * from t1 where id=1001 or 1=1\uff1b<br>2\u67e5\u8be2\u7ed3\u679c\u96c6\u662f\u539f\u8868\u4e2d\u7684\u5927\u90e8\u5206\u6570\u636e\uff0c\u5e94\u8be5\u662f15-25%\u4ee5\u4e0a\u3002<br>\u67e5\u8be2\u7684\u7ed3\u679c\u96c6\uff0c\u8d85\u8fc7\u4e86\u603b\u6570\u884c\u657025%\uff0c\u4f18\u5316\u5668\u89c9\u5f97\u5c31\u6ca1\u5fc5\u8981\u8d70\u7d22\u5f15\u4e86\u3002<br>MySQL\u7684\u9884\u8bfb\u529f\u80fd\u6709\u5173\u3002<br>\u53ef\u4ee5\u901a\u8fc7\u7cbe\u786e\u67e5\u627e\u8303\u56f4\uff0c\u8fbe\u5230\u4f18\u5316\u7684\u6548\u679climit \u6216\u8005\u8303\u56f4\u3002<br>1000000 &gt;500000 and<br>3\u7d22\u5f15\u672c\u8eab\u5931\u6548\uff0c\u7edf\u8ba1\u4fe1\u606f\u4e0d\u771f\u5b9e\uff08\u8fc7\u65e7\uff09<br>\u7d22\u5f15\u6709\u81ea\u6211\u7ef4\u62a4\u7684\u80fd\u529b<br>\u5bf9\u4e8e\u8868\u5185\u5bb9\u53d8\u5316\u6bd4\u8f83\u9891\u7e41\u7684\u60c5\u51b5\u4e0b\uff0c\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u7d22\u5f15\u5931\u6548<br>\u4e00\u822c\u662f\u5220\u9664\u91cd\u5efa<br>\u73b0\u8c61\uff1a<br>\u6709\u4e00\u6761select\u8bed\u53e5\u5e73\u5e38\u67e5\u8be2\u65f6\u5f88\u5feb\uff0c\u7a81\u7136\u6709\u4e00\u5929\u5f88\u6162\uff0c\u4f1a\u662f\u4ec0\u4e48\u539f\u56e0<br>select\uff1f ---\u300b\u7d22\u5f15\u5931\u6548\uff0c\u7edf\u8ba1\u6570\u636e\u4e0d\u771f\u5b9e<br>Innodb_index_stats<br>Innodb_table_stats<br>Mysql&gt;Analyze table world.city;<br>4\u67e5\u8be2\u6761\u4ef6\u4f7f\u7528\u51fd\u6570\u5728\u7d22\u5f15\u5217\u4e0a\uff0c\u6216\u8005\u5bf9\u7d22\u5f15\u5217\u8fdb\u884c\u8fd0\u7b97\uff0c\u8fd0\u7b97\u5305\u62ec\uff08+\uff0c-\uff0c*\uff0c\/\uff0c\uff01\u7b49\uff09<br>\u4f8b\u5b50\uff1a<br>\u9519\u8bef\u7684\u4f8b\u5b50\uff1aselect * from test where id-1=9 ###\u6709\u8fd0\u7b97<br>\u6b63\u786e\u7684\u5199\u6cd5\uff1aselect * from test where id=10<br>5\u9690\u5f0f\u81f3\u8f6c\u6362\u5bfc\u81f4\u7d22\u5f15\u5931\u6548\u3002\u8fd9\u4e00\u70b9\u5e94\u5f53\u5f15\u8d77\u91cd\u89c6\uff0c\u4e5f\u662f\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u72af\u7684\u9519\u8bef<br>desc select * from b where telnum=110;<br>desc select * from b where telnum=\u2019110\u2019;<br>\u56e0\u4e3atelnum\u5217\u4f7f\u7528\u7684\u662f\u5b57\u7b26\u4e32\u5217\uff0c110\u4f1a\u5148\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5728\u8fdb\u884c\u67e5\u627e<br>6&lt;&gt;,not in \u4e0d\u8d70\u7d22\u5f15\uff08\u8f85\u52a9\u7d22\u5f15\uff09<br>7like \u201c%_\u201d\u767e\u5206\u53f7\u5728\u524d\u9762\u4e0d\u8d70<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/962"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=962"}],"version-history":[{"count":20,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/962\/revisions"}],"predecessor-version":[{"id":1010,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/962\/revisions\/1010"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}