MySQL優化 之 索引

2021-06-20 01:49:36 字數 3159 閱讀 1119

四種索引(主鍵索引,唯一索引,普通索引,全文索引)

【對查詢語句會提高效率】

【對增刪改語句會降低效率,因為還要對索引進行增刪改!】

【建立索引會佔磁碟空間】

【對頻繁查詢的字段應建立索引,對頻繁更新的字段不適合建立索引】

1、新增

1.1------------------主鍵索引新增------------------

@1.當一張表,把某個列設為主鍵的時候,則該列就是主鍵索引

create table emp(

id int primary key auto_increment,

name varchar(32) not null default ''

);這裡id列就是主鍵索引

@2.建立表時沒有指定主鍵,則

alter table emp add primary key(empno);

1.2------------------普通索引新增------------------

一般來說,普通索引是在建立表後,再建立普通索引的

比如:create table aaa(

id int,

name varchar(32)

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

create index my_index on aaa(name);

1.3------------------唯一索引新增------------------

@1.建表時,建立唯一索引

create table ddd(

id int primary key auto_increment,

name varchar(32) unique

);insert into ddd value (1,'sss');

insert into ddd value (2,'sss'); 【這裡拒絕插入!因為新增了unique唯一索引】

insert into ddd value (3,null);

insert into ddd value (4,null); 【但這裡可以,允許多個null】

@2.建表後,建立唯一索引

create table eee(

id int primary key auto_increment,

name varchar(32)

);create unique index my_unique on eee(name);

1.4------------------全文索引新增------------------

主要是針對對檔案、文字的檢索,比如文章,

【且只針對myisam有效!】

建立方法,建表時建立

create table articles (

id int  auto_increment not null primary key,

title varchar(200),

content text,

fulltext (title,content)

)engine  =  myisam  character  set utf8;

插入資料作測試

insert into articles (title,content) values

('mysql tutorial','dbms stands for database ...'),

('how to use mysql well','after you went through a ...'),

('optimizing mysql','in this tutorial we will show ...'),

('1001 mysql tricks','1. never run mysqld as root. 2. ...'),

('mysql vs. yoursql','in the following database comparison ...'),

('mysql security','when configured properly, mysql ...');

如何使用全文索引

錯誤用法:

select * from articles where content like '%mysql%';   【沒有用到全文索引!】

正確用法:

select * from articles where match(title,content) against('configured');

select * from articles where match(title,content) against('database');

select * from articles where match(title,content) against('root');

select match(title,content) against('root') from articles; #可以檢視匹配度

全文索引說明:

1.fulltext索引只對myisam有效

2.fulltext針對英文有效【中文的話要用sphinx,所以fulltext索引了解即可】

3.全文索引乙個 叫 停止詞,就是對一些詞不會建立全文索引的,就叫停止詞

例如select * from articles where match(title,content) against('a');

select * from articles where match(title,content) against('mysql');

返回結果都為空,因為這些詞出現頻繁,如果都建立全文索引,

那麼索引會無窮大,不科學!這些就叫停止詞。

2、查詢

如下幾種方法:

desc emp;   (缺點:不能顯示索引名)

show index from emp\g

show indexes from emp\g

show keys from emp\g

3、刪除

-----------刪除索引-----------

drop index 索引名 on 表名;

或者alter table 表名 drop index 索引名;

-----------刪除主鍵-----------

alter table 表名 drop primary key; 

4、修改

先刪除,再建立索引!

Mysql優化之索引

索引其實就是乙個檔案,它與mysql資料檔案不一樣的地方是 它是順序的儲存資料,檔案小且儲存的位置也不一樣 索引能加快檢索,但系統每一次維護資料 寫入 更新 的同時也需要維護索引,帶來額外的開銷。索引按照底層實現方式分為 b樹索引 r樹索引 雜湊索引等 索引按照具體表現分為 主鍵索引 primary...

效能優化之mysql索引優化

sql及索引優化 如何通過慢查詢日誌發現有問題的sql?查詢次數多且每次查詢占用時間長的sql 通常為pt query digest分析的前幾個查詢 io大的sql 注意pt query digest分析中的rows examine項 未命中索引的sql 注意pt query digest分析中ro...

mysql 索引優化器 Mysql之查詢優化器

對於乙個sql語句,查詢優化器先看是不是能轉換成join,再將join進行優化 優化分為 1.條件優化 2.計算全表掃瞄成本 3.找出所有能用到的索引 4.針對每個索引計算不同的訪問方式的成本 5.選出成本最小的索引以及訪問方式 開啟查詢優化器日誌 開啟 set optimizer trace en...