MySQL 索引介紹及索引的分類 索引原則

2021-10-21 01:42:40 字數 3280 閱讀 2860

-- 索引的使用

-- 1.在建立表的時候給字段增加索引.

-- 2.建立表完畢後,增加索引.

use school;

show

index

from student;

-- 顯示所有的索引資訊

-- 增加乙個全文索引 (索引名) 列名

alter

table

`student`

add fulltext index

`studentname`

(`studentname`);

-- explain 分析sql執行的狀況

explain

select

*from student;

-- 非全文索引

explain

select

*from student where

match

(`studentname`

) against(

"劉")

;

插入100萬條資料

create

table

(`id`

bigint(20

)unsigned

notnull

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

default

current_timestamp

,`update_time`

timestamp

notnull

default

current_timestamp

onupdate

current_timestamp

,primary

key(

`id`))

engine

=innodb

default

charset

=utf8 comment=;

-- 插入100萬資料.

delimiter $$

create

function mock_data (

)returns

intdeterministic

begin

declare num int

default

1000000

;declare i int

default0;

while

i < num do

insert

`name`

,`email`

,`phone`

,`gender`

,`password`

,`age`

)values

( concat(

'使用者'

, i)

,'[email protected]'

, concat(

'18'

, floor(

rand()*

(999999999

-100000000)+

100000000))

, floor(rand()*

2), uuid(),

floor(rand()*

100));

set i = i +1;

endwhile

;return i ;

end $$

select mock_data();

-- 執行此函式 生成一百萬條資料

開始操作:

select

*from

where

`name`

="使用者9999"

="使用者9999"

-- 檢視sql執行資訊

-- id_表名_欄位名

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

create

(`name`

);

select

*from

where

`name`

="使用者9999"

總結:

索引在小資料量的時候,用處不大,但是在大資料的時候,區別十分明顯。

MYSQL索引分類介紹

簡單介紹一下mysql的索引分類,並給出幾個常見問題,大家自己去探索加深理解,權當拋磚引玉了。從資料結構角度 1 b 樹索引 2 hash索引 3 fulltext索引 innodb引擎5.7以後支援 4 r tree索引 用於對gis資料型別建立spatial索引 問題 這些索引的區別跟用途在哪?...

Mysql索引介紹及常見索引的區別

mysql索引概念 說說mysql索引,看到乙個很少比如 索引就好比一本書的目錄,它會讓你更快的找到內容,顯然目錄 索引 並不是越多越好,假如這本書1000頁,有500也是目錄,它當然效率低,目錄是要佔紙張的,而索引是要佔磁碟空間的。mysql索引主要有兩種結構 b tree索引和hash索引.ha...

mysql 索引劃分 MySQL索引分類

索引是一種特殊的檔案 innodb 資料表上的索引是表空間的乙個組成部分 它們包含著對資料表裡所有記錄的引用指標。索引不是萬能的,索引可以加快資料檢索操作,但會使資料修改操作變慢。每修改資料記錄,索引就必須重新整理一次。為了在某種程度上彌補這一缺陷,許多 sql 命令都有乙個 delay key w...