五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

簡(jiǎn)單了解添加mysql索引的3條原則

發(fā)布日期:2022-03-10 13:15 | 文章來源:gibhub

一,索引的重要性

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

假如你用新華字典來查找“張”這個(gè)漢字,不使用目錄的話,你可能要從新華字典的第一頁找到最后一頁,可能要花二個(gè)小時(shí)。字典越厚呢,你花的時(shí)間就越多?,F(xiàn)在你使用目錄來查找“張”這個(gè)漢字,張的首字母是z,z開頭的漢字從900多頁開始,有了這條線索,你查找一個(gè)漢字可能只要一分鐘,由此可見索引的重要性。但是索引建的是不是越多越好呢,當(dāng)然不是,如果一本書的目錄分成好幾級(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ǔ)過程,為插數(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是不是開啟動(dòng)了,默認(rèn)是不開啟的 
+---------------------------+-------+ 
| 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; //開啟后,是為了對(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 | //使用索引后,差不多是沒有使用索引的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不同的查詢語句會(huì)有不同的select_type
  • table:表示查找的表名
  • type:表示使用索引類型,或者有無使用索引.效率從高到低const、eq_reg、ref、range、index和ALL,其實(shí)這個(gè)根你sql的寫法有直接關(guān)系,例如:能用主鍵就用主鍵,where后面的條件加上索引,如果是唯一加上唯一索引等
  • possible_keys:可能存在的索引
  • key:使用索引
  • key_len:使用索引的長(zhǎng)度
  • ref:使用哪個(gè)列或常數(shù)與key一起從表中選擇行,一般在多表聯(lián)合查詢時(shí)會(huì)有。
  • rows:查找出的行數(shù)
  • Extra:額外說明

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

2,where后的條件,order by ,group by 等這樣過濾時(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ù)庫名,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í)有所幫助,也希望大家多多支持本站。

美國(guó)穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部