創(chuàng)建高性能索引

Indexing Basics

索引類型

B-TREE 索引

InnoDB使用的即是B-TREE索引峦嗤。
存儲(chǔ)引擎以不同的方式使用B-TREE索引,性能也各有不同。例如吧秕,MyISAM使用前綴壓縮技術(shù)使得索引更小,但I(xiàn)nnoDB則按照原數(shù)據(jù)格式進(jìn)行存儲(chǔ)迹炼。再如MyISAM索引通過數(shù)據(jù)的物理位置引用被索引的行砸彬,而InnoDB則根據(jù)主鍵引用被索引的行。

B-TREE對(duì)索引列是順序組織存儲(chǔ)的斯入,所以很適合查找范圍數(shù)據(jù)砂碉。下圖為B-TREE索引的抽象表示:


B-TREE Index.png

B-TREE索引能夠加快數(shù)據(jù)訪問的速度,因?yàn)榇鎯?chǔ)引擎不再需要進(jìn)行全表掃描獲取需要的數(shù)據(jù)刻两。

假設(shè)有下面這個(gè)數(shù)據(jù)表:

CREATE TABLE People ( 
last_name varchar(50) ,
first_name varchar(50) dob date,
not null, not null, not null,
gender enum('m', 'f')not null,
key(last_name, first_name, dob) );

The index will contain the values from the last_name, first_name, and dob columns for every row in the table.
!!!索引對(duì)多個(gè)值進(jìn)行排序的依據(jù)是CREATE TABLE語(yǔ)句中定義索引時(shí)的順序增蹭。

Types of queries that can use a B-Tree index. B-Tree indexes work well for lookups by the full key value, a key range, or a key prefix. They are useful only if the lookup uses a leftmost prefix of the index.

Here are some limitations of B-Tree indexes:

  • They are not useful if the lookup does not start from the leftmost side of the indexed columns. For example, this index won’t help you find all people named Bill or all people born on a certain date, because those columns are not leftmost in the index. Likewise, you can’t use the index to find people whose last name ends with a par- ticular letter.
  • You can’t skip columns in the index. That is, you won’t be able to find all people whose last name is Smith and who were born on a particular date. If you don’t specify a value for the first_name column, MySQL can use only the first column of the index.
  • The storage engine can’t optimize accesses with any columns to the right of the first range condition. For example, if your query is WHERE last_name="Smith" AND first_name LIKE 'J%' AND dob='1976-12-23', the index access will use only the first two columns in the index, because the LIKE is a range condition (the server can use the rest of the columns for other purposes, though). For a column that has a limited number of values, you can often work around this by specifying equality conditions instead of range conditions. We show detailed examples of this in the indexing case study later in this chapter.

所以索引列的順序是非常的重要,這些限制都和索引列的順序有關(guān)磅摹。在優(yōu)化的時(shí)候可以使用相同的列但順序不同的索引來滿足快速查詢的需求滋迈。

哈希索引

A hash index is built on a hash table and is useful only for exact lookups that use every column in the index.
In MySQL, only the Memory storage engine supports explicit hash indexes. They are the default index type for Memory tables, though Memory tables can have B-Tree indexes, too.

Building your own hash indexes. If your storage engine doesn’t support hash indexes, you can emulate them yourself in a manner similar to that InnoDB uses. This will give you access to some of the desirable properties of hash indexes, such as a very small index size for very long keys.

An example of when this approach works well is for URL lookups. URLs generally cause B-Tree indexes to become huge, because they’re very long. You’d normally query a table of URLs like this:

mysql> SELECT id FROM url WHERE url="http://www.mysql.com";

But if you remove the index on the url column and add an indexed url_crc column to the table, you can use a query like this:

mysql> SELECT id FROM url WHERE url="http://www.mysql.com" -> AND url_crc=CRC32("http://www.mysql.com");

CRC32 is one of hash functions based on on the "polynomial" division idea. The CRC is acronym for Cyclic Redundancy Code (other variants instead "Code" is "Check" and "Checksum") algorithm. The number 32 is specifying the size of resulting hash value (checksum) - 32 bits. The checksum is used to detect errors after transmission or storage of any piece of information.

This works well because the MySQL query optimizer notices there’s a small, highly selective index on the url_crc column and does an index lookup for entries with that value (1560514994, in this case). Even if several rows have the same url_crc value, it’s very easy to find these rows with a fast integer comparison and then examine them to find the one that matches the full URL exactly. The alternative is to index the full URL as a string, which is much slower.

這樣做的一個(gè)缺陷是需要維護(hù)hash值霎奢,可以通過使用觸發(fā)器來較好的解決這個(gè)問題。

If you use this approach, you should not use SHA1() or MD5() hash functions. These return very long strings, which waste a lot of space and result in slower comparisons. They are cryptographically strong functions designed to virtually eliminate collisions, which is not your goal here. Simple hash functions can offer acceptable collision rates with better performance.

If your table has many rows and CRC32() gives too many collisions, implement your own 64-bit hash function. Make sure you use a function that returns an integer, not a string. One way to implement a 64-bit hash function is to use just part of the value returned by MD5().

空間數(shù)據(jù)索引(R-Tree)

全文索引

索引的優(yōu)點(diǎn)

Three main benefits proceed from these properties:

  1. Indexes reduce the amount of data the server has to examine.
  2. Indexes help the server avoid sorting and temporary tables.
  3. Indexes turn random I/O into sequential I/O.

高性能索引策略

獨(dú)立的列

“Isolating” the column means it should not be part of an expression or be inside a function in the query.如果查詢中得列不是獨(dú)立的饼灿,MySQL就不會(huì)使用索引椰憋。

前綴索引和索引選擇性

Index selectivity is the ratio of the number of distinct indexed values (the cardinality) to the total number of rows in the table (#T), and ranges from 1/#T to 1.

A prefix of the column is often selective enough to give good performance. If you’re indexing BLOB or TEXT columns, or very long VARCHAR columns, you must define prefix indexes, because MySQL disallows indexing their full length.

創(chuàng)建前綴索引

mysql> ALTER TABLE tableName ADD KEY (colName(len));

前綴索引的一個(gè)缺點(diǎn)是MySQL無法用其做orderBy和groupBy。

選擇合適的索引列順序

選擇索引列順序的經(jīng)驗(yàn)法則: 將選擇性最高的列放在前面赔退。

聚族索引

聚族索引并不是一種單獨(dú)的索引類型橙依,而是一種數(shù)據(jù)存儲(chǔ)方式。
InnoDB將通過主鍵聚集數(shù)據(jù)硕旗。


cluster_index.png

覆蓋索引

An index that contains (or “covers”) all the data needed to satisfy a query is called a covering index.

使用索引掃描來做排序

壓縮(前綴壓縮)索引

MySQL需要單獨(dú)維護(hù)重復(fù)的索引窗骑,所以有性能上的考慮。重復(fù)索引是指在相同的列上按照相同的順序創(chuàng)建的相同類型的索引漆枚。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末创译,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子墙基,更是在濱河造成了極大的恐慌软族,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件残制,死亡現(xiàn)場(chǎng)離奇詭異立砸,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)初茶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門颗祝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人恼布,你說我怎么就攤上這事螺戳。” “怎么了折汞?”我有些...
    開封第一講書人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵倔幼,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我爽待,道長(zhǎng)损同,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任堕伪,我火速辦了婚禮揖庄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘欠雌。我一直安慰自己,他們只是感情好疙筹,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開白布富俄。 她就那樣靜靜地躺著禁炒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪霍比。 梳的紋絲不亂的頭發(fā)上幕袱,一...
    開封第一講書人閱讀 49,772評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音悠瞬,去河邊找鬼们豌。 笑死,一個(gè)胖子當(dāng)著我的面吹牛浅妆,可吹牛的內(nèi)容都是我干的望迎。 我是一名探鬼主播,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼凌外,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼辩尊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起康辑,我...
    開封第一講書人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤摄欲,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后疮薇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胸墙,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年按咒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了劳秋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡胖齐,死狀恐怖玻淑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情呀伙,我是刑警寧澤补履,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站剿另,受9級(jí)特大地震影響箫锤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜雨女,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一谚攒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧氛堕,春花似錦馏臭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)绕沈。三九已至,卻和暖如春帮寻,著一層夾襖步出監(jiān)牢的瞬間乍狐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工固逗, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留浅蚪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓烫罩,卻偏偏與公主長(zhǎng)得像惜傲,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嗡髓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容