1. 程式人生 > >MySQL聯合索引最左匹配範例

MySQL聯合索引最左匹配範例

HERE order xtra pack sim simple emp pan int

MySQL聯合索引最左匹配範例
參考文章:http://blog.jobbole.com/24006/

創建示例表。
示例表來自MySQL官方文檔: https://dev.mysql.com/doc/employee/en/
CREATE TABLE titles (
emp_no      INT             NOT NULL,
title       VARCHAR(50)     NOT NULL,
from_date   DATE            NOT NULL,
to_date     DATE,
PRIMARY KEY (emp_no,title,from_date)
) ;

導入測試數據  load_titles.dump 
https:
//github.com/datacharmer/test_db 索引情況 mysql> show index from titles; +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comme
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------ | titles | 0 | PRIMARY | 1 | emp_no | A | 0 | NULL | NULL | | BTREE | | titles | 0 | PRIMARY | 2 | title | A | 0 | NULL | NULL | | BTREE | | titles | 0 | PRIMARY | 3 | from_date | A | 0 | NULL | NULL | | BTREE | +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------ 3 rows in
set (0.00 sec) 情況一.WHERE條件中全部列都在索引中 mysql> desc SELECT * FROM titles WHERE emp_no=10001 AND title=Senior Engineer AND from_date=1986-06-26; +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ | 1 | SIMPLE | titles | NULL | const | PRIMARY | PRIMARY | 159 | const,const,const | 1 | 100.00 | NULL | +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE title=Senior Engineer AND emp_no=10001 AND from_date=1986-06-26; +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ | 1 | SIMPLE | titles | NULL | const | PRIMARY | PRIMARY | 159 | const,const,const | 1 | 100.00 | NULL | +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ 1 row in set, 1 warning (0.01 sec) mysql> desc SELECT * FROM titles WHERE from_date=1986-06-26 AND title=Senior Engineer AND emp_no=10001; +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ | 1 | SIMPLE | titles | NULL | const | PRIMARY | PRIMARY | 159 | const,const,const | 1 | 100.00 | NULL | +----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE emp_no=10001 AND title in(Senior Engineer,Staff,Assistant Engineer) AND from_date=1986-06-26; +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 1 | SIMPLE | titles | NULL | range | PRIMARY | PRIMARY | 159 | NULL | 3 | 100.00 | Using where | +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE emp_no in(10061,10062,10063,10064) AND title in(Senior Engineer,Staff,Assistant Engineer) AND from_date=1986-06-26; +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 1 | SIMPLE | titles | NULL | range | PRIMARY | PRIMARY | 159 | NULL | 12 | 100.00 | Using where | +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE emp_no=10001 AND title=Senior Engineer AND from_date>=1986-06-26; +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ | 1 | SIMPLE | titles | NULL | range | PRIMARY | PRIMARY | 159 | NULL | 1 | 100.00 | Using where | +----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) 情況二.WHERE條件和ORDER BY中全部列都在索引中 mysql> desc SELECT * FROM titles WHERE emp_no=10001 AND title=Senior Engineer order by from_date; +----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+ | 1 | SIMPLE | titles | NULL | ref | PRIMARY | PRIMARY | 156 | const,const | 1 | 100.00 | Using where | +----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE emp_no=10001 order by title,from_date; +----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | titles | NULL | ref | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using where | +----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.01 sec) mysql> desc SELECT * FROM titles order by emp_no,title,from_date; +----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------+ | 1 | SIMPLE | titles | NULL | index | NULL | PRIMARY | 159 | NULL | 441772 | 100.00 | NULL | +----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------+ 1 row in set, 1 warning (0.01 sec)

MySQL聯合索引最左匹配範例