1)索引就比如一本書的目錄,它能讓你更快的找到本身想要的內容。
2)讓獲取的數據更有目的性,從而提升數據庫檢索數據的性能。mysql
1)BTREE:B+樹索引
2)HASH:HASH索引
3)FULLTEXT:全文索引
4)RTREE:R樹索引linux
Btree索引redis
B+tree索引sql
B*tree索引數據庫
在枝節點添加了相鄰節點的指針服務器
索引創建在表的列上(字段)的。
在where後面的列創建索引纔會加快查詢速度。
pages<---索引(屬性)<----查數據。函數
主鍵索引(primary key)
普通索引( index key)
惟一索引(unique key)性能
#建立索引 alter table test add index index_name(name); #建立索引 create index index_name on test(name); #查看索引 desc table; show create table student; #查看索引 show index from table; #刪除索引 alter table test drop key index_name; #添加主鍵索引 alter table test add primary key pri_id(id); #添加惟一性索引 alter table student add unique key uni_xxx(xxx);
mysql> select count(cname) from course; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.00 sec) mysql> select count(distinct(cname)) from course; +------------------------+ | count(distinct(cname)) | +------------------------+ | 3 | +------------------------+ 1 row in set (0.00 sec)
前綴索引測試
根據字段的前N個字符創建索引優化
alter table test add index idx_name(name(10));
避免對大列建索引
若是有,就使用前綴索引
mysql> alter table course drop index cname1;
多個字段創建一個索引
例:
where a.女生 and b.身高 and c.體重 and d.身材好
index(a,b,c)
原則:把最經常使用來作爲條件查詢的列放在最前面
#建立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); alter table people add index xxxx_all(a,b,c,d); select * from people where a b c d a b c a b a c(部分走索引) 不走索引 b c d c d b c
1.不要在全部字段上都建立索引
2.若是有需求字段比較多,選擇聯合索引
3.若是有需求字段數據比較大,選擇前綴索引
4.若是能夠建立惟一索引,必定建立惟一索引
explain命令使用方法
mysql> explain select name,countrycode from city where id=1;
explain命令應用
查詢數據的方式
1.全表掃描1)在explain語句結果中type爲ALL
2)何時出現全表掃描?
生產中,mysql在使用全表掃描時的性能是極其差的,因此MySQL儘可能避免出現全表掃描
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等操做的字段,排序操做會浪費不少時間。
若是爲其創建索引,能夠有效地避免排序操做
3.爲常做爲查詢條件的字段創建索引
若是某個字段常常用來作查詢條件,那麼該字段的查詢速度會影響整個表的查詢速度。
所以,爲這樣的字段創建索引,能夠提升整個表的查詢速度。
注:若是常常做爲條件的列,重複值特別多,能夠創建聯合索引
若是索引字段的值很長,最好使用值的前綴來索引。例如,TEXT和BLOG類型的字段,進行全文檢索
會很浪費時間。若是隻檢索字段的前面的若干個字符,這樣能夠提升檢索速度。
----------------------------------------------------------------------------- 我是華麗的分割線 --------------------------------------------------
---------------------------------------------------------------------------- 是的,沒錯,又是我 --------------------------------------------
重點關注:
#全表掃描 select * from table; select * from tab where 1=1;
在業務數據庫中,特別是數據量比較大的表,是沒有全表掃描這種需求。
1)對用戶查看是很是痛苦的。
2)對服務器來說毀滅性的。
3)SQL改寫成如下語句:
#狀況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;
1)若是業務容許,可使用limit控制。
2)結合業務判斷,有沒有更好的方式。若是沒有更好的改寫方案就儘可能不要在mysql存放這個數據了,放到redis裏面。
3.索引自己失效,統計數據不真實
索引有自我維護的能力。
對於表內容變化比較頻繁的狀況下,有可能會出現索引失效。
重建索引就能夠解決
4.查詢條件使用函數在索引列上或者對索引列進行運算,運算包括(+,-,*等)
#例子 錯誤的例子: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 (去重) + all (不去重)
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';
1.有沒有建立索引
2.查看數據類型,和查詢語句是否一致,
3.查詢語句中是否使用字段作用算
4.查詢出來的結果集很大,limit
5.查詢語句中是否使用<>或者not in
6.查詢語句是否使用模糊查詢,且'%*'在前面
7.若是使用聯合索引,請安裝建立索引的順序
8.索引損壞