欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

簡單了解添加mysql索引的3條原則

一,索引的重要性

平武網(wǎng)站建設公司創(chuàng)新互聯(lián)公司,平武網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為平武上千多家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的平武做網(wǎng)站的公司定做!

索引用于快速找出在某個列中有一特定值的行。不使用索引,MySQL必須從第1條記錄開始然后讀完整個表直到找出相關的行。表越大,花費的時間越多。如果表中查詢的列有一個索引,MySQL能快速到達一個位置去搜尋到數(shù)據(jù)文件的中間,沒有必要看所有數(shù)據(jù)。注意如果你需要訪問大部分行,順序讀取要快得多,因為此時我們避免磁盤搜索。

假如你用新華字典來查找“張”這個漢字,不使用目錄的話,你可能要從新華字典的第一頁找到最后一頁,可能要花二個小時。字典越厚呢,你花的時間就越多?,F(xiàn)在你使用目錄來查找“張”這個漢字,張的首字母是z,z開頭的漢字從900多頁開始,有了這條線索,你查找一個漢字可能只要一分鐘,由此可見索引的重要性。但是索引建的是不是越多越好呢,當然不是,如果一本書的目錄分成好幾級的話,我想你也會暈的。

二,準備工作

//準備二張測試表 
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)建一個存儲過程,為插數(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是不是開啟動了,默認是不開啟的 
+---------------------------+-------+ 
| 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; //開啟后,是為了對比加了索引后的執(zhí)行時間 
Query OK, 0 rows affected (0.00 sec) 

三,實例

1,單表數(shù)據(jù)太少,索引反而會影響速度

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 | //使用索引后,差不多是沒有使用索引的0.2倍 
| 6 | 0.00024375 | explain select num from test_t where num!=0 | 
+----------+------------+---------------------------------------------+ 
6 rows in set (0.00 sec) 

解釋:

  • id:表示sql執(zhí)行的順序
  • 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:使用哪個列或常數(shù)與key一起從表中選擇行,一般在多表聯(lián)合查詢時會有。
  • rows:查找出的行數(shù)
  • Extra:額外說明

前段時間寫過一篇博文mysql distinct和group by誰更好,里面有朋友留言,說測試結(jié)果根我當時做的測試結(jié)果不一樣,當時我打比方解釋了一下,今天有時間,以例子的形勢,更直觀的表達出索引的工作原理。

2,where后的條件,order by ,group by 等這樣過濾時,后面的字段最好加上索引。根據(jù)實際情況,選擇PRIMARY KEY、UNIQUE、INDEX等索引,但是不是越多越好,要適度。

3,聯(liá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ù)庫名,a.num是test_t的一個字段 
rows: 1080 
Extra: Using index 
2 rows in set (0.01 sec) 
ERROR: 
No query specified 

數(shù)據(jù)量特別大的時候,最好不要用聯(lián)合查詢,即使你做了索引。

上面只是個人的一點小結(jié),拋磚引玉一下。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享標題:簡單了解添加mysql索引的3條原則
當前地址:http://aaarwkj.com/article22/gpegjc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、做網(wǎng)站手機網(wǎng)站建設、服務器托管、搜索引擎優(yōu)化、網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
国产精品美女自拍视频| 日本人妻伦理在线播放| 内射嫩国产欧美国产日韩欧美| 欧美一区二区三区人妻激情| 禁止18岁以下的视频| 狠狠综合久久av一区二区大宝| 国产黄色一区二区三区,| 日本熟妇中文字幕三级久久| 色呦呦视频在线免费看| 91精品国产91久久综合桃花| 日本一区二区高清在线观看| 久久re这里只有精品6| 国内精品自产拍久久久久久久久91| 欧美日韩国产一区二区三区在线观看| 五月婷婷丁香视频在线| 巨乳人妻一区二区三区| 亚洲精品国产av一区二区三区| 自拍偷拍亚洲丝袜大秀| 啄木乌法国一区二区三区 | 亚洲精品一区二区三区香蕉| 精品福利视频一区二区| 高颜值美女后入内射视频| 91美女人妻精品久久| 日本在线观看精品综合| 国产精品亚洲一区二区在| 丝袜美腿亚洲综合一区| 风间由美亚洲一区二区三区| 成人av免费高清在线播放| 麻豆精东传媒一区二区| 久久免费看少妇高潮av| av资源网大全手机在线观看| 国产免费av一区二区在线观看 | 日韩欧美黄片一区二区三区| 亚洲黄色录像特级生活片| 欧美一区二区在线精品| 亚洲精品在线观看日韩欧美| 日本韩国三级伦理在线观看| 黑丝美女国产精品久久久| 偷窥偷拍原味一区二区三区| 白白色成人在线免费视频| 91精品在线观看第一页|