MySQL查詢優化之 index 索引的分類和使用

2022-06-06 00:57:16 字數 3280 閱讀 8368

唯一索引 (unique key)

常規索引 (key/index)

全文索引 (fulltext)

基礎語法

/*

# 方法一:建立表時

create table 表名 (

欄位名1 資料型別 [完整性約束條件…],

欄位名2 資料型別 [完整性約束條件…],

[unique | fulltext | spatial] index | key

[索引名] (欄位名[(長度)] [asc |desc])

);*/

-- 方法二:create在已存在的表上建立索引

-- create [unique | fulltext | spatial] index 索引名 on 表名 (欄位名[(長度)] [asc |desc]) ;

/*常規索引*/

-- 方法三:alter table在已存在的表上建立索引

-- alter table 表名 add [unique | fulltext | spatial] index

-- 索引名 (欄位名[(長度)] [asc |desc]) ;

/*增加全文索引*/

alter table `school`.`student` add fulltext index `studentname`(`studentname`);

-- 刪除索引:drop index 索引名 on 表名字;

-- 刪除主鍵索引: alter table 表名 drop primary key;

#顯示索引資訊: show index from student;

/*explain : 分析sql語句執行效能*/

explain select * from student where studentno='1000';

/*使用全文索引*/

-- 全文搜尋通過 match() 函式完成。

-- 搜尋字串作為 against() 的引數被給定。搜尋以忽略字母大小寫的方式執行。對於表中的每個記錄行,match() 返回乙個相關性值。即,在搜尋字串與記錄行在 match() 列表中指定的列的文字之間的相似性尺度。

explain select *from student where match(studentname) against('love');

/*開始之前,先說一下全文索引的版本、儲存引擎、資料型別的支援情況

mysql 5.6 以前的版本,只有 myisam 儲存引擎支援全文索引;

mysql 5.6 及以後的版本,myisam 和 innodb 儲存引擎均支援全文索引;

只有欄位的資料型別為 char、varchar、text 及其系列才可以建全文索引。

測試或使用全文索引時,要先看一下自己的 mysql 版本、儲存引擎和資料型別是否支援全文索引。

*/

-- 建表

-- 預設時間 default current_timestamp

`id` bigint(20) unsigned not null auto_increment,

`name` varchar(50) default '' comment '使用者暱稱',

`email` varchar(50) not null comment '使用者郵箱',

`phone` varchar(20) default '' comment '手機號',

`gender` tinyint(4) unsigned default '0' comment '性別(0:男;1:女)',

`password` varchar(100) not null comment '密碼',

`age` tinyint(4) default '0' comment '年齡',

`create_time` datetime,

`update_time` timestamp null default current_timestamp on update current_timestamp,

primary key (`id`)

-- 建立 插入一百萬條資料 的函式

delimiter $$ -- 寫函式之前的預設操作

create function mock_data()

returns int

begin

declare num int default 1000000;

declare i int default 0;

while i < num do

-- 插入資料

values(

concat('使用者', i),

'[email protected]',

concat('18', floor(rand()*(999999999-100000000)+100000000)),

floor(rand()*2),

uuid(),

floor(rand()*100),

now()

); set i = i+1;

end while;

return i;

end;

-- 呼叫函式

select mock_data();

-- 一般索引命名: id_表名_欄位名

-- 建立索引: create index 索引名 on 表(字段)

-- 測試發現快了許多

索引的資料結構

-- 我們可以在建立上述索引的時候,為其指定索引型別,分兩類

hash型別的索引:查詢單條快,範圍查詢慢

btree型別的索引:b+樹,層數越多,資料量指數級增長(我們就用它,因為innodb預設支援它)

-- 不同的儲存引擎支援的索引型別也不一樣

innodb 支援事務,支援行級別鎖定,支援 b-tree、full-text 等索引,不支援 hash 索引;

myisam 不支援事務,支援表級別鎖定,支援 b-tree、full-text 等索引,不支援 hash 索引;

memory 不支援事務,支援表級別鎖定,支援 b-tree、hash 等索引,不支援 full-text 索引;

ndb 支援事務,支援行級別鎖定,支援 hash 索引,不支援 b-tree、full-text 等索引;

archive 不支援事務,支援表級別鎖定,不支援 b-tree、hash、full-text 等索引;

MySQL查詢優化之 index 索引的分類和使用

唯一索引 unique key 常規索引 key index 全文索引 fulltext 基礎語法 方法一 建立表時 create table 表名 欄位名1 資料型別 完整性約束條件 欄位名2 資料型別 完整性約束條件 unique fulltext spatial index key 索引名 欄...

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

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

MySQL查詢優化之COUNT

count 聚合函式,以及如何優化使用了該函式的查詢,很可能是mysql中最容易被誤解的前10個話題之一,在網上隨便搜尋一下就能看到很多錯誤的理解,可能比我們想象的多得多。在做優化之前,先來看看count 函式的真正作用是什麼。count 的作用 count 的另外乙個作用是統計結果集的行數。當my...