创建官方测试库
官方 Employees Sample Database
Git: https://github.com/datacharmer/test_db
MySQL 5.5+
Download the repository
Change directory to the repository
Then run
基本操作
See https://www.runoob.com/mysql/mysql-index.html
修改表结构(添加索引)
1 2 3 4
| ALTER TABLE tbl_name ADD PRIMARY KEY (column_list); ALTER TABLE tbl_name ADD UNIQUE index_name (column_list); ALTER TABLE tbl_name ADD INDEX index_name (column_list); ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list);
|
创建表的时候直接指定
1 2 3 4 5 6 7
| CREATE TABLE mytable( ID INT NOT NULL, username1 VARCHAR(16) NOT NULL, username2 VARCHAR(16) NOT NULL, INDEX [indexName] (username1(length)) , UNIQUE [indexName] (username2(length)) );
|
删除索引的语法
1 2
| DROP INDEX [indexName] ON mytable; ALTER TABLE tbl_name DROP INDEX index_name;
|
查看索引
1
| SHOW INDEX FROM table_name;
|
查看索引大小
1 2 3
| USE `information_schema`;
SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB') AS 'Total Index Size' FROM `TABLES` WHERE table_name = 'dept_emp';
|
Index Type
const
1 2 3 4 5 6
| EXPLAIN SELECT * FROM employees.titles WHERE emp_no='10001' AND title='Senior Engineer' AND from_date='1986-06-26'; +----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+ | 1 | SIMPLE | titles | const | PRIMARY | PRIMARY | 59 | const,const,const | 1 | | +----+-------------+--------+-------+---------------+---------+---------+-------------------+------+-------+
|
理论上索引对顺序是敏感的,但是由于MySQL的查询优化器会自动调整where子句的条件顺序以使用适合的索引