一,索引的重要性
索引用于快速找出在某個列中有一特定值的行。不使用索引,MySQL必須從第1條記錄開始然后讀完整個表直到找出相關的行。表越大,花費的時間越多。如果表中查詢的列有一個索引,MySQL能快速到達一個位置去搜尋到數據文件的中間,沒有必要看所有數據。注意如果你需要訪問大部分行,順序讀取要快得多,因為此時我們避免磁盤搜索。
假如你用新華字典來查找“張”這個漢字,不使用目錄的話,你可能要從新華字典的第一頁找到最后一頁,可能要花二個小時。字典越厚呢,你花的時間就越多。現在你使用目錄來查找“張”這個漢字,張的首字母是z,z開頭的漢字從900多頁開始,有了這條線索,你查找一個漢字可能只要一分鐘,由此可見索引的重要性。但是索引建的是不是越多越好呢,當然不是,如果一本書的目錄分成好幾級的話,我想你也會暈的。
二,準備工作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
//準備二張測試表 -> `id` int (11) NOT NULL auto_increment, -> `num` int (11) NOT NULL default '0' , -> `d_num` varchar (30) NOT NULL default '0' , -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; Query OK, 0 rows affected (0.05 sec) mysql> CREATE TABLE `test_test` ( -> `id` int (11) NOT NULL auto_increment, -> `num` int (11) NOT NULL default '0' , -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; Query OK, 0 rows affected (0.05 sec) //創建一個存儲過程,為插數據方便 mysql> delimiter | mysql> create procedure i_test(pa int (11),tab varchar (30)) -> begin -> declare max_num int (11) default 100000; -> declare i int default 0; -> declare rand_num int ; -> declare double_num char ; -> -> if tab != 'test_test' then -> select count (id) into max_num from test_t; -> while i < pa do -> if max_num < 100000 then -> select cast (rand()*100 as unsigned) into rand_num; -> select concat(rand_num,rand_num) into double_num; -> insert into test_t(num,d_num) values (rand_num,double_num); -> end if; -> set i = i +1; -> end while; -> else -> select count (id) into max_num from test_test; -> while i < pa do -> if max_num < 100000 then -> select cast (rand()*100 as unsigned) into rand_num; -> insert into test_test(num) values (rand_num); -> end if; -> set i = i +1; -> end while; -> end if; -> end | Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> show variables like "%pro%" ; //查看一下,記錄執行的profiling是不是開啟動了,默認是不開啟的 + ---------------------------+-------+ | Variable_name | Value | + ---------------------------+-------+ | profiling | OFF | | profiling_history_size | 15 | | protocol_version | 10 | | slave_compressed_protocol | OFF | + ---------------------------+-------+ 4 rows in set (0.00 sec) mysql> set profiling=1; //開啟后,是為了對比加了索引后的執行時間 Query OK, 0 rows affected (0.00 sec) |
三,實例
1,單表數據太少,索引反而會影響速度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
mysql> call i_test(10, 'test_t' ); //向test_t表插入10條件 Query OK, 1 row affected (0.02 sec) mysql> select num from test_t where num!=0; mysql> explain select num from test_t where num!=0\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table : test_t type: ALL possible_keys: NULL key : NULL key_len: NULL ref: NULL rows : 10 Extra: Using where 1 row in set (0.00 sec) ERROR: No query specified mysql> create index num_2 on test_t (num); Query OK, 10 rows affected (0.19 sec) Records: 10 Duplicates: 0 Warnings: 0 mysql> select num from test_t where num!=0; mysql> explain select num from test_t where num!=0\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table : test_t type: index possible_keys: num_2 key : num_2 key_len: 4 ref: NULL rows : 10 Extra: Using where ; Using index 1 row in set (0.00 sec) ERROR: No query specified mysql> show profiles; + ----------+------------+---------------------------------------------+ | Query_ID | Duration | Query | + ----------+------------+---------------------------------------------+ | 1 | 0.00286325 | call i_test(10, 'test_t' ) | //插入十條數據 | 2 | 0.00026350 | select num from test_t where num!=0 | | 3 | 0.00022250 | explain select num from test_t where num!=0 | | 4 | 0.18385400 | create index num_2 on test_t (num) | //創建索引 | 5 | 0.00127525 | select num from test_t where num!=0 | //使用索引后,差不多是沒有使用索引的0.2倍 | 6 | 0.00024375 | explain select num from test_t where num!=0 | + ----------+------------+---------------------------------------------+ 6 rows in set (0.00 sec) |
解釋:
- id:表示sql執行的順序
- select_type:SIMPLE,PRIMARY,UNION,DEPENDENT UNION,UNION RESULT,SUBQUERY,DEPENDENT SUBQUERY,DERIVED不同的查詢語句會有不同的select_type
- table:表示查找的表名
- type:表示使用索引類型,或者有無使用索引.效率從高到低const、eq_reg、ref、range、index和ALL,其實這個根你sql的寫法有直接關系,例如:能用主鍵就用主鍵,where后面的條件加上索引,如果是唯一加上唯一索引等
- possible_keys:可能存在的索引
- key:使用索引
- key_len:使用索引的長度
- ref:使用哪個列或常數與key一起從表中選擇行,一般在多表聯合查詢時會有。
- rows:查找出的行數
- Extra:額外說明
前段時間寫過一篇博文mysql distinct和group by誰更好,里面有朋友留言,說測試結果根我當時做的測試結果不一樣,當時我打比方解釋了一下,今天有時間,以例子的形勢,更直觀的表達出索引的工作原理。
2,where后的條件,order by ,group by 等這樣過濾時,后面的字段最好加上索引。根據實際情況,選擇PRIMARY KEY、UNIQUE、INDEX等索引,但是不是越多越好,要適度。
3,聯合查詢,子查詢等多表操作時關連字段要加索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
mysql> call i_test(10, 'test_test' ); //向test_test表插入10條數據 Query OK, 1 row affected (0.02 sec) mysql> explain select a.num as num1,b.num as num2 from test_t as a left join tes t_test as b on a.num=b.num\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table : a type: index possible_keys: NULL key : num_2 key_len: 4 ref: NULL rows : 10 Extra: Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table : b type: ref possible_keys: num_1 key : num_1 key_len: 4 ref: bak_test.a.num //bak_test是數據庫名,a.num是test_t的一個字段 rows : 1080 Extra: Using index 2 rows in set (0.01 sec) ERROR: No query specified |
數據量特別大的時候,最好不要用聯合查詢,即使你做了索引。
上面只是個人的一點小結,拋磚引玉一下。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.51yip.com/mysql/1157.html