show create table txtx; +-------+..."/>
您好,登錄后才能下訂單哦!
實(shí)驗(yàn)環(huán)境:MySQL 5.7.17
1、表結(jié)構(gòu)如下,在id,name,tx列建立主鍵
mysql> show create table txtx; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | txtx | CREATE TABLE `txtx` ( `id` int(11) NOT NULL, `name` char(2) NOT NULL, `tx` char(3) NOT NULL, `id1` int(11) DEFAULT NULL, PRIMARY KEY (`id`,`name`,`tx`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
2、查看執(zhí)行計(jì)劃
mysql> explain select * from txtx where id=1 and id1 =1 and tx='tx'; +----+-------------+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | txtx | NULL | ref | PRIMARY | PRIMARY | 4 | const | 1 | 33.33 | Using where | +----+-------------+-------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
從上面執(zhí)行計(jì)劃可以看出,盡管查詢在where語句中使用了id與tx列,但只使用了id列,原因是MySQL只能使用定義索引的最左邊部分。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。