1.索引
MySQL索引的建立對于MySQL的高效運行是很重要的,索引可以大大提高MySQL的檢索速度。
索引分單列索引和組合索引。單列索引,即一個索引只包含單個列,一個表可以有多個單列索引,但這不是組合索引。組合索引,即一個索引包含多個列。
創(chuàng)建索引時,你需要確保該索引是應用在 SQL 查詢語句的條件(一般作為 WHERE 子句的條件)。
實際上,索引也是一張表,該表保存了主鍵與索引字段,并指向實體表的記錄。
缺點:雖然索引大大提高了查詢速度,同時卻會降低更新表的速度,如對表進行INSERT、UPDATE和DELETE。因為更新表時,MySQL不僅要保存數據,還要保存一下索引文件。
建立索引會占用磁盤空間的索引文件。
2.創(chuàng)建索引
create index indexName on mytable(username(length));
create index a on user (id(2));
創(chuàng)建user表id列的a索引
3.刪除索引
drop index [indexName] on mytable;
drop index a on user;
刪除user表的a索引
4.唯一索引
create unique index indexName on mytable(username(length));
create unique index a on user (id(2));
創(chuàng)建user表id列的a唯一索引
修改表結構
alter table mytable add unique [indexName] (username(length))
alter table user add unique a (id(2));
5.全文索引
alter table tbl_name add fulltext index_name (column_list):該語句指定了索引為 fulltext ,用于全文索引。
alter table user add fulltext a (id(2));
6.顯示索引
show index from table_name;
show index from user;
查看user表的索引