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

首頁(yè) > 數(shù)據(jù)庫(kù) > MySQL > 正文

簡(jiǎn)單分析MySQL中的primary key功能

2024-07-24 12:46:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

在5.1.46中優(yōu)化器在對(duì)primary key的選擇上做了一點(diǎn)改動(dòng):

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函數(shù),該函數(shù)的作用可以認(rèn)為是選擇最小key length的

索引來(lái)滿足我們的查詢。

該函數(shù)是怎么工作的:

復(fù)制代碼 代碼如下:
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.

調(diào)用Primary_key_is_clustered(),當(dāng)返回值為true,執(zhí)行find_shortest_key:選擇key length最小的覆蓋索引(Secondary covering indexes),然后來(lái)滿足查詢。

首先在5.1.45中測(cè)試:

$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

創(chuàng)建索引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)
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赣州市| 宁夏| 榕江县| 牙克石市| 哈密市| 葵青区| 湘西| 子洲县| 通江县| 莒南县| 安吉县| 革吉县| 河津市| 定安县| 鄂伦春自治旗| 吉隆县| 大宁县| 肃宁县| 凤山市| 扶绥县| 皮山县| 夏邑县| 鄱阳县| 北安市| 新巴尔虎右旗| 会理县| 夹江县| 蓝田县| 明光市| 平谷区| 南澳县| 高邑县| 潞西市| 南康市| 遂溪县| 日喀则市| 昭平县| 津市市| 建昌县| 澳门| 淮阳县|