一,索引的重要性
平武網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,平武網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為平武上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的平武做網(wǎng)站的公司定做!
索引用于快速找出在某個(gè)列中有一特定值的行。不使用索引,MySQL必須從第1條記錄開(kāi)始然后讀完整個(gè)表直到找出相關(guān)的行。表越大,花費(fèi)的時(shí)間越多。如果表中查詢的列有一個(gè)索引,MySQL能快速到達(dá)一個(gè)位置去搜尋到數(shù)據(jù)文件的中間,沒(méi)有必要看所有數(shù)據(jù)。注意如果你需要訪問(wèn)大部分行,順序讀取要快得多,因?yàn)榇藭r(shí)我們避免磁盤(pán)搜索。
假如你用新華字典來(lái)查找“張”這個(gè)漢字,不使用目錄的話,你可能要從新華字典的第一頁(yè)找到最后一頁(yè),可能要花二個(gè)小時(shí)。字典越厚呢,你花的時(shí)間就越多。現(xiàn)在你使用目錄來(lái)查找“張”這個(gè)漢字,張的首字母是z,z開(kāi)頭的漢字從900多頁(yè)開(kāi)始,有了這條線索,你查找一個(gè)漢字可能只要一分鐘,由此可見(jiàn)索引的重要性。但是索引建的是不是越多越好呢,當(dāng)然不是,如果一本書(shū)的目錄分成好幾級(jí)的話,我想你也會(huì)暈的。
二,準(zhǔn)備工作
//準(zhǔn)備二張測(cè)試表 mysql> CREATE TABLE `test_t` ( -> `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) //創(chuàng)建一個(gè)存儲(chǔ)過(guò)程,為插數(shù)據(jù)方便 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%"; //查看一下,記錄執(zhí)行的profiling是不是開(kāi)啟動(dòng)了,默認(rèn)是不開(kāi)啟的 +---------------------------+-------+ | 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; //開(kāi)啟后,是為了對(duì)比加了索引后的執(zhí)行時(shí)間 Query OK, 0 rows affected (0.00 sec)
三,實(shí)例
1,單表數(shù)據(jù)太少,索引反而會(huì)影響速度
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') | //插入十條數(shù)據(jù) | 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) | //創(chuàng)建索引 | 5 | 0.00127525 | select num from test_t where num!=0 | //使用索引后,差不多是沒(méi)有使用索引的0.2倍 | 6 | 0.00024375 | explain select num from test_t where num!=0 | +----------+------------+---------------------------------------------+ 6 rows in set (0.00 sec)
解釋:
前段時(shí)間寫(xiě)過(guò)一篇博文mysql distinct和group by誰(shuí)更好,里面有朋友留言,說(shuō)測(cè)試結(jié)果根我當(dāng)時(shí)做的測(cè)試結(jié)果不一樣,當(dāng)時(shí)我打比方解釋了一下,今天有時(shí)間,以例子的形勢(shì),更直觀的表達(dá)出索引的工作原理。
2,where后的條件,order by ,group by 等這樣過(guò)濾時(shí),后面的字段最好加上索引。根據(jù)實(shí)際情況,選擇PRIMARY KEY、UNIQUE、INDEX等索引,但是不是越多越好,要適度。
3,聯(lián)合查詢,子查詢等多表操作時(shí)關(guān)連字段要加索引
mysql> call i_test(10,'test_test'); //向test_test表插入10條數(shù)據(jù) 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是數(shù)據(jù)庫(kù)名,a.num是test_t的一個(gè)字段 rows: 1080 Extra: Using index 2 rows in set (0.01 sec) ERROR: No query specified
數(shù)據(jù)量特別大的時(shí)候,最好不要用聯(lián)合查詢,即使你做了索引。
上面只是個(gè)人的一點(diǎn)小結(jié),拋磚引玉一下。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享標(biāo)題:簡(jiǎn)單了解添加mysql索引的3條原則
當(dāng)前地址:http://www.chinadenli.net/article22/gpegjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、做網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、服務(wù)器托管、搜索引擎優(yōu)化、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)