mysql中索引的使用

2021-09-25 09:04:26 字數 820 閱讀 8942

**

我們在使用sql時,在遇到sql效能下降、執行時間長時,就需要考慮用索引來幫我們解決問題。如,資料過多,關聯太多的表等。

建立索引

create index idx_name_age_address on student(name,age,address);

create table student(

id int(10) auto_increment,

name varchar(20) ,

age int(3),

address varchar(200) ,

class varchar(3) ,

primary key(id),

key(name),

unique(name),

key(name,address)

);刪除索引

drop index idx_name,age,address on student;

檢視索引

show index from student;

檢視是否使用索引

explain select sql_no_cache * from student where name=『張三』;

注:sql_no_cache不使用快取來查詢。

索引使用失敗的場景。

使用不等於(!=或<>)時

使用is not null

like以萬用字元開頭(』%三『)

字串不加單引號

mysql中索引的使用

索引是加速查詢的主要手段,特別對於涉及多個表的查詢更是如此。本節中,將介紹索引的作用 特點,以及建立和刪除索引的語法。索引是快速定位資料的技術,首先通過乙個示例來了解其含義及作用,詳細的介紹請參考第14章。1 索引示例 假設對於10.3節所建的表,各個表上都沒有索引,資料的排列也沒有規律,如表13....

MySQL中索引的使用

語法 create table table name 屬性名 資料型別 約束條件 屬性名 資料型別 約束條件 unique fulltext spatial index key 別名 屬性名 asc desc 建立普通索引 普通索引 create table score id int auto in...

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

一張表最多不超過4個索引 某個欄位的值離散度越高,該字段越適合做索引的關鍵字。占用儲存空間少的字段更適合選做索引的關鍵字。較頻繁的作為where查詢條件的字段應該建立索引,分組字段或者排序字段應該建立索引,兩個表的連線字段應該建立索引。更新頻繁的字段不適合做索引,不會出現在where中的字段不應該建...