索引創建在表的列上(字段)的。 在where後面的列創建索引纔會加快查詢速度。 pages<---索引(屬性)<----查數據。
主鍵索引 普通索引***** 惟一索引
#建立索引 alter table test add index index_name(name); #建立索引 create index index_name on test(name); #查看索引 desc table; #查看索引 show index from table; #刪除索引 alter table test drop key index_name; #添加主鍵索引(略) #添加惟一性索引 alter table student add unique key uni_xxx(xxx); #查看錶中數據行數 select count(*) from city; #查看去重數據行數 select count(distinct name) from city;
前綴索引mysql
根據字段的前N個字符創建索引linux
alter table test add index idx_name(name(10)); 避免對大列建索引 若是有,就使用前綴索引
聯合索引redis
多個字段創建一個索引sql
例: where a.女生 and b.身高 and c.體重 and d.身材好 index(a,b,c) 特色:前綴生效特性 a,ab,ac,abc,abcd 能夠走索引或部分走索引 b bc bcd cd c d ba ... 不走索引
原則:把最經常使用來作爲條件查詢的列放在最前面數據庫
#建立people表 create table people (id int,name varchar(20),age tinyint,money int ,gender enum('m','f')); #建立聯合索引 alter table people add index idx_gam(gender,age,money);
explain命令使用方法服務器
mysql> explain select name,countrycode from city where id=1;
explain命令應用
查詢數據的方式函數
- 2.1 常見的索引掃描類型: 1)index 2)range 3)ref 4)eq_ref 5)const 6)system 7)null
從上到下,性能從最差到最好,咱們認爲至少要達到range級別性能
index:Full Index Scan,index與ALL區別爲index類型只遍歷索引樹。
range:索引範圍掃描,對索引的掃描開始於某一點,返回匹配值域的行。顯而易見的索引範圍掃描是帶有between或者where子句裏帶有<,>查詢。測試
mysql> alter table city add index idx_city(population); mysql> explain select * from city where population>30000000;
ref:使用非惟一索引掃描或者惟一索引的前綴掃描,返回匹配某個單獨值的記錄行。優化
mysql> alter table city drop key idx_code; mysql> explain select * from city where countrycode='chn'; mysql> explain select * from city where countrycode in ('CHN','USA'); mysql> explain select * from city where countrycode='CHN' union all select * from city where countrycode='USA';
eq_ref:相似ref,區別就在使用的索引是惟一索引,對於每一個索引鍵值,表中只有一條記錄匹配,簡單來講,就是多表鏈接中使用primary key或者 unique key做爲關聯條件A
join B on A.sid=B.sid
const、system:當MySQL對查詢某部分進行優化,並轉換爲一個常量時,使用這些類型訪問。
如將主鍵置於where列表中,MySQL就能將該查詢轉換爲一個常量
mysql> explain select * from city where id=1000;
NULL:MySQL在優化過程當中分解語句,執行時甚至不用訪問表或索引,例如從一個索引列裏選取最小值能夠經過單獨索引查找完成。
mysql> explain select * from city where id=1000000000000000000000000000;
Extra(擴展)
Using temporary Using filesort 使用了默認的文件排序(若是使用了索引,會避免這類排序) Using join buffer
若是出現Using filesort請檢查order by ,group by ,distinct,join 條件列上沒有索引
mysql> explain select * from city where countrycode='CHN' order by population;
當order by語句中出現Using filesort,那就儘可能讓排序值在where條件中出現
mysql> explain select * from city where population>30000000 order by population; mysql> select * from city where population=2870300 order by population;
key_len: 越小越好
前綴索引去控制
rows: 越小越好
爲了使索引的使用效率更高,在建立索引時,必須考慮在哪些字段上建立索引和建立什麼類型的索引。
那麼索引設計原則又是怎樣的?
惟一性索引的值是惟一的,能夠更快速的經過該索引來肯定某條記錄。
例如: 學生表中學號是具備惟一性的字段。爲該字段創建惟一性索引能夠很快的肯定某個學生的信息。 若是使用姓名的話,可能存在同名現象,從而下降查詢速度。 主鍵索引和惟一鍵索引,在查詢中使用是效率最高的。
select count(*) from world.city; select count(distinct countrycode) from world.city; select count(distinct countrycode,population ) from world.city;
注意:若是重複值較多,能夠考慮採用聯合索引
例如: 常常須要ORDER BY、GROUP BY、DISTINCT和UNION等操做的字段,排序操做會浪費不少時間。 若是爲其創建索引,能夠有效地避免排序操做
若是某個字段常常用來作查詢條件,那麼該字段的查詢速度會影響整個表的查詢速度。
所以,爲這樣的字段創建索引,能夠提升整個表的查詢速度。
若是索引字段的值很長,最好使用值的前綴來索引。例如,TEXT和BLOG類型的字段,進行全文檢索
會很浪費時間。若是隻檢索字段的前面的若干個字符,這樣能夠提升檢索速度。
索引的數目不是越多越好。每一個索引都須要佔用磁盤空間,索引越多,須要的磁盤空間就越大。
修改表時,對索引的重構和更新很麻煩。越多的索引,會使更新表變得很浪費時間。
表中的數據被大量更新,或者數據的使用方式被改變後,原有的一些索引可能再也不須要。數據庫管理
員應當按期找出這些索引,將它們刪除,從而減小索引對更新操做的影響。
#全表掃描 select * from table; select * from tab where 1=1;
在業務數據庫中,特別是數據量比較大的表,是沒有全表掃描這種需求。
#狀況1 #全表掃描 select * from table; #須要在price列上創建索引 selec * from tab order by price limit 10; #狀況2 #name列沒有索引 select * from table where name='zhangsan'; 一、換成有索引的列做爲查詢條件 二、將name列創建索引
mysql> explain select * from city where population>3000 order by population;
索引有自我維護的能力。 對於表內容變化比較頻繁的狀況下,有可能會出現索引失效。 重建索引就能夠解決
#例子 錯誤的例子:select * from test where id-1=9; 正確的例子:select * from test where id=10;
mysql> create table test (id int ,name varchar(20),telnum varchar(10)); mysql> insert into test values(1,'zs','110'),(2,'l4',120),(3,'w5',119),(4,'z4',112); mysql> explain select * from test where telnum=120; mysql> alter table test add index idx_tel(telnum); mysql> explain select * from test where telnum=120; mysql> explain select * from test where telnum=120; mysql> explain select * from test where telnum='120';
mysql> select * from tab where telnum <> '1555555'; mysql> explain select * from tab where telnum <> '1555555';
單獨的>,<,in 有可能走,也有可能不走,和結果集有關,儘可能結合業務添加limit
or或in儘可能改爲union
EXPLAIN SELECT * FROM teltab WHERE telnum IN ('110','119'); #改寫成 EXPLAIN SELECT * FROM teltab WHERE telnum='110' UNION ALL SELECT * FROM teltab WHERE telnum='119'
#走range索引掃描 EXPLAIN SELECT * FROM teltab WHERE telnum LIKE '31%'; #不走索引 EXPLAIN SELECT * FROM teltab WHERE telnum LIKE '%110';
%linux%類的搜索需求,可使用Elasticsearch -------> ELK
CREATE TABLE t1 (id INT,NAME VARCHAR(20),age INT ,sex ENUM('m','f'),money INT); ALTER TABLE t1 ADD INDEX t1_idx(money,age,sex); DESC t1 SHOW INDEX FROM t1 #走索引的狀況測試 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND age=30 AND sex='m'; #部分走索引 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND age=30; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE money=30 AND sex='m'; #不走索引 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE age=20 EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE age=30 AND sex='m'; EXPLAIN SELECT NAME,age,sex,money FROM t1 WHERE sex='m';