mysql欄位簡索引 MySQL中索引使用簡例

2021-10-13 12:29:49 字數 2035 閱讀 6534

一張表最多不超過4個索引

某個欄位的值離散度越高,該字段越適合做索引的關鍵字。

占用儲存空間少的字段更適合選做索引的關鍵字。

較頻繁的作為where查詢條件的字段應該建立索引,分組字段或者排序字段應該建立索引,兩個表的連線字段應該建立索引。

更新頻繁的字段不適合做索引,不會出現在where中的字段不應該建立索引。

最左字首原則:

index(id,name,age)

查詢id='' name='' age='' 會使用索引

id='' name='' 不會

id='' age='' 不會,必須從左至右

盡量使用字首索引

全文索引fulltext只有在mysam引擎中有效

show index from 表名;鄭州去胎記醫院

檢視表中的索引。

注意:在指定主鍵的時候,資料庫會自動在主鍵欄位上創立主鍵索引.

建表時建立索引語法:

create table 表名(

欄位名1 資料型別 [約束條件],

[其他約束條件],

[ unique | fulltext ] index [索引名] ( 字段

名 [(長度)] [ asc | desc ] )

) engine=儲存引擎型別 default charset=

字符集型別.

例項:create table book(

isbn char(20) primary key,

name char(100) not null,

brief_introduction text not null,

price decimal(6,2),

publish_time date not null,

unique index isbn_unique (isbn),

index name_index (name (20)),

fulltext index brief_fulltext

(name,brief_introduction),

index complex_index (price,publish_time)

) engine=myisam default charset=gbk;

在已有表上建立索引:

語法格式⼀:

create [ unique | fulltext ] index 索引名 on 表名 ( 字段

名 [(長度)] [ asc | desc ] )

語法格式⼆:

alter table 表名 add [ unique | fulltext ] index 索引名

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

例項:create unique index name_index on studentinfo(name(3));

使用索引的格式:

select studentid,score

from score use index(索引名)

where studentid>=3;

select studentid,score

from score use index(index_stuid)

where studentid>=3;

在其他欄位上建立的索引,但查詢的studentid上沒有,在查詢的時候呼叫其他欄位的索引

刪除索引:

drop index 索引名 on 表名;

檢視create view ppcourse4

asselect

fir.cno,fir.cname as c1,third.cname as c2

from

course fir,course sec,course third

where

fir.cpno = sec.cno

andsec.cpno = third.cno

select * from ppcourse4

ppcourse4

delete from ppcourse4

drop view ppcourse4

mysql 索引 簡書 MySQL 索引

簡介 索引用於快速找出在某個列中有一特定值的行,不使用索引,mysql必須從第一條記錄開始讀完整個表,直到找出相關的行,表越大,查詢資料所花費的時間就越多。如果表中查詢的列有乙個索引,mysql能夠快速到達乙個位置去搜尋資料檔案,而不必檢視所有資料,那麼將會節省很大一部分時間。使用原則 索引底層使用...

mysql欄位索引

drop table if exists auth assignment drop table if exists auth item child drop table if exists auth item drop table if exists auth rule create table a...

mysql索引簡談

mysql索引簡談 一 什麼是索引 就好比我們在看一本書的時候,有目錄的話,我們可以快速定位到想看的地方,而沒有目錄的話,我們只能一頁一頁地翻。索引就像目錄,有了索引,資料庫可以快速查詢到目標內容,而不必查詢整個資料庫表,但是如果沒有的話,資料庫只能一行一行地遍歷資料。create table t ...