索引
索引
推薦博客MySQL索引原理以及查詢優化html
建立表
CREATE TABLE emp( id INT , NAME VARCHAR(20), gender VARCHAR(10), email VARCHAR(50) );
插入數據
臨時增長插入效率
mysql> set autocommit =off; mysql> show variables like 'autocom%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | OFF | +---------------+-------+ 1 row in set (0.00 sec)
存儲過程快速插入
delimiter $$ create procedure auto_insert1() BEGIN declare i int default 1; while(i<3000000)do insert into emp values(i,concat('jack',i),'male',concat('www.jack',i,'@qq.com')); set i=i+1; end while; END$$ delimiter ;
調用call auto_insert1() 手動 commit;java
插入操做
mysql> delimiter $$ mysql> create procedure auto_insert1() -> BEGIN -> declare i int default 1; -> while(i<300000)do -> insert into employer values(i,concat('egon',i),'male',concat('egon',i,'@oldboy')); -> set i=i+1; -> end while; -> END$$ Query OK, 0 rows affected (0.04 sec) mysql> delimiter ; mysql> show create procedure auto_insert1 \G *************************** 1. row *************************** Procedure: auto_insert1 sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION Create Procedure: CREATE DEFINER=`root`@`%` PROCEDURE `auto_insert1`() BEGIN declare i int default 1; while(i<300000)do insert into employer values(i,concat('jack',i),'male',concat('www.jack',i,'@qq.com')); set i=i+1; end while; END character_set_client: latin1 collation_connection: latin1_swedish_ci Database Collation: utf8mb4_0900_ai_ci 1 row in set (0.00 sec) mysql> call auto_insert1(); Query OK, 1 row affected (5 min 50.89 sec) mysql> drop PROCEDURE auto_insert1; mysql> commit; Query OK, 0 rows affected (10.24 sec) mysql> exit;
不增長索引查詢耗時
耗時4smysql
mysql> select * from emp where id = 33333; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 33333 | jack33333 | male | www.jack33333@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (4.55 sec)
存在300萬數據的時候增長索引耗時sql
mysql> create index indexid on emp(id); Query OK, 0 rows affected (21.24 sec) Records: 0 Duplicates: 0 Warnings: 0
增長索引的查詢耗時優化
mysql> select * from emp where id = 33333; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 33333 | jack33333 | male | www.jack33333@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (0.00 sec) mysql> select * from emp where id = 100333; +--------+------------+--------+-----------------------+ | id | NAME | gender | email | +--------+------------+--------+-----------------------+ | 100333 | jack100333 | male | www.jack100333@qq.com | +--------+------------+--------+-----------------------+ 1 row in set (0.01 sec)
刪除主鍵url
mysql> select * from emp where id =10000; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 10000 | jack10000 | male | www.jack10000@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (0.00 sec) mysql> drop index indexid on emp; Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from emp where id =10001; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 10001 | jack10001 | male | www.jack10001@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (4.79 sec) mysql> help create index^C mysql> create index indexid on emp(id); Query OK, 0 rows affected (19.61 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from emp where id =20001; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 20001 | jack20001 | male | www.jack20001@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (0.00 sec) mysql>
建立索引的語法
1.建立索引 -在建立表時就建立(須要注意的幾點) create table s1( id int ,#能夠在這加primary key #id int index #不能夠這樣加索引,由於index只是索引,沒有約束一說, #不能像主鍵,還有惟一約束同樣,在定義字段的時候加索引 name char(20), age int, email varchar(30) #primary key(id) #也能夠在這加 index(id) #能夠這樣加 ); -在建立表後在建立 create index name on s1(name); #添加普通索引 create unique age on s1(age);添加惟一索引 alter table s1 add primary key(id); #添加住建索引,也就是給id字段增長一個主鍵約束 create index name on s1(id,name); #添加普通聯合索引 2.刪除索引 drop index id on s1; drop index name on s1; #刪除普通索引 drop index age on s1; #刪除惟一索引,就和普通索引同樣,不用在index前加unique來刪,直接就能夠刪了 alter table s1 drop primary key; #刪除主鍵(由於它添加的時候是按照alter來增長的,那麼咱們也用alter來刪)
組合索引
建立組合索引spa
mysql> create index indexidname on emp(id,name); Query OK, 0 rows affected (30.87 sec)
組合索引能夠命中的查詢語句.net
mysql> select * from emp where id=10000 and name ='jack10000'; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 10000 | jack10000 | male | www.jack10000@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (0.00 sec) 從左到右原則不會命中下面的查詢語句 mysql> select * from emp where name ='jack10000'; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 10000 | jack10000 | male | www.jack10000@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (5.48 sec) mysql> select * from emp where id =10000; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 10000 | jack10000 | male | www.jack10000@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (0.00 sec)
查詢優化
索引是爲了增長查詢的效率,查詢須要注意:code
- 避免使用select 、count*這樣的操做
mysql> select name from emp where name ='jack10000'; +-----------+ | name | +-----------+ | jack10000 | +-----------+ 1 row in set (4.15 sec) mysql> select * from emp where name ='jack10000'; +-------+-----------+--------+----------------------+ | id | NAME | gender | email | +-------+-----------+--------+----------------------+ | 10000 | jack10000 | male | www.jack10000@qq.com | +-------+-----------+--------+----------------------+ 1 row in set (5.48 sec) mysql> select count(1) from emp; +----------+ | count(1) | +----------+ | 2999999 | +----------+ 1 row in set (2.47 sec) mysql> select count(*) from emp; +----------+ | count(*) | +----------+ | 2999999 | +----------+ 1 row in set (2.72 sec)
- 索引儘可能短
- 查詢條件複雜使用聯合索引