id爲主鍵,id爲奇數sex=1,id爲偶數sex=0 sex=0,50000條數據;sex=1,50000條數據 mysql
CREATE TABLE `people` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`sex` tinyint(1) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8;
複製代碼
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_initData`()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<=100000 DO
INSERT INTO people(name, sex) VALUES(CONCAT('姓名',i),0);
SET i = i+1;
END WHILE;
END
CALL proc_initData();
UPDATE people SET sex = 1 WHERE MOD(id,2) = 1;
複製代碼
添加的索引類型 sql
SELECT * FROM people WHERE sex = 0;
SELECT * FROM people WHERE sex = 1;
複製代碼
無sex索引 | 有sex索引 | |
---|---|---|
sex=0 |
![]() |
![]() |
sex=1 |
![]() |
![]() |
添加索引後的EXPLAIN: 測試
在InnoDB中每個表都會有彙集索引,若是表定義了主鍵,則主鍵就是彙集索引。一個表只有一個彙集索引,其他爲普通索引。 索引的結構是B+樹,非葉子節點存儲key,葉子節點存儲value。ui
以上表的索引結構示例以下(PS:索引結構僅供參考) 彙集索引spa
參考:
mp.weixin.qq.com/s/tmkRAmc1M…
blog.jcole.us/innodb/
blog.csdn.net/u012978884/…
draveness.me/mysql-innod….net