{"id":4117,"date":"2012-12-10T14:49:20","date_gmt":"2012-12-10T06:49:20","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=4117"},"modified":"2013-05-13T17:04:56","modified_gmt":"2013-05-13T09:04:56","slug":"mysql-source-code-optimizer-range-and-ref","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2012\/12\/mysql-source-code-optimizer-range-and-ref\/","title":{"rendered":"MySQL\u6e90\u7801:Range\u548cRef\u4f18\u5316\u7684\u6210\u672c\u8bc4\u4f30"},"content":{"rendered":"<p>\u5728\u5f00\u59cb\u4ecb\u7ecdindex merge\/ROR\u4f18\u5316\u4e4b\u524d\uff0c\u6253\u7b97\u5148\u4ecb\u7ecdMySQL\u662f\u5982\u4f55\u5bf9range\/ref\u505a\u6210\u672c\u8bc4\u4f30\u7684\u3002MySQL\u662f\u57fa\u4e8e\u6210\u672c(cost)\u6a21\u578b\u9009\u62e9\u6267\u884c\u8ba1\u5212\uff0c\u5728\u591a\u4e2arange\uff0c\u5168\u8868\u626b\u63cf\uff0cref\u4e4b\u95f4\u4f1a\u9009\u62e9\u6210\u672c\u6700\u5c0f\u7684\u4f5c\u4e3a\u6700\u7ec8\u7684\u6267\u884c\u8ba1\u5212\u3002\u4ecd\u7136\u5f3a\u70c8\u5efa\u8bae\u5148\u9605\u8bfb\u767b\u535a\u7684slide\uff1a\u300a<a href=\"http:\/\/hedengcheng.com\/?p=372\" target=\"_blank\">\u67e5\u8be2\u4f18\u5316\u6d45\u6790<\/a>\u300b\uff0c\u6587\u4e2d\u8f83\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecdMySQL\u5728range\u4f18\u5316\u65f6\u6210\u672c\u7684\u8ba1\u7b97\u3002<\/p>\n<p>\u672c\u6587\u5c06\u7ee7\u7eed\u4ecb\u7ecdrange\/ref\u6267\u884c\u8ba1\u5212\u9009\u62e9\u7684\u4e00\u4e9b\u4e0d\u5bb9\u5ffd\u7565\u7684\u7ec6\u8282\u3002\u5e0c\u671b\u770b\u5ba2\u80fd\u591f\u901a\u8fc7\u6b64\u6587\u80fd\u591f\u4e86\u89e3\u66f4\u591a\u7ec6\u8282\u3002<!--more--><\/p>\n\n<h3>0. \u6210\u672c\u8ba1\u7b97\u7684\u603b\u539f\u5219<\/h3>\n<p>MySQL\u7684\u4e00\u4e2a\u6267\u884c\u8ba1\u5212\uff0c\u6709\u4e24\u90e8\u5206\u6210\u672c\uff0cCPU\u6210\u672c(CPU COST)\u548cIO\u6210\u672c(IO COST)\u3002CPU COST\u662f\u6307\u67e5\u8be2\u51fa\u7eaa\u5f55\u540e\uff0c\u9700\u8981\u505a\u8fc7\u6ee4\u7b49\u5904\u7406\u7684\u65f6\u5019\u7684CPU\u6d88\u8017\uff0cIO COST\u662f\u6307\uff0c\u4ece\u5b58\u50a8\u5f15\u64ce\u8bfb\u53d6\u6570\u636e\u65f6\u9700\u8981\u505a\u7684IO\u6d88\u8017\u3002<\/p>\n<p>\u603b\u6210\u672c \uff1d CPU COST + IO COST<\/p>\n<p>\u8865\u5145\u8bf4\u660e\uff1a(1) IO\u6210\u672c\u8ba1\u7b97\u4e0d\u8003\u8651\u7f13\u5b58\u7684\u5f71\u54cd\u3002\u56e0\u4e3a\u5728\u4f18\u5316\u5668\u672c\u8eab\u662f\u65e0\u6cd5\u9884\u77e5\u9700\u8981\u7684\u6570\u636e\u5230\u5e95\u5728\u5185\u5b58\u4e2d\u8fd8\u662f\u78c1\u76d8\u4e0a\u3002<\/p>\n<h3>1. range\u6210\u672c\u7684\u8ba1\u7b97\u4e0e\u5206\u6790<\/h3>\n<p>MySQL\u4f7f\u7528\u4e00\u9897SEL_ARG\u7684\u6811\u5f62\u7ed3\u6784\u63cf\u8ff0\u4e86WHERE\u6761\u4ef6\u4e2d\u7684range\uff0c\u5982\u679c\u6709\u591a\u4e2arange\uff0c\u5219\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u5f0f\u904d\u5386SEL_ARG\u7ed3\u6784\uff0c\u5728<a href=\"http:\/\/www.orczhou.com\/index.php\/2012\/11\/mysql-source-code-range-optimize-data-structure\/#3_RANGE\" target=\"_blank\">\u524d\u9762<\/a>\u8be6\u7ec6\u7684\u4ecb\u7ecdrange\u7684\u7ea2\u9ed1\u6811\u7ed3\u6784\uff0c\u4ee5\u53caMySQL\u5982\u4f55\u904d\u5386\u4e4b\u3002<\/p>\n<p>\u63a5<a href=\"http:\/\/www.orczhou.com\/index.php\/2012\/11\/mysql-source-code-range-optimize-data-structure\/#3_RANGE\" target=\"_blank\">\u4e0a\u6587<\/a>\uff0c\u8fd9\u91cc\u5c06\u770b\u770b\uff0c\u904d\u5386\u5230\u6700\u540e\uff0cMySQL\u5982\u4f55\u8ba1\u7b97\u4e00\u4e2a\u7b80\u5355range\u7684\u6210\u672c\u3002<\/p>\n<h4>1.1 range\u8fd4\u56de\u7684\u8bb0\u5f55\u6570<\/h4>\n<p>MySQL\u9996\u5148\u8ba1\u7b97range\u9700\u8981\u8fd4\u56de\u90fd\u5c11\u7eaa\u5f55\uff0c\u901a\u8fc7\u51fd\u6570check_quick_select\u8fd4\u56de\u5bf9\u67d0\u4e2a\u7d22\u5f15\u505arange\u67e5\u8be2\u5927\u7ea6\u547d\u4e2d\u591a\u5c11\u6761\u7eaa\u5f55\u3002<\/p>\n<blockquote><p>found_records= check_quick_select(param, idx, *key, update_tbl_stats);<\/p><\/blockquote>\n<h4>1.2 CPU COST<\/h4>\n<blockquote><p>#define TIME_FOR_COMPARE   5    \/\/ 5 compares == one read<br \/>\ndouble cpu_cost= (double) found_records \/ TIME_FOR_COMPARE;<\/p><\/blockquote>\n<h4>1.3 IO COST<\/h4>\n<p>\u5bf9\u4e8eInnoDB\u7684\u4e8c\u7ea7\u7d22\u5f15\uff0c\u4e14\u4e0d\u662f\u8986\u76d6\u626b\u63cf\uff1a<\/p>\n<blockquote><p>found_read_time := number of ranges + found_records<\/p><\/blockquote>\n<p>\u8fd9\u91cc\uff0cfound_records\u662f\u4e3b\u8981\u90e8\u5206\uff0cnumber of ranges\u8868\u793a\u4e00\u5171\u6709\u591a\u5c11\u4e2arange\uff0c\u8fd9\u662f\u4e00\u4e2a\u4fee\u6b63\u503c\uff0c\u8868\u793aIO COST\u4e0d\u5c0f\u4e8erange\u7684\u4e2a\u6570\u3002<\/p>\n<h4>1.4 \u5168\u8868\u626b\u63cf\u7684\u6210\u672c<\/h4>\n<p>\u5177\u4f53\u7684\uff0c\u5bf9\u4e8eInnoDB\u8868\uff0c\u6211\u4eec\u6765\u770b\uff1a<\/p>\n<blockquote><p>read_time= number of total page + \uff08records \/ TIME_FOR_COMPARE + 1\uff09 + 1.1;<\/p><\/blockquote>\n<p>\u5bf9\u4e8eInnoDB\u53d6\u503c\u4e3a\uff1a\u4e3b\u952e\u7d22\u5f15(\u6570\u636e)\u6240\u4f7f\u7528\u7684page\u6570\u91cf(stat_clustered_index_size)<\/p>\n<p>\u5bf9\u4e8eMyISAM\u53d6\u503c\u4e3a\uff1astats.data_file_length\/IO_SIZE + file->tables<\/p>\n<h4>1.5 \u5173\u4e8erange\u6267\u884c\u8ba1\u5212\u7684\u5206\u6790<\/h4>\n<p>\u8fd9\u91cc\u6765\u770b\u770b\uff0crange\u7684\u9009\u62e9\u5ea6(selectivty)\u5927\u6982\u4e3a\u591a\u5c11\u7684\u65f6\u5019\uff0c\u4f1a\u653e\u5f03range\u4f18\u5316\uff0c\u800c\u9009\u62e9\u5168\u8868\u626b\u63cf\u3002\u4e0b\u9762\u65f6\u4e00\u4e2a\u5b9a\u91cf\u7684\u5206\u6790\uff1a<\/p>\n<p><!-- <a href=\"http:\/\/www.flickr.com\/photos\/26825745@N06\/8252248350\/\" title=\"screenshot-1 by orczhou, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm9.staticflickr.com\/8344\/8252248350_8d097a4e08.jpg\" width=\"400\" height=\"290\" alt=\"screenshot-1\"><\/a>--><\/p>\n\n<p>(1) \u5047\u8bbe\u603b\u8bb0\u5f55\u6570\u4e3aR\uff1brange\u9700\u8981\u8fd4\u56de\u7684\u7eaa\u5f55\u6570\u4e3ar<\/p>\n<p>(2) \u5047\u8bbe\u8be5\u8868\u7684\u603b\u9875\u9762\u6570(IO COST)\u4e3aP\uff1b\u5355\u4e2a\u9875\u9762\u7eaa\u5f55\u6570\u4e3ac<\/p>\n<p>\\[r+1\\frac{r}{5} > P + \\frac{R}{5} + 1 + 1.1 \\]<\/p>\n<p>\\[ \\frac{r}{R} > \\frac{1}{6} + \\frac{5}{6} * \\frac{P}{R} + \\frac{5.5}{6*R}  \\]<\/p>\n<p>\\[ \\frac{r}{R} > \\frac{1}{6} + \\frac{5}{6} * \\frac{1}{c} \\frac{5.5}{6*R} \\]<\/p>\n<p>\u5728\u6211\u7684\u6d4b\u8bd5\u6848\u4f8b\u4e2d\uff0cP=4\uff0cR=1016 \uff0c\u6709<\/p>\n<p>\\[ \\frac{r}{R} > 0.171 \\] <\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u5982\u679c\u9009\u62e9\u5ea6(selectivity)\u9ad8\u4e8e17.1%\u5c31\u4f1a\u653e\u5f03range\u4f18\u5316\uff0c\u800c\u8d70\u5168\u8868\u626b\u63cf\u3002\u8fd9\u91cc\u7eaa\u5f55\u6570\u8d85\u8fc71016*0.171\uff1d173\u65f6\u5c06\u653e\u5f03range\u4f18\u5316\u3002<\/p>\n<h4>1.6 \u9a8c\u8bc1<\/h4>\n<p>MySQL\u901a\u8fc7\u51fd\u6570check_quick_select\u8fd4\u56derange\u53ef\u80fd\u626b\u63cf\u7684\u8bb0\u5f55\u6570\uff0c\u6240\u4ee5\uff0c\u8fd9\u91cc\u901a\u8fc7\u5bf9\u8be5\u51fd\u6570\u8bbe\u7f6e\u65ad\u70b9\uff0c\u5e76\u624b\u52a8\u8bbe\u7f6e\u8fd4\u56de\u503c\uff0c\u901a\u8fc7\u6b64\u6765\u9a8c\u8bc1\u4e0a\u9762\u5bf9selectivity\u7684\u8ba1\u7b97\uff0c\u8be6\u7ec6\u5730\uff1a<\/p>\n<pre><blockquote>(gdb) p head->file->stats.records\r\n$1 = 1016\r\n(gdb) p head->file->scan_time()\r\n$3 = 4\r\n(gdb) p 1016*(1.0\/6+(5.0\/6)*(4.0\/1016)+5.5\/(6*1016))\r\n$43 = 173.58333333333329\r\n\r\n(gdb) b check_quick_select\r\nBreakpoint 5 at 0x679377: file opt_range.cc, line 7436.\r\n(gdb) c\r\nContinuing.\r\n\r\n\u9047\u5230\u65ad\u70b9\uff1a\r\n(gdb) return 173\r\n\u770b\u5230\uff1a\r\nroot@test 05:07:52>explain select * from users where reg_date >= '2012-09-20 12:00:00';\r\n+----+-------------+-------+-------+---------------+-------------+---------+------+------+-------------+\r\n| id | select_type | table | type  | possible_keys | key         | key_len | ref  | rows | Extra       |\r\n+----+-------------+-------+-------+---------------+-------------+---------+------+------+-------------+\r\n|  1 | SIMPLE      | users | range | ind_regdate   | ind_regdate | 9       | NULL |  173 | Using where |\r\n+----+-------------+-------+-------+---------------+-------------+---------+------+------+-------------+\r\n\r\n(gdb) return 174\r\n\u770b\u5230\r\nroot@test 05:08:05>explain select * from users where reg_date >= '2012-09-20 12:00:00';\r\n+----+-------------+-------+------+---------------+------+---------+------+------+-------------+\r\n| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |\r\n+----+-------------+-------+------+---------------+------+---------+------+------+-------------+\r\n|  1 | SIMPLE      | users | ALL  | ind_regdate   | NULL | NULL    | NULL | 1016 | Using where |\r\n+----+-------------+-------+------+---------------+------+---------+------+------+-------------+\r\n<\/blockquote><\/pre>\n<p>\u4e0a\u9762\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679crange\u547d\u4e2d\u7684\u8bb0\u5f55\u6570\u8d85\u8fc7173\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u653e\u5f03range\uff0c\u9009\u62e9\u5168\u8868\u626b\u63cf\u3002<\/p>\n<h4>1.7 \u4e00\u4e9b\u9650\u5236<\/h4>\n<p>(1) \u65e0\u8bba\u65f6InnoDB\u8fd8\u662fMyISAM\u7684scan_time\uff0crange\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u90fd\u4e0d\u662f\u7cbe\u786e\u503c\uff0c\u800c\u4e14\u5bf9\u4e8eInnoDB\uff0c\u603b\u8bb0\u5f55\u6570\u4e5f\u4e0d\u662f\u7cbe\u786e\u503c\uff0c\u6240\u4ee5\u4e0a\u9762\u53ea\u662f\u4e00\u4e2aHigh level\u7684\u9884\u4f30\u3002<\/p>\n<p>(2) \u4e0a\u9762\u6848\u4f8b\u4e2d\uff0c\u6761\u7eaa\u5f55\u5f88\u77ed\uff0c\u6240\u4ee5\u770b\u5230\u603bpage\u5f88\u5c11\uff0c\u5b9e\u9645\u60c5\u51b5\uff0c\u5355\u6761\u7eaa\u5f55\u66f4\u5927\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u7684\u5355\u4e2a\u9875\u9762\u7eaa\u5f55\u6570\u4e3ac\u66f4\u5c0f\uff0c\u6240\u4ee5\u901a\u5e38\u9009\u62e9\u5ea6\u66f4\u9ad8\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u9009\u62e9\u5168\u8868\u626b\u63cf\u3002<\/p>\n<h3>2. ref\u6210\u672c\u7684\u8ba1\u7b97\u4e0e\u5206\u6790<\/h3>\n<h4>2.1 ref\u8fd4\u56de\u7684\u8bb0\u5f55\u6570<\/h4>\n<p>ref\u4f18\u5316\u7684\u65f6\u5019\uff0c\u8ba1\u7b97\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u4ece\u4ee3\u7801\u4e0a\u6765\u770b\u8981\u590d\u6742\u5f88\u591a\uff0c\u4f46\u662f\u601d\u60f3\u5f88\u7b80\u5355\u3002<\/p>\n<p>\u601d\u8def\uff1a\u5728range\u4f18\u5316\u9636\u6bb5\uff0c\u4efb\u4f55\u7b49\u503c\u90fd\u4f1a\u5f53\u4f5c\u8303\u56f4\u6761\u4ef6\uff08<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/range-access-single-part.html\" target=\"_blank\">\u53c2\u80031<\/a>\uff0c<a href=\"http:\/\/www.orczhou.com\/index.php\/2012\/11\/mysql-source-code-range-optimize-data-structure\/\" target=\"_blank\">\u53c2\u80032<\/a>\uff09\u3002<\/p>\n<p>\u5bf9\u4e8ekp1 = const and kp2 = const\u8fd9\u7c7bref\uff0cMySQL\u5c06\u76f4\u63a5\u4f7f\u7528range\u4f18\u5316\u65f6\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u901a\u8fc7\u5b58\u50a8\u5f15\u64ce\u63a5\u53e3records_in_range\u8fd4\u56de\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u7c7b\u8f83\u4e3a\u7279\u6b8a\u7684ref\uff0ckp1 = const and kp2 > const\uff0c\u5bf9\u4e8e\u6b64\u7c7bref\uff0crange\u4f18\u5316\u7684\u65f6\u5019\uff0c\u4f1a\u4f7f\u7528\u4e24\u4e2a\u7d22\u5f15\u5217\uff0c\u4f46\u662fref\u53ea\u80fd\u7528\u4e00\u4e2a\u7d22\u5f15\u5217\u3002\u8fd9\u65f6\uff0cref\u9996\u5148\u6839\u636e\u7d22\u5f15\u7edf\u8ba1\u4fe1\u606f(show index from users\u4e2dCardinality\u7684\u503c)\u9884\u4f30\u3002\u56e0\u4e3a\u8fd9\u91cc\u6709range\u4f18\u5316\u7684\u503c\uff0c\u8fd8\u4f1a\u505a\u4e00\u6b21\u4fee\u6b63\uff0c\u56e0\u4e3arange\u4f7f\u7528\u4e86\u66f4\u591a\u7684\u7d22\u5f15\u5b57\u6bb5\u3002\u4fee\u6b63\u903b\u8f91\u4e3a\uff1a\u5982\u679c\u53d1\u73b0\u7d22\u5f15\u7edf\u8ba1\u4fe1\u606f\u592a\u8fc7\u4fdd\u5b88(\u4f8b\u5982\u6570\u636e\u5206\u5e03\u4e0d\u5747\u5300\u65f6\uff0c\u9047\u5230\u4e00\u4e2a\u70ed\u70b9)\uff0c\u8fd9\u65f6\u4f1a\u7528range\u4f18\u5316\u7684\u503c\u4fee\u6b63\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u8fd4\u56de\u7684\u7eaa\u5f55\u6570\uff0c\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u83b7\u53d6\uff1a<\/p>\n<blockquote><p>records= keyinfo->rec_per_key[max_key_part-1]<br \/>\nif(records < (double)table->quick_rows[key]&#8230;)<br \/>\n  records= (double)table->quick_rows[key];\n<\/p><\/blockquote>\n<h4>2.2 CPU COST<\/h4>\n<blockquote><p>CPU COST := records\/(double) TIME_FOR_COMPARE;<\/p><\/blockquote>\n<h4>2.3 IO COST<\/h4>\n<p>ref\u5728\u505aIO\u6210\u672c\u8bc4\u4f30\u7684\u65f6\u5019\uff0c\u57fa\u672c\u540crange\u76f8\u540c\uff0cref\u547d\u4e2d\u591a\u5c11\u7eaa\u5f55\u5219\u9700\u8981\u591a\u5c11\u4e2aIO COST\u3002\u4f46\u662f\u8ddfrange\u4f18\u5316\u6253\u4e0d\u540c\u7684\u662f\uff0c\u8fd9\u91cc\u505a\u4e86\u4e00\u4e2a\u4fee\u6b63(range\u4f18\u5316\u5e76\u6ca1\u6709\u505a)\uff0c\u4e5f\u662fIO COST\u6700\u574f\u4e0d\u4f1a\u8d85\u8fc7\u5168\u8868\u626b\u63cfIO\u6d88\u8017\u76843\u500d(\u6216\u8005\u603b\u8bb0\u5f55\u6570\u9664\u4ee510)\uff0c\u6709\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<blockquote><p>\ns->worst_seeks= min((double) s->found_records \/ 10,<br \/>\n                        (double) s->read_time*3);<br \/>\nIO COST := record_count*min(tmp,s->worst_seeks);<\/p><\/blockquote>\n<p>\u8fd9\u91ccrecord_count\u662f\u524d\u4e00\u6b21\u5173\u8054\u540e\u7684\u8bb0\u5f55\u6570\u3002tmp\u662f\u5f53\u524dref\u547d\u4e2d\u7684\u8bb0\u5f55\u6570\u3002\u8fd9\u4e2a\u4fee\u6b63\u7684\u903b\u8f91\u662f\u5f88\u597d\u7406\u89e3\u7684\uff1a\u5373\u4f7f\u52a0\u4e0a\u7d22\u5f15\u626b\u63cf\u5176io cost\u4ecd\u7136\u662f\u6709\u9650\u5ea6\u7684\u3002\u56e0\u4e3arange\u7684\u8bc4\u4f30\u5e76\u6ca1\u6709\u52a0\u4e0a\u8fd9\u4e2a\u4fee\u6b63\uff0c\u6240\u4ee5\u5c31\u5bfc\u81f4\u4e86\u4e00\u4e9b\u5947\u602a\u7684\u4e8b\u60c5\u53d1\u751f\u4e86\uff0c\u540e\u9762\u6211\u4eec\u518d\u8be6\u7ec6\u5206\u6790\u8fd9\u4e00\u70b9\u3002<\/p>\n<h4>2.4 \u5168\u8868\u626b\u63cf\u7684\u6210\u672c<\/h4>\n<p>\u7b80\u5355\u7248\u672c(\u4e0d\u8003\u8651\u591a\u8868\u5173\u8054)\uff1a<\/p>\n<blockquote><p>scan_time() + s->records\/TIME_FOR_COMPARE<\/p><\/blockquote>\n<p>scan_time()\u4e3a\u5b58\u50a8\u5f15\u64ce\u8fd4\u56de\u7684\u5168\u8868\u626b\u63cfIO\u6b21\u6570\uff1bs->records\u4e3a\u5b58\u50a8\u5f15\u64ce\u7ef4\u62a4\u7684\u5355\u8868\u603b\u7eaa\u5f55\u6570\u3002<\/p>\n<p>\u590d\u6742\u7248\u672c(\u6709\u591a\u8868\u5173\u8054)\uff1a<\/p>\n<p>\u5047\u8bbe\u524d\u9762\u5173\u8054\u540e\u7684\u7eaa\u5f55\u6570\u4e3arecord_count\uff0c\u5f53\u524d\u8868\u7684where\u6761\u4ef6\u5c06\u8fc7\u6ee4\u540e\u5269\u4f593\/4\u7684\u7eaa\u5f55\uff08\u4e0d\u6ee1\u8db3where\u6761\u4ef6\u7684\u4e3a1\/4\uff09\uff0c\u5e76\u5c06\u8fd9\u4e2a\u503c\u8bb0\u4e3arnd_records\u3002<\/p>\n<pre><blockquote>(s->records - rnd_records)\/TIME_FOR_COMPARE +  \r\nrecord_count * (rnd_records\/TIME_FOR_COMPARE)<\/blockquote><\/pre>\n<p>\u8fd9\u91cc\u5047\u8bbe\u5c06\u8fc7\u6ee41\/4\u6570\u636e\uff0c\u5b9e\u9645\u4ee3\u7801\u4e2d\u8fd8\u5c06\u505a\u4e00\u6b21\u4fee\u6b63\uff0c\u5982\u679c\u6709range\u8ba1\u7b97\uff0c\u5047\u8bbe\u5176\u547d\u4e2dq\u6761\u7eaa\u5f55\uff0c\u90a3\u4e48\u5c31\u8ba4\u4e3a\u5c06\u8fc7\u6ee4s->records-q\u6761\u7eaa\u5f55\u3002<\/p>\n<h4>2.5 \u5173\u4e8eref\u6267\u884c\u8ba1\u5212\u7684\u5206\u6790<\/h4>\n<p>\u4e0a\u9762\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u770b\u5230\uff0cref\u6210\u672c\u6709\u4e00\u90e8\u5206\u662f\u53d6min\u51fd\u6570\u7684\uff0c\u4e3a\u4e86\u5206\u6790ref\u548c\u5168\u8868\u626b\u63cf\u7684\u4e34\u754c\u6761\u4ef6\uff0c\u4e3a\u4e86\u7b80\u5316\u505a\u4e0b\u9762\u7684\u5047\u8bbe\uff1a<\/p>\n<blockquote><p>(1) scan_time()*3  < s->records \/ 10<br \/>\n(2) scan_time()*3 < r <\/p><\/blockquote>\n<p>\u7b2c\u4e00\u4e2a\u6761\u4ef6\u8868\u793a\u7ea630\u6761\u7eaa\u5f55\u4e00\u4e2apage\uff1b\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u662fref\u547d\u4e2d\u7684\u8bb0\u5f55\u6570\u4e3a\u603b\u9875\u9762\u76843\u500d\u3002<\/p>\n<p>\u90a3\u4e48\u653e\u5f03ref\u5168\u8868\u626b\u63cf\u7684\u6761\u4ef6\u662f\uff1a<\/p>\n<blockquote><p>scan_time()*3 + r\/5  > scan_time() + R\/5<br \/>\n\u5373\uff1a<br \/>\nscan_time()*2 > (R-r)\/5<br \/>\nscan_time() > (R-r)\/10<br \/>\n\u5177\u4f53\u7684\uff1a<\/p><\/blockquote>\n<p><!-- <a href=\"http:\/\/www.flickr.com\/photos\/26825745@N06\/8260536234\/\" title=\"ref-ana by orczhou, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm9.staticflickr.com\/8488\/8260536234_0306525dd1.jpg\" width=\"406\" height=\"295\" alt=\"ref-ana\"><\/a> --><\/p>\n<p>(1) \u5047\u8bbe\u603b\u8bb0\u5f55\u6570\u4e3aR\uff1bref\u9700\u8981\u8fd4\u56de\u7684\u7eaa\u5f55\u6570\u4e3ar <\/p>\n<p>(2) \u5047\u8bbe\u8be5\u8868\u7684\u603b\u9875\u9762\u6570(IO COST)\u4e3aP\uff1b\u5355\u4e2a\u9875\u9762\u7eaa\u5f55\u6570\u4e3ac<\/p>\n<p>\u90a3\u4e48range\u7684\u4ee3\u4ef7\u8d85\u8fc7\u5168\u8868\u626b\u63cf\u4ee3\u4ef7\uff0c\u5219\u6709\uff1a<\/p>\n<p>\\[3*P + \\frac{r}{5} > P + \\frac{R}{5} \\]<\/p>\n<p>\\[\\frac{r}{R} > 1 &#8211; \\frac{10*P}{R}\\]<\/p>\n<p>\\[\\frac{r}{R} > 1 &#8211; \\frac{10}{c}\\]<\/p>\n<p>\u5728\u6211\u7684\u6d4b\u8bd5\u6848\u4f8b\u4e2d\uff0cP=6.4\uff0cR=900 \uff0c\u6709<\/p>\n<p>\\[ \\frac{r}{R} > 0.929 \\]<\/p>\n<p>\u5bf9\u4e8e\u5177\u4f53\u7684\u6848\u4f8b\uff0c\u7531\u4e8e\u53d6\u6574\u7684\u95ee\u9898\uff0c\u4f1a\u548c\u4e0a\u9762\u6709\u5c0f\u5c0f\u7684\u504f\u5dee\uff1a<\/p>\n<blockquote><p>3*((int)6.39) + r\/5 > 6.39453125 + 900\/5<br \/>\nr > 841.97<\/p><\/blockquote>\n<h4>2.6 \u9a8c\u8bc1<\/h4>\n<p>\u8fd9\u91cc\u518d\u901a\u8fc7gdb\u4fee\u6539r\u7684\u503c\u6765\u9a8c\u8bc1\uff0c\u56e0\u4e3aref\u547d\u4e2d\u7eaa\u5f55\u7684\u9884\u4f30\u662f\u53d6range\u7684\u8ba1\u7b97\u503c\uff0c\u6240\u4ee5\uff1a<\/p>\n<blockquote><p>gdb) set s->table->quick_rows[1]=841<br \/>\n(gdb) c<\/p>\n<p>root@test 04:37:16>explain select * from users where reg_date = &#8216;2012-09-21 12:00:00&#8217;;<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra       |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n|  1 | SIMPLE      | users | ref  | IND_REGDATE   | IND_REGDATE | 9       | const |  841 | Using where |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n1 row in set (47.61 sec)<\/p>\n<p>(gdb) set s->table->quick_rows[1]=842<br \/>\n(gdb) c<\/p>\n<p>root@test 04:38:46>explain select * from users where reg_date = &#8216;2012-09-21 12:00:00&#8217;;<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n|  1 | SIMPLE      | users | ALL  | IND_REGDATE   | NULL | NULL    | NULL |  900 | Using where |<br \/>\n+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+<\/p><\/blockquote>\n<p>\u53e6\u4e00\u4e2a\u7ed3\u8bba\u662f\uff0c\u5982\u679c\u5f53\u6761\u8bb0\u5f55\u5f88\u5c0f\uff0c\u5355\u4e2a\u9875\u9762\u7684\u8bb0\u5f55\u6570\u5f88\u591a\u7684\u8bdd\uff0c\u53ea\u6709\u9009\u62e9\u5ea6(selectivity)\u975e\u5e38\u9ad8\u7684\u65f6\u5019\uff0cMySQL\u624d\u4f1a\u653e\u5f03ref\uff0c\u8d70\u5168\u8868\u626b\u63cf\uff0c<a href=\"http:\/\/www.mysqlperformanceblog.com\/2006\/06\/02\/indexes-in-mysql\/\" target=\"_blank\">\u8fd9\u4e5f\u662f\uff0cVadim\u57282006\u5e74\u5410\u69fdMySQL\u7684\u4e00\u70b9<\/a>\u3002<\/p>\n<h3>3. \u4e0a\u9762\u8ba1\u7b97\u7684\u5c40\u9650\u6027<\/h3>\n<p>\u4e0a\u9762\u7684\u63a8\u5012\u5c1d\u8bd5\u4ecb\u7ecd\u4e00\u4e9b\u901a\u7528\u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u4f18\u5316\u5668\u4e2d\u8ba1\u7b97ref\/range\u7684\u6210\u672c\u65f6\uff0c\u4f1a\u6709\u4e00\u4e9b\u4e0d\u540c\uff1a<\/p>\n<p>(1) \u65e0\u8bba\u65f6InnoDB\u8fd8\u662fMyISAM\u7684scan_time\uff0crange\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u90fd\u4e0d\u662f\u7cbe\u786e\u503c\uff0c\u800c\u4e14\u5bf9\u4e8eInnoDB\uff0c\u603b\u8bb0\u5f55\u6570\u4e5f\u4e0d\u662f\u7cbe\u786e\u503c\uff0c\u6240\u4ee5\u4e0a\u9762\u53ea\u662f\u4e00\u4e2aHigh level\u7684\u9884\u4f30<\/p>\n<p>(2) \u4e0a\u9762\u6848\u4f8b\u4e2d\uff0c\u6761\u7eaa\u5f55\u5f88\u77ed\uff0c\u6240\u4ee5\u770b\u5230\u603bpage\u5f88\u5c11\uff0c\u5b9e\u9645\u60c5\u51b5\uff0c\u5355\u6761\u7eaa\u5f55\u66f4\u5927\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u7684\u5355\u4e2a\u9875\u9762\u7eaa\u5f55\u6570\u4e3ac\u66f4\u5c0f\uff0c\u6240\u4ee5\u901a\u5e38\u9009\u62e9\u5ea6\u66f4\u9ad8\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u9009\u62e9\u5168\u8868\u626b\u63cf\u3002<\/p>\n<p>(3) \u4e0a\u9762\u7684\u8ba1\u7b97\uff0c\u90fd\u4e0d\u662f\u8986\u76d6\u626b\u63cf\u7684\u60c5\u51b5\uff0c\u8986\u76d6\u626b\u63cf\u7684\u65f6\u5019\uff0c\u6210\u672c\u8ba1\u7b97\u4e0e\u4e0a\u9762\u7565\u6709\u4e0d\u540c<\/p>\n<p>(4) \u4e0a\u9762\u90fd\u662f\u4f7f\u7528gdb\u4fee\u6539\u67d0\u4e9b\u503c\u7684\u65b9\u5f0f\u6765\u9a8c\u8bc1\u3002\u5982\u679c\u60f3\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u8868\uff0c\u591f\u9020\u67d0\u4e2a\u7d22\u5f15\u7684\u533a\u5206\u5ea6\/\u9009\u5236\u5ea6\uff0c\u56e0\u4e3ascan_time\u548c\u8fd4\u56de\u7684\u8bb0\u5f55\u6570\u90fd\u662f\u9884\u4f30\u7684\uff0c\u8fd9\u6837\u7684\u65b9\u5f0f\u662f\u4e0d\u884c\u7684<\/p>\n<p>(5) (update) range\u7684cost\u8ba1\u7b97\uff0c\u6700\u7ec8\u7684\u516c\u5f0f\u662f\uff1a#rows + (#rows\/5)*2 + 1 \u89e3\u91ca\u5982\u4e0b\uff0c<\/p>\n<p>** #rows \u4e3aIO\u6210\u672c\uff0c\u56e0\u4e3a\u8bfb\u53d6\u7684\u8bb0\u5f55\u90fd\u9700\u8981\u56de\u8868\u67e5\u627e\u5b8c\u6574\u8bb0\u5f55\uff0c\u800c\u8fd9\u4e9b\u90fd\u662f\u79bb\u6563IO\uff0c\u6240\u4ee5\u591a\u5c11\u6761\u8bb0\u5f55\uff0c\u591a\u5c11\u4e2aIO<\/p>\n<p>** (#rows\/5)*2 \u662fCPU\u6210\u672c\uff0c\u5206\u4e24\u90e8\u5206\uff0c\u7b2c\u4e00\u90e8\u5206\u662f\u626b\u63cf\u7d22\u5f15\u65f6\uff0c\u786e\u5b9a\u5728\u67e5\u627e\u8303\u56f4\u5185\uff1b\u7b2c\u4e8c\u90e8\u5206\u662f\u627e\u5230\u8bb0\u5f55\u540e\u5224\u65ad\u662f\u5426\u6ee1\u8db3WHERE\u6761\u4ef6\uff1b(\u8fd9\u90e8\u5206\u6210\u672c\uff0c\u5728range analysis\u7684\u65f6\u5019\u6ca1\u6709\u8ba1\u7b97)<\/p>\n<p>** 1\u662f\u4e00\u4e2a\u4fee\u6b63\u503c\uff0c\u9632\u6b620\u6210\u672c\u51fa\u73b0<\/p>\n<h3>4. \u6848\u4f8b\u4e2d\u4f7f\u7528\u7684\u6570\u636e\u548c\u8868<\/h3>\n<pre><blockquote>CREATE TABLE `users` (\r\n  `id` int(11) NOT NULL,\r\n  `nick` varchar(32) DEFAULT NULL,\r\n  `reg_date` datetime DEFAULT NULL,\r\n  KEY `IND_NICK` (`nick`),\r\n  KEY `IND_REGDATE` (`reg_date`),\r\n  KEY `IND_ID` (`id`)\r\n) ENGINE=MyISAM\r\nfor id in `seq 1 886`; \\\r\ndo mysql -uroot test -e \\\r\n\"insert into users values($id,char(round(ord('A') + rand()*(ord('z')-ord('A')))),\\\r\n'2012-09-21 12:00:00')\"  ;done\r\n\r\nfor id in `seq 887 900`; \\\r\ndo mysql -uroot test -e \\\r\n\"insert into users values($id,char(round(ord('A') + rand()*(ord('z')-ord('A')))),\\\r\n'2012-09-20 12:00:00')\"  ;done\r\n<\/blockquote><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u59cb\u4ecb\u7ecdindex merge\/ROR\u4f18\u5316\u4e4b\u524d\uff0c\u6253\u7b97\u5148\u4ecb\u7ecdMySQL\u662f\u5982\u4f55\u5bf9range\/ref\u505a\u6210\u672c\u8bc4\u4f30\u7684\u3002MySQL\u662f\u57fa\u4e8e\u6210\u672c(cost)\u6a21\u578b\u9009\u62e9\u6267\u884c\u8ba1\u5212\uff0c\u5728\u591a\u4e2arange\uff0c\u5168\u8868\u626b\u63cf\uff0cref\u4e4b\u95f4\u4f1a\u9009\u62e9\u6210\u672c\u6700\u5c0f\u7684\u4f5c\u4e3a\u6700\u7ec8\u7684\u6267\u884c\u8ba1\u5212\u3002\u4ecd\u7136\u5f3a\u70c8\u5efa\u8bae\u5148\u9605\u8bfb\u767b\u535a\u7684slide\uff1a\u300a\u67e5\u8be2\u4f18\u5316\u6d45\u6790\u300b\uff0c\u6587\u4e2d\u8f83\u4e3a\u8be6\u7ec6\u7684\u4ecb\u7ecdMySQL\u5728range\u4f18\u5316\u65f6\u6210\u672c\u7684\u8ba1\u7b97\u3002 \u672c\u6587\u5c06\u7ee7\u7eed\u4ecb\u7ecdrange\/ref\u6267\u884c\u8ba1\u5212\u9009\u62e9\u7684\u4e00\u4e9b\u4e0d\u5bb9\u5ffd\u7565\u7684\u7ec6\u8282\u3002\u5e0c\u671b\u770b\u5ba2\u80fd\u591f\u901a\u8fc7\u6b64\u6587\u80fd\u591f\u4e86\u89e3\u66f4\u591a\u7ec6\u8282\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6,4],"tags":[101,118,102,58],"class_list":["post-4117","post","type-post","status-publish","format-standard","hentry","category-mysql","category-code-detail","tag-index","tag-mysql","tag-optimizer","tag-source-code"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=4117"}],"version-history":[{"count":30,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4117\/revisions"}],"predecessor-version":[{"id":4145,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4117\/revisions\/4145"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=4117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=4117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=4117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}