¼ÓÈëÊÕ²Ø | ÉèΪÊ×Ò³ | »áÔ±ÖÐÐÄ | ÎÒҪͶ¸å Àî´óͬ £¨https://www.lidatong.com.cn/£©- ¿Æ¼¼¡¢½¨Õ¾¡¢¾­Ñé¡¢ÔÆ¼ÆËã¡¢5G¡¢´óÊý¾Ý,Õ¾³¤Íø!
µ±Ç°Î»Ö㺠Ê×Ò³ > Õ¾³¤Ñ§Ôº > MySql½Ì³Ì > ÕýÎÄ

MysqlÓ¦Óüòµ¥·ÖÎöMySQLÖеÄprimary key¹¦ÄÜ

·¢²¼Ê±¼ä£º2020-12-12 01:21:31 ËùÊôÀ¸Ä¿£ºMySql½Ì³Ì À´Ô´£ºÍøÂçÕûÀí
µ¼¶Á£º¡¶MysqlÓ¦Óüòµ¥·ÖÎöMySQLÖеÄprimary key¹¦ÄÜ¡·Òªµã£º ±¾ÎĽéÉÜÁËMysqlÓ¦Óüòµ¥·ÖÎöMySQLÖеÄprimary key¹¦ÄÜ£¬Ï£Íû¶ÔÄúÓÐÓá£Èç¹ûÓÐÒÉÎÊ£¬¿ÉÒÔÁªÏµÎÒÃÇ¡£ ÔÚ5.1.46ÖÐÓÅ»¯Æ÷ÔÚ¶Ôprimary keyµÄÑ¡ÔñÉÏ×öÁËÒ»µã¸Ä¶¯£º MYSQL½Ì³Ì Performance: While looking fo

¡¶MysqlÓ¦Óüòµ¥·ÖÎöMySQLÖеÄprimary key¹¦ÄÜ¡·Òªµã£º
±¾ÎĽéÉÜÁËMysqlÓ¦Óüòµ¥·ÖÎöMySQLÖеÄprimary key¹¦ÄÜ£¬Ï£Íû¶ÔÄúÓÐÓá£Èç¹ûÓÐÒÉÎÊ£¬¿ÉÒÔÁªÏµÎÒÃÇ¡£

ÔÚ5.1.46ÖÐÓÅ»¯Æ÷ÔÚ¶Ôprimary keyµÄÑ¡ÔñÉÏ×öÁËÒ»µã¸Ä¶¯£ºMYSQL½Ì³Ì

Performance: While looking for the shortest index for a covering index scan,the optimizer did not consider the full row length for a clustered primary key,as in InnoDB. Secondary covering indexes will now be preferred,making full table scans less likely.MYSQL½Ì³Ì

¸Ã°æ±¾ÖÐÔö¼ÓÁËfind_shortest_keyº¯Êý,¸Ãº¯ÊýµÄ×÷ÓÿÉÒÔÈÏΪÊÇÑ¡Ôñ×îСkey lengthµÄMYSQL½Ì³Ì

Ë÷ÒýÀ´Âú×ãÎÒÃǵIJéѯ.MYSQL½Ì³Ì

¸Ãº¯ÊýÊÇÔõô¹¤×÷µÄ£ºMYSQL½Ì³Ì

´úÂëÈçÏÂ: What find_shortest_key should do is the following. If the primary key is a covering index

and is clustered,like in MyISAM,then the behavior today should remain the same. If theMYSQL½Ì³Ì

primary key is clustered,like in InnoDB,then it should not consider using the primaryMYSQL½Ì³Ì

key because then the storage engine will have to scan through much more data.MYSQL½Ì³Ì

µ÷ÓÃPrimary_key_is_clustered(),µ±·µ»ØÖµÎªtrue,Ö´ÐÐfind_shortest_key£ºÑ¡Ôñkey length×îСµÄ¸²¸ÇË÷Òý(Secondary covering indexes),È»ºóÀ´Âú×ã²éѯ.MYSQL½Ì³Ì

Ê×ÏÈÔÚ5.1.45ÖвâÊÔ£ºMYSQL½Ì³Ì

$mysql -V
mysql Ver 14.14 Distrib 5.1.45,for unknown-linux-gnu (x86_64) using EditLine wrapper
root@test 03:49:45>create table test(id int,name varchar(20),name2 varchar(20),d datetime,primary key(id)) engine=innodb;
Query OK,0 rows affected (0.16 sec)
root@test 03:49:47>insert into test values(1,'xc','sds',now()),(2,'xcx','dd',(3,'sdds','ddd',(4,'sdsdf','dsd',(5,'sdsdaa',now());
Query OK,5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
root@test 03:49:51>
root@test 03:49:51>insert into test values(6,'xce','sdsd',(7,(8,(9,'sdsdsdf','sdsdsd',(10,'sdssdfdaa',5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

´´½¨Ë÷Òýind_1£ºMYSQL½Ì³Ì

root@test 03:49:53>alter table test add index ind_1(name,d);
Query OK,0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@test 03:50:08>explain select count(*) from test;
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D¨D+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra    |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D¨D+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| 1 | SIMPLE   | test | index | NULL     | PRIMARY | 4    | NULL |  10 | Using index |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D¨D+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
1 row in set (0.00 sec)

Ìí¼Óind_2£ºMYSQL½Ì³Ì

root@test 08:04:35>alter table test add index ind_2(d);
Query OK,0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@test 08:04:45>explain select count(*) from test;
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D¨D+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra    |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D¨D+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| 1 | SIMPLE   | test | index | NULL     | PRIMARY | 4    | NULL |  10 | Using index |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D¨D+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
1 row in set (0.00 sec)

ÉÏÃæµÄ°æ±¾¡¾5.1.45¡¿ÖÐ,¿ÉÒÔ¿´µ½ÓÅ»¯Æ÷Ñ¡ÔñʹÓÃÖ÷¼üÀ´Íê³ÉɨÃè,²¢Ã»ÓÐʹÓÃind_1,ind_2À´Íê³É²éѯ£»MYSQL½Ì³Ì

½ÓÏÂÀ´ÊÇ£º5.1.48MYSQL½Ì³Ì

$mysql -V
mysql Ver 14.14 Distrib 5.1.48,for unknown-linux-gnu (x86_64) using EditLine wrapper
root@test 03:13:15> create table test(id int,0 rows affected (0.00 sec)
root@test 03:48:04>insert into test values(1,5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
root@test 03:48:05>insert into test values(6,5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

´´½¨Ë÷Òýind_1£ºMYSQL½Ì³Ì

root@test 03:13:57>alter table test add index ind_1(name,0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
root@test 03:15:55>explain select count(*) from test;
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D-+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra    |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D-+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| 1 | SIMPLE   | test | index | NULL     | ind_1 | 52   | NULL |  10 | Using index |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D-+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
root@test 08:01:56>alter table test add index ind_2(d);
Query OK,0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
Ìí¼Óind_2£º
root@test 08:02:09>explain select count(*) from test;
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D-+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra    |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D-+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
| 1 | SIMPLE   | test | index | NULL     | ind_2 | 9    | NULL |  10 | Using index |
+¨D-+¨D¨D¨D¨D-+¨D¨D-+¨D¨D-+¨D¨D¨D¨D¨D+¨D¨D-+¨D¨D¨D+¨D¨D+¨D¨D+¨D¨D¨D¨D-+
1 row in set (0.00 sec)

°æ±¾¡¾5.1.48¡¿ÖÐÊ×ÏÈÃ÷ÖǵÄÑ¡Ôñind_1À´Íê³ÉɨÃè,²¢Ã»Óп¼Âǵ½Ê¹ÓÃÖ÷¼ü(È«Ë÷ÒýɨÃè)À´Íê³É²éѯ,ËæºóÌí¼Óind_2,ÓÉÓÚ ind_1µÄkey³¤¶ÈÊÇ´óÓÚind_2 key³¤¶È,ËùÒÔmysqlÑ¡Ôñ¸üÓŵÄind_2À´Íê³É²éѯ,¿ÉÒÔ¿´µ½mysqlÔÚÑ¡Ôñ·½Ê½ÉÏÒ²ÔÚÂýÂýÖÇÄÜÁË.MYSQL½Ì³Ì

¹Û²ìÐÔÄÜ£ºMYSQL½Ì³Ì

5.1.48
root@test 08:49:32>set profiling =1;
Query OK,0 rows affected (0.00 sec)
root@test 08:49:41>select count(*) from test;
+¨D¨D¨D-+
| count(*) |
+¨D¨D¨D-+
| 5242880 |
+¨D¨D¨D-+
1 row in set (1.18 sec)
root@test 08:56:30>show profile cpu,block io for query 1;
+¨D¨D¨D¨D¨D¨D¨D¨D¨D¨DC+¨D¨D¨D-+¨D¨D¨D-+¨D¨D¨D¨D+¨D¨D¨D¨DC+¨D¨D¨D¨D¨D+
| Status             | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+¨D¨D¨D¨D¨D¨D¨D¨D¨D¨DC+¨D¨D¨D-+¨D¨D¨D-+¨D¨D¨D¨D+¨D¨D¨D¨DC+¨D¨D¨D¨D¨D+
| starting            | 0.000035 | 0.000000 |  0.000000 |      0 |       0 |
| checking query cache for query | 0.000051 | 0.000000 |  0.000000 |      0 |       0 |
| Opening tables         | 0.000014 | 0.000000 |  0.000000 |      0 |       0 |
| System lock          | 0.000005 | 0.000000 |  0.000000 |      0 |       0 |
| Table lock           | 0.000010 | 0.000000 |  0.000000 |      0 |       0 |
| init              | 0.000015 | 0.000000 |  0.000000 |      0 |       0 |
| optimizing           | 0.000007 | 0.000000 |  0.000000 |      0 |       0 |
| statistics           | 0.000015 | 0.000000 |  0.000000 |      0 |       0 |
| preparing           | 0.000012 | 0.000000 |  0.000000 |      0 |       0 |
| executing           | 0.000007 | 0.000000 |  0.000000 |      0 |       0 |
| Sending data          | 1.178452 | 1.177821 |  0.000000 |      0 |       0 |
| end              | 0.000016 | 0.000000 |  0.000000 |      0 |       0 |
| query end           | 0.000005 | 0.000000 |  0.000000 |      0 |       0 |
| freeing items         | 0.000040 | 0.000000 |  0.000000 |      0 |       0 |
| logging slow query       | 0.000002 | 0.000000 |  0.000000 |      0 |       0 |
| logging slow query       | 0.000086 | 0.000000 |  0.000000 |      0 |       0 |
| cleaning up          | 0.000006 | 0.000000 |  0.000000 |      0 |       0 |
+¨D¨D¨D¨D¨D¨D¨D¨D¨D¨DC+¨D¨D¨D-+¨D¨D¨D-+¨D¨D¨D¨D+¨D¨D¨D¨DC+¨D¨D¨D¨D¨D+

¶Ô±ÈÐÔÄÜ£ºMYSQL½Ì³Ì

5.1.45
root@test 08:57:18>set profiling =1;
Query OK,0 rows affected (0.00 sec)
root@test 08:57:21>select count(*) from test;
+¨D¨D¨D-+
| count(*) |
+¨D¨D¨D-+
| 5242880 |
+¨D¨D¨D-+
1 row in set (1.30 sec)
root@test 08:57:27>show profile cpu,block io for query 1;
+¨D¨D¨D¨D¨D¨D¨D¨D¨D¨DC+¨D¨D¨D-+¨D¨D¨D-+¨D¨D¨D¨D+¨D¨D¨D¨DC+¨D¨D¨D¨D¨D+
| Status             | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+¨D¨D¨D¨D¨D¨D¨D¨D¨D¨DC+¨D¨D¨D-+¨D¨D¨D-+¨D¨D¨D¨D+¨D¨D¨D¨DC+¨D¨D¨D¨D¨D+
| starting            | 0.000026 | 0.000000 |  0.000000 |      0 |       0 |
| checking query cache for query | 0.000041 | 0.000000 |  0.000000 |      0 |       0 |
| Opening tables         | 0.000014 | 0.000000 |  0.000000 |      0 |       0 |
| System lock          | 0.000005 | 0.000000 |  0.000000 |      0 |       0 |
| Table lock           | 0.000008 | 0.000000 |  0.000000 |      0 |       0 |
| init              | 0.000015 | 0.000000 |  0.000000 |      0 |       0 |
| optimizing           | 0.000006 | 0.000000 |  0.000000 |      0 |       0 |
| statistics           | 0.000014 | 0.000000 |  0.000000 |      0 |       0 |
| preparing           | 0.000012 | 0.000000 |  0.000000 |      0 |       0 |
| executing           | 0.000007 | 0.000000 |  0.000000 |      0 |       0 |
| Sending data          | 1.294178 | 1.293803 |  0.000000 |      0 |       0 |
| end              | 0.000016 | 0.000000 |  0.000000 |      0 |       0 |
| query end           | 0.000004 | 0.000000 |  0.000000 |      0 |       0 |
| freeing items         | 0.000040 | 0.000000 |  0.001000 |      0 |       0 |
| logging slow query       | 0.000002 | 0.000000 |  0.000000 |      0 |       0 |
| logging slow query       | 0.000080 | 0.000000 |  0.000000 |      0 |       0 |
| cleaning up          | 0.000006 | 0.000000 |  0.000000 |      0 |       0 |
+¨D¨D¨D¨D¨D¨D¨D¨D¨D¨DC+¨D¨D¨D-+¨D¨D¨D-+¨D¨D¨D¨D+¨D¨D¨D¨DC+¨D¨D¨D¨D¨D+

´ÓÉÏÃæµÄprofileÖпÉÒÔ¿´µ½ÔÚSending dataÉÏ,²îÒ컹ÊDZȽÏÃ÷ÏÔµÄ,mysql²»ÐèҪɨÃèÕû¸ö±íµÄÒ³¿é,¶øÊÇɨÃè±íÖÐË÷Òýkey×î¶ÌµÄË÷ÒýÒ³¿éÀ´Íê³É²éѯ,ÕâÑù¾Í¼õÉÙÁ˺ܶ಻±ØÒªµÄÊý¾Ý.MYSQL½Ì³Ì

PS:innodbÊÇÊÂÎñÒýÇæ,ËùÒÔÔÚÒ¶×Ó½ÚµãÖгýÁË´æ´¢±¾ÐмǼÍâ,»¹»á¶à¼Ç¼һЩ¹ØÓÚÊÂÎñµÄÐÅÏ¢(DB_TRX_ID,DB_ROLL_PTR µÈ),Òò´Ëµ¥Ðг¤¶È¶îÍ⿪Ïú20¸ö×Ö½Ú×óÓÒ,×îÖ±¹ÛµÄ·½·¨Êǽ«myisamתΪinnodb,´æ´¢¿Õ¼ä»áÃ÷ÏÔÉÏÉý.ÄÇôÔÚÖ÷±íΪt(id,name,pk(id)),¶þ¼¶Ë÷Òýind_name(name,id),Õâ¸öʱºòºÜÈÝÒ×»ìÏý,¼´Ê¹Ö»ÓÐÁ½¸ö×Ö¶Î,µÚÒ»Ë÷Òý»¹ÊDZȵڶþË÷ÒýÒª´ó(¿ÉÒÔͨ¹ýinnodb_table_monitor¹Û²ì±íµÄµÄÄÚ²¿½á¹¹)ÔÚ²éѯËùÓÐidµÄʱºò,ÓÅ»¯Æ÷»¹ÊÇ»áÑ¡ÔñµÚ¶þË÷Òýind_name.
MYSQL½Ì³Ì

£¨±à¼­£ºÀî´óͬ£©

¡¾ÉùÃ÷¡¿±¾Õ¾ÄÚÈݾùÀ´×ÔÍøÂ磬ÆäÏà¹ØÑÔÂÛ½ö´ú±í×÷Õ߸öÈ˹۵㣬²»´ú±í±¾Õ¾Á¢³¡¡£ÈôÎÞÒâÇÖ·¸µ½ÄúµÄȨÀû£¬Ç뼰ʱÓëÁªÏµÕ¾³¤É¾³ýÏà¹ØÄÚÈÝ!

    ÍÆ¼öÎÄÕÂ
      ÈȵãÔĶÁ