表的相關(guān)情況如下:
結(jié)構(gòu):
數(shù)據(jù):
聯(lián)合索引:
分析語句如下:
EXPLAIN SELECT
*
FROM
c_test
WHERE
c3 = 6
AND c4 = 8
AND c5 =3
結(jié)果如下:
不明白為什么possible_keys為null了, key卻有值, 然后rows為7,這特么不是全表掃描么!!!
而Extra的Using index不是覆蓋索引么, 懵逼了!!!
explain-join-types (MySQL 5.7 Reference Manual)
If the index is a covering index for the queries and can be used to satisfy all data required from the table, only the index tree is scanned. In this case, the Extra column says Using index. An index-only scan usually is faster than ALL because the size of the index usually is smaller than the table data.
你的索引c1c2排在最前面,查詢時where里又沒有,所以索引是用不上的.
EDITED:
你這結(jié)果之所以這樣,是因為聚簇索引的原因, 你的索引包含了全部數(shù)據(jù)(主鍵在所有的索引里都存在), 等于是和掃描全表一樣的.
以下是有點長的解釋, 有興趣的可以執(zhí)行看看
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
`c4` int(11) DEFAULT NULL,
`c5` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `c12345` (`c1`,`c2`,`c3`,`c4`,`c5`)
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=latin1;
delimiter #
create procedure fill_data()
begin
declare v_max int unsigned default 10000;
declare v_counter int unsigned default 0;
truncate table mytable;
start transaction;
while v_counter < v_max do
insert into mytable (c1,c2,c3,c4,c5) values(null,null,null,null,null);
set v_counter=v_counter+1;
end while;
commit;
end #
delimiter ;
-- drop TRIGGER before_insert_mytable;
DELIMITER ;;
CREATE TRIGGER before_insert_mytable
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
IF new.c1 IS NULL THEN
SET new.c1 = floor(0 + (rand() * 65535));
END IF;
IF new.c2 IS NULL THEN
SET new.c2 = floor(0 + (rand() * 65535));
END IF;
IF new.c3 IS NULL THEN
SET new.c3 = floor(0 + (rand() * 65535));
END IF;
IF new.c4 IS NULL THEN
SET new.c4 = floor(0 + (rand() * 65535));
END IF;
IF new.c5 IS NULL THEN
SET new.c5 = floor(0 + (rand() * 65535));
END IF;
END
;;
ALTER TABLE `test`.`mytable`
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ;
ALTER TABLE `test`.`mytable`
ADD INDEX `c12345` (`c1` ASC, `c2` ASC, `c3` ASC, `c4` ASC, `c5` ASC);
call fill_data();
EXPLAIN SELECT
*
FROM
mytable
WHERE
c3 = 6
AND c4 = 8
AND c5 =3;
/*
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'mytable', 'index', NULL, 'c12345', '25', NULL, '10207', 'Using where; Using index'
*/
ALTER TABLE `test`.`mytable`
drop INDEX `c12345` (`c1` ASC, `c2` ASC, `c3` ASC, `c4` ASC, `c5` ASC);
ALTER TABLE `test`.`mytable`
add INDEX `c1234` (`c1` ASC, `c2` ASC, `c3` ASC, `c4` ASC);
EXPLAIN SELECT
*
FROM
mytable
WHERE
c3 = 6
AND c4 = 8
AND c5 =3;
/*
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'mytable', 'ALL', NULL, NULL, NULL, NULL, '10207', 'Using where'
*/
ALTER TABLE `test`.`mytable`
drop INDEX `c1234`;
ALTER TABLE `test`.`mytable`
add INDEX `c2345` ( `c2` ASC, `c3` ASC, `c4` ASC, `c5` ASC);
EXPLAIN SELECT
*
FROM
mytable
WHERE
c3 = 6
AND c4 = 8
AND c5 =3;
/*
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
'1', 'SIMPLE', 'mytable', 'ALL', NULL, NULL, NULL, NULL, '10207', 'Using where'
*/ ```
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。