日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

如何快速上手mysql_mysql快速上手3

發(fā)布時間:2025/3/15 数据库 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何快速上手mysql_mysql快速上手3 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上一章給大家說的是數(shù)據(jù)庫的視圖,存儲過程等等操作,這章主要講索引,以及索引注意事項(xiàng),如果想看前面的文章,url如下:

索引簡介

索引是對數(shù)據(jù)庫表中一個或多個列(例如,employee 表的姓名 (name) 列)的值進(jìn)行排序的結(jié)構(gòu)。如果想按特定職員的姓來查找他或她,則與在表中搜索所有的行相比,索引有助于更快地獲取信息。

例如這樣一個查詢:select * from table1 where id=10000。如果沒有索引,必須遍歷整個表,直到ID等于10000的這一行被找到為止;有了索引之后(必須是在ID這一列上建立的索引),即可在索引中查找。由于索引是經(jīng)過某種算法優(yōu)化過的,因而查找次數(shù)要少的多。可見,索引插敘的速度要比沒有索引的速度要快很多

MySQL中常見索引有:

普通索引

唯一索引

主鍵索引

組合索引

下面就應(yīng)用一下索引吧

索引操作

一、普通索引(index)

普通所以只有一個功能,就是加快查找速度。操作如下

1、先創(chuàng)建一個表

create table tab1(

nid int not nullauto_increment primary key,

name varchar(32) not null,

email varchar(64) not null,

extra text,

index ix_name (name)

)

2、創(chuàng)建索引

create index 索引名稱 on 表名(列名)

3、刪除索引

drop 索引名稱 on 表名;

4、查看索引

show index from 表名;

5、注意事項(xiàng)(對于創(chuàng)建索引時如果是BLOB?和?TEXT?類型,必須指定length。)

create index index_name on tab1(extra(32));

二、唯一索引(unique)

唯一性索引unique index和一般索引normal index最大的差異就是在索引列上增加了一層唯一約束。添加唯一性索引的數(shù)據(jù)列可以為空,但是只要存在數(shù)據(jù)值,就必須是唯一的。

1、創(chuàng)建表+唯一索引

create table tab2(

nid int not nullauto_increment primary key,

name varchar(32) not null,

email varchar(64) not null,

extra text,

unique ix_name (name) --重點(diǎn)在這里

)

2、創(chuàng)建索引

create unique index 索引名 on 表名(列名)

3、刪除索引

drop unique index 索引名 on 表名

三、主鍵索引

在數(shù)據(jù)庫關(guān)系圖中為表定義一個主鍵將自動創(chuàng)建主鍵索引,主鍵索引是唯一索引的特殊類型。主鍵索引要求主鍵中的每個值是唯一的。當(dāng)在查詢中使用主鍵索引時,它還允許快速訪問數(shù)據(jù)。數(shù)據(jù)不能為空

1、創(chuàng)建表+主鍵索引

create table in1(

nid int not nullauto_increment,

name varchar(32) not null,

email varchar(64) not null,

extra text,

primary key(nid),

index zhang (name)

)

2、創(chuàng)建主鍵

alter table 表名 add primary key(列名);

3、刪除主鍵

alter table 表名 drop primary key;

alter table 表名 modify 列名 int, drop primary key;

四、組合索引

組合索引,就是組合查詢的意思嘛嘻嘻,將兩列或者多列組合成一個索引進(jìn)行查詢

其應(yīng)用場景為:頻繁的同時使用n列來進(jìn)行查詢,如:where name = '張巖林' and email = 666。

1、創(chuàng)建表

create table in3(

nid int not nullauto_increment primary key,

name varchar(32) not null,

email varchar(64) not null,

extra text

)

2、創(chuàng)建組合索引

create index ix_name_email on in3(name,email);

如上創(chuàng)建組合索引之后,查詢有的會使用索引,有的不會:

name and email ?-- 使用索引

name ? ? ? ? ? ? ? ? -- 使用索引

email ? ? ? ? ? ? ? ? -- 不使用索引

索引注意事項(xiàng)

1、正確使用索引

數(shù)據(jù)庫表中添加索引后能夠讓查詢數(shù)據(jù)庫速度飛快,但前提必須是正確的使用索引來查詢,如果以錯誤的方式使用,則即使建立索引也會不奏效。

下面這些情況不會使用到索引:

1、like '%xx'

select * from tb1 where name like '%cn';

2、使用函數(shù)

select * from tb1 where reverse(name) = '張巖林';

3、or

select * from tb1 where nid = 1 or email='zhangyanlin@live.com';

特別的:當(dāng)or條件中有未建立索引的列才失效,以下會走索引

select * from tb1 where nid = 1 or name = 'zhangyanlin';

select * from tb1 where nid = 1 or email = 'zhangyanlin@live.com' and name = 'aylin'

4、類型不一致

如果列是字符串類型,傳入條件是必須用引號引起來,不然...

select * from tb1 where name = 999;

5、 !=

select * from tb1 where name != 'aylin'特別的:如果是主鍵,則還是會走索引

select * from tb1 where nid != 123

6、 >

select * from tb1 where name > 'alex'特別的:如果是主鍵或索引是整數(shù)類型,則還是會走索引

select * from tb1 where nid > 123

select * from tb1 where num > 123

7、order by

select email fromtb1 order by name desc;

當(dāng)根據(jù)索引排序時候,選擇的映射如果不是索引,則不走索引

特別的:如果對主鍵排序,則還是走索引:

select * fromtb1 order by nid desc;

8、 組合索引最左前綴

如果組合索引為:(name,email)

name and email --使用索引

name --使用索引

email -- 不使用索引

2、其他注意事項(xiàng)

避免使用select?*

3、執(zhí)行計(jì)劃

explain + 查詢SQL - 用于顯示SQL執(zhí)行信息參數(shù),根據(jù)參考信息可以進(jìn)行SQL優(yōu)化

mysql> explain select * fromtb2;

+----+-------------+-------+------+---------------+------+---------+------+------+-------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-------+------+---------------+------+---------+------+------+-------+

| 1 | SIMPLE | tb2 | ALL | NULL | NULL | NULL | NULL | 2 | NULL |

+----+-------------+-------+------+---------------+------+---------+------+------+-------+

1 row in set (0.00 sec)

id

查詢順序標(biāo)識

如:mysql> explain select * from (select nid,name from tb1 where nid < 10) asB;

+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+

| 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 9 | NULL |

| 2 | DERIVED | tb1 | range | PRIMARY | PRIMARY | 8 | NULL | 9 | Using where |

+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+特別的:如果使用union連接氣值可能為null

select_type

查詢類型

SIMPLE 簡單查詢

PRIMARY 最外層查詢

SUBQUERY 映射為子查詢

DERIVED 子查詢

UNION 聯(lián)合

UNION RESULT 使用聯(lián)合的結(jié)果

...

table

正在訪問的表名

type

查詢時的訪問方式,性能:all < index < range < index_merge < ref_or_null < ref < eq_ref < system/constALL 全表掃描,對于數(shù)據(jù)表從頭到尾找一遍

select * fromtb1;

特別的:如果有l(wèi)imit限制,則找到之后就不在繼續(xù)向下掃描

select * from tb1 where email = 'seven@live.com'

select * from tb1 where email = 'seven@live.com' limit 1;

雖然上述兩個語句都會進(jìn)行全表掃描,第二句使用了limit,則找到一個后就不再繼續(xù)掃描。

INDEX 全索引掃描,對索引從頭到尾找一遍

select nid fromtb1;

RANGE 對索引列進(jìn)行范圍查找

select * from tb1 where name < 'alex';

PS:

between and

in

> >= < <=操作

注意:!= 和 >符號

INDEX_MERGE 合并索引,使用多個單列索引搜索

select * from tb1 where name = 'alex' or nid in (11,22,33);

REF 根據(jù)索引查找一個或多個值

select * from tb1 where name = 'seven';

EQ_REF 連接時使用primary key 或 unique類型

select tb2.nid,tb1.name from tb2 left join tb1 on tb2.nid =tb1.nid;

CONST 常量

表最多有一個匹配行,因?yàn)閮H有一行,在這行的列值可被優(yōu)化器剩余部分認(rèn)為是常數(shù),const表很快,因?yàn)樗鼈冎蛔x取一次。

select nid from tb1 where nid = 2;

SYSTEM 系統(tǒng)

表僅有一行(=系統(tǒng)表)。這是const聯(lián)接類型的一個特例。

select * from (select nid from tb1 where nid = 1) asA;

possible_keys

可能使用的索引

key

真實(shí)使用的

key_len

MySQL中使用索引字節(jié)長度

rows

mysql估計(jì)為了找到所需的行而要讀取的行數(shù) ------只是預(yù)估值

extra

該列包含MySQL解決查詢的詳細(xì)信息

“Using index”

此值表示mysql將使用覆蓋索引,以避免訪問表。不要把覆蓋索引和index訪問類型弄混了。

“Using where”

這意味著mysql服務(wù)器將在存儲引擎檢索行后再進(jìn)行過濾,許多where條件里涉及索引中的列,當(dāng)(并且如果)它讀取索引時,就能被存儲引擎檢驗(yàn),因此不是所有帶where子句的查詢都會顯示“Using where”。有時“Using where”的出現(xiàn)就是一個暗示:查詢可受益于不同的索引。

“Using temporary”

這意味著mysql在對查詢結(jié)果排序時會使用一個臨時表。

“Using filesort”

這意味著mysql會對結(jié)果使用一個外部索引排序,而不是按索引次序從表里讀取行。mysql有兩種文件排序算法,這兩種排序方式都可以在內(nèi)存或者磁盤上完成,explain不會告訴你mysql將使用哪一種文件排序,也不會告訴你排序會在內(nèi)存里還是磁盤上完成。

“Range checked foreach record(index map: N)”

這個意味著沒有好用的索引,新的索引將在聯(lián)接的每一行上重新估算,N是顯示在possible_keys列中索引的位圖,并且是冗余的。

4、limit分頁

分頁功能是個值得關(guān)注的問題,因?yàn)槲覀儠恢庇玫?/p>

每頁顯示10條:

當(dāng)前 118 120, 125倒序:

大 小

980 970 7 6 6 5 54 43 32

21 19 98下一頁:

select

*

fromtb1

wherenid < (select nid from (select nid from tb1 where nid < 當(dāng)前頁最小值 order by nid desc limit 每頁數(shù)據(jù) *【頁碼-當(dāng)前頁】) A order by A.nid asc limit 1)

order by

nid desc

limit 10;

select

*

fromtb1

wherenid < (select nid from (select nid from tb1 where nid < 970 order by nid desc limit 40) A order by A.nid asc limit 1)

order by

nid desc

limit 10;

上一頁:

select

*

fromtb1

wherenid < (select nid from (select nid from tb1 where nid > 當(dāng)前頁最大值 order by nid asc limit 每頁數(shù)據(jù) *【當(dāng)前頁-頁碼】) A order by A.nid asc limit 1)

order by

nid desc

limit 10;

select

*

fromtb1

wherenid < (select nid from (select nid from tb1 where nid > 980 order by nid asc limit 20) A order by A.nid desc limit 1)

order by

nid desc

limit 10;

轉(zhuǎn)載:http://www.cnblogs.com/aylin/p/5777289.html

總結(jié)

以上是生活随笔為你收集整理的如何快速上手mysql_mysql快速上手3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。