国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

簡單分析MySQL中的primary key功能

2020-01-18 22:56:26
字體:
來源:轉載
供稿:網友

在5.1.46中優化器在對primary key的選擇上做了一點改動:

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。

該版本中增加了find_shortest_key函數,該函數的作用可以認為是選擇最小key length的

索引來滿足我們的查詢。

該函數是怎么工作的:

復制代碼 代碼如下:
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 the

primary key is clustered, like in InnoDB, then it should not consider using the primary

key because then the storage engine will have to scan through much more data.

調用Primary_key_is_clustered(),當返回值為true,執行find_shortest_key:選擇key length最小的覆蓋索引(Secondary covering indexes),然后來滿足查詢。

首先在5.1.45中測試:

$mysql -Vmysql Ver 14.14 Distrib 5.1.45, for unknown-linux-gnu (x86_64) using EditLine wrapperroot@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',now()),(3,'sdds','ddd',now()),(4,'sdsdf','dsd',now()),(5,'sdsdaa','sds',now());Query OK, 5 rows affected (0.00 sec)Records: 5 Duplicates: 0 Warnings: 0root@test 03:49:51>root@test 03:49:51>insert into test values(6,'xce','sdsd',now()),(7,'xcx','sdsd',now()),(8,'sdds','sds',now()),(9,'sdsdsdf','sdsdsd',now()),(10,'sdssdfdaa','sdsdsd',now());Query OK, 5 rows affected (0.00 sec)Records: 5 Duplicates: 0 Warnings: 0

創建索引ind_1:

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: 0root@test 03:50:08>explain select count(*) from test;+―-+――――-+――-+――-+―――――+―――+―――+――+――+――――-+| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra    |+―-+――――-+――-+――-+―――――+―――+―――+――+――+――――-+| 1 | SIMPLE   | test | index | NULL     | PRIMARY | 4    | NULL |  10 | Using index |+―-+――――-+――-+――-+―――――+―――+―――+――+――+――――-+1 row in set (0.00 sec)

添加ind_2:

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: 0root@test 08:04:45>explain select count(*) from test;+―-+――――-+――-+――-+―――――+―――+―――+――+――+――――-+| id | select_type | table | type | possible_keys | key   | key_len | ref | rows | Extra    |+―-+――――-+――-+――-+―――――+―――+―――+――+――+――――-+| 1 | SIMPLE   | test | index | NULL     | PRIMARY | 4    | NULL |  10 | Using index |+―-+――――-+――-+――-+―――――+―――+―――+――+――+――――-+1 row in set (0.00 sec)

上面的版本【5.1.45】中,可以看到優化器選擇使用主鍵來完成掃描,并沒有使用ind_1,ind_2來完成查詢;

接下來是:5.1.48

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

創建索引ind_1:

root@test 03:13:57>alter table test add index ind_1(name,d);Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0root@test 03:15:55>explain select count(*) from test;+―-+――――-+――-+――-+―――――+――-+―――+――+――+――――-+| id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra    |+―-+――――-+――-+――-+―――――+――-+―――+――+――+――――-+| 1 | SIMPLE   | test | index | NULL     | ind_1 | 52   | NULL |  10 | Using index |+―-+――――-+――-+――-+―――――+――-+―――+――+――+――――-+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;+―-+――――-+――-+――-+―――――+――-+―――+――+――+――――-+| id | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra    |+―-+――――-+――-+――-+―――――+――-+―――+――+――+――――-+| 1 | SIMPLE   | test | index | NULL     | ind_2 | 9    | NULL |  10 | Using index |+―-+――――-+――-+――-+―――――+――-+―――+――+――+――――-+1 row in set (0.00 sec)

版本【5.1.48】中首先明智的選擇ind_1來完成掃描,并沒有考慮到使用主鍵(全索引掃描)來完成查詢,隨后添加ind_2,由于 ind_1的key長度是大于ind_2 key長度,所以mysql選擇更優的ind_2來完成查詢,可以看到mysql在選擇方式上也在慢慢智能了。

觀察性能:

5.1.48root@test 08:49:32>set profiling =1;Query OK, 0 rows affected (0.00 sec)root@test 08:49:41>select count(*) from test;+―――-+| count(*) |+―――-+| 5242880 |+―――-+1 row in set (1.18 sec)root@test 08:56:30>show profile cpu,block io for query 1;+――――――――――

主站蜘蛛池模板:
惠州市|
阆中市|
台东县|
义马市|
德保县|
麻栗坡县|
怀远县|
嵊州市|
昔阳县|
城步|
贵州省|
姜堰市|
永嘉县|
新野县|
会理县|
绥芬河市|
定襄县|
高青县|
民丰县|
阳原县|
高淳县|
南昌县|
岳阳市|
华池县|
恩施市|
宝鸡市|
基隆市|
柏乡县|
青河县|
琼海市|
如皋市|
湛江市|
鄂伦春自治旗|
五大连池市|
绍兴市|
玉山县|
定陶县|
汶川县|
固镇县|
江北区|
新沂市|