程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL索引和查詢優化的實際操作(1)

MySQL索引和查詢優化的實際操作(1)

編輯:MySQL綜合教程

以下的文章主要介紹的是MySQL索引和查詢優化的實際操作流程,我們大家都知道MySQL索引和查詢優化在實際操作中出現的比例較高,所以對其有更深的了解會在你今後的學習中有所收獲所。

恰當的索引可以加快查詢速度,可以分為四種類型:主鍵、唯一索引、全文索引、普通索引。

主鍵:唯一且沒有null值。

  1. create table pk_test(f1 int not null,primary key(f1));  
  2. alter table customer modify id int not null, add primary key(id); 

普通索引:允許重復的值出現。

  1. create table tableanme (fieldname1 columntype,fieldname2 columntype,index [indexname] (fieldname1 [,fieldname2...]));  
  2. create table tablename add index [indexname] (fieldname1 [fieldname2...]);  
  3. alter table slaes add index(value); 

全文MySQL索引:用來對大表的文本域(char,varchar,text)進行索引。

語法和普通索引一樣-fulltext。

使用全文索引:create table ft2 (f1 varchar(255),fulltext(f1));

insert into ft2 values('wating for the bvarbariands'),('in the heart of the country'),('the master of petersburg'),('writing and being'),('heart of the beast'),('master master');

select * from ft2 where match(f1) against('master'); // match()-匹配域;against()匹配值。

MySQL會對某些字忽略,造成查詢的誤差:a. 50%以上的域出現的單詞;b.少於三個字的單詞;c.MySQL預定義的列表,包括the。查詢語句:select * from ft2 where match(f1) against('the master'); // 與希望的結果是不同的

相關性分數查詢:select f1,(match(f1) against('master')) from ft2;

MySQL4的新功能-布爾全文查詢:select * from ft2 where match(f1) against('+master -pet' in boolean mode); // 運算符類型 +-<>()~*"

唯一索引:除了不能有重復的記錄外,其它和普通索引一樣。

create table ui_test (f1 int,f2 int,unique(f1));

alter table ui_test add unique(f2);

對域(varchar,char,blob,text)的部分創建MySQL索引:alter table customer add index (surname(10));

自動增加域:每次插入記錄時會自動增加一個域的值,只能用於一個域,且這個域有索引。

create table tablename(fieldname int auto_increment,[fieldname2...,] primary key(filedname));

alter table tablename modify fieldname columntype auto_increment;

last_insert_id()函數返回最新插入的自動增加值。

select last_insert_id() from customer limit 1;

此函數在多個連接同時進行時,會發生錯誤。

重置自動增加計數器的值:

create table tablename(fieldname int auto_increment,[fieldname2...,] primary key(filedname) auto_increment=50);

alter table tablename auto_increment=50;

如果重置的值比存在的值小,自動增加計數器會從記錄中最大的那個值開始增加計數,比如customer表中的id已經有1、2、3、15、16、20,當把自動增加計數器的值設為1時,下次插入的記錄會從21開始。

自動增加計數器的越界:有效值為1~2的127次方,即2147483647。如果超過這個值(包括負值),MySQL會自動把它設為最大值,這樣就會產生一個重復鍵值的錯誤。

自動增加域在多列MySQL索引中的使用:

create table staff(rank enum('employee','manager','contractor') not null,position varchar(100),id int not null auto_increment,primary key(rank,id));

insert into staff(rank,position) values('employee','cleaner'),('cotractor','network maintenance'),('manager','sales manager');

在對每個級別添加一些數據,會看到熟悉的自動增加現象:

insert into staff(rank,position) values('employee','cleaner1'),('employee','network maintenance1'),('manager','sales manager1');

在這種情況下是不能重置自動增加計數器的。

刪除或更改索引:對索引的更改都需要先刪除再重新定義。

alter table tablename drop primary key;

alter table table drop index indexname;

drop index on tablename;

高效使用索引:下面討論的是用了索引會給我們帶來什麼?

1.) 獲得域where從句中匹配的行:select * from customer where surname>'c';

2.) 查找max()和min()值時,MySQL只需在排序的索引中查找第一個和最後一個值。

3.) 返回的部分是MySQL索引的一部分,MySQL就不需要去查詢全表的數據而只需看索引:select id from custo及mer;

4.) 對域使用order by的地方:select * from customer order by surname;

5.) 還可以加速表的連接:select first_name,surname,commission from sales,sales_rep where sales.sales_rep=sales_rep.employee_number and code=8;

6.) 在通配符的情況下:select * from sales_rep where surname like 'ser%';

這種情況就不能起作用:select * from sales_rep where surname like '%ser%';


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved