1. 程式人生 > >Mysql索引的設計和使用

Mysql索引的設計和使用

<code class="hljs oxygene"><code class="hljs asciidoc">1、all -- 環境描述 ([email protected]) [sakila]> show variables like 'version'; +---------------+--------+ | Variable_name | Value  | +---------------+--------+ | version      | 5.6.26 | +---------------+--------+ MySQL採取全表遍歷的方式來返回資料行,等同於Oracle的full table scan ([email protected]) [sakila]> explain select count(description) from film; +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ |  1 | SIMPLE      | film  | ALL  | NULL          | NULL | NULL    | NULL | 1000 | NULL  | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 2、index MySQL採取索引全掃描的方式來返回資料行,等同於Oracle的full index scan (
[email protected]
) [sakila]> explain select title from film \G *************************** 1. row ***************************           id: 1   select_type: SIMPLE         table: film         type: index possible_keys: NULL           key: idx_title       key_len: 767           ref: NULL         rows: 1000         Extra: Using index 1 row in set (0.00 sec) 3、  range 索引範圍掃描,對索引的掃描開始於某一點,返回匹配值域的行,常見於between、<、>等的查詢 等同於Oracle的index range scan (
[email protected]
) [sakila]> explain select * from payment where customer_id>300 and customer_id<400\G *************************** 1. row ***************************           id: 1   select_type: SIMPLE         table: payment         type: range possible_keys: idx_fk_customer_id           key: idx_fk_customer_id       key_len: 2           ref: NULL         rows: 2637         Extra: Using where 1 row in set (0.00 sec) (
[email protected]
) [sakila]> explain select * from payment where customer_id in (200,300,400)\G *************************** 1. row ***************************           id: 1   select_type: SIMPLE         table: payment         type: range possible_keys: idx_fk_customer_id           key: idx_fk_customer_id       key_len: 2           ref: NULL         rows: 86         Extra: Using index condition 1 row in set (0.00 sec) 4、ref 非唯一性索引掃描或者,返回匹配某個單獨值的所有行。常見於使用非唯一索引即唯一索引的非唯一字首進行的查詢 ([email protected]) [sakila]> explain select * from payment where customer_id=305\G *************************** 1. row ***************************           id: 1   select_type: SIMPLE         table: payment         type: ref possible_keys: idx_fk_customer_id           key: idx_fk_customer_id       key_len: 2           ref: const         rows: 25         Extra: 1 row in set (0.00 sec) idx_fk_customer_id為表payment上的外來鍵索引,且存在多個不不唯一的值,如下查詢 ([email protected]) [sakila]> select customer_id,count(*) from payment group by customer_id     -> limit 2; +-------------+----------+ | customer_id | count(*) | +-------------+----------+ |          1 |      32 | |          2 |      27 | +-------------+----------+ -- 下面是非唯一字首索引使用ref的示例 ([email protected]) [sakila]> create index idx_fisrt_last_name on customer(first_name,last_name); Query OK, 599 rows affected (0.09 sec) Records: 599  Duplicates: 0  Warnings: 0 ([email protected]) [sakila]> select first_name,count(*) from customer group by first_name     -> having count(*)>1 limit 2; +------------+----------+ | first_name | count(*) | +------------+----------+ | JAMIE      |        2 | | JESSIE    |        2 | +------------+----------+ 2 rows in set (0.00 sec) ([email protected]) [sakila]> explain select first_name from customer where first_name='JESSIE'\G *************************** 1. row ***************************           id: 1   select_type: SIMPLE         table: customer         type: ref possible_keys: idx_fisrt_last_name           key: idx_fisrt_last_name       key_len: 137           ref: const         rows: 2         Extra: Using where; Using index 1 row in set (0.00 sec) ([email protected]) [sakila]> alter table customer drop index idx_fisrt_last_name; Query OK, 599 rows affected (0.03 sec) Records: 599  Duplicates: 0  Warnings: 0 --下面演示出現在join是ref的示例 ([email protected]) [sakila]> explain select b.*,a.* from payment a inner join     -> customer b on a.customer_id=b.customer_id\G *************************** 1. row ***************************           id: 1   select_type: SIMPLE         table: b         type: ALL possible_keys: PRIMARY           key: NULL       key_len: NULL           ref: NULL         rows: 599         Extra: NULL *************************** 2. row ***************************           id: 1   select_type: SIMPLE         table: a         type: ref possible_keys: idx_fk_customer_id           key: idx_fk_customer_id       key_len: 2           ref: sakila.b.customer_id         rows: 13         Extra: NULL 2 rows in set (0.01 sec) 5、eq_ref 類似於ref,其差別在於使用的索引為唯一索引,對於每個索引鍵值,表中只有一條記錄與之匹配。 多見於主鍵掃描或者索引唯一掃描。 ([email protected]) [sakila]> explain select * from film a join film_text b     -> on a.film_id=b.film_id; +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ | id | select_type | table | type  | possible_keys | key    | key_len | ref              | rows | Extra      | +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ |  1 | SIMPLE      | b    | ALL    | PRIMARY      | NULL    | NULL    | NULL            | 1000 | NULL        | |  1 | SIMPLE      | a    | eq_ref | PRIMARY      | PRIMARY | 2      | sakila.b.film_id |    1 | Using where | +----+-------------+-------+--------+---------------+---------+---------+------------------+------+-------------+ ([email protected]) [sakila]> explain select title from film where film_id=5; +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type  | possible_keys | key    | key_len | ref  | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ |  1 | SIMPLE      | film  | const | PRIMARY      | PRIMARY | 2      | const |    1 | NULL  | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+ 6、const、system: 當MySQL對查詢某部分進行優化,這個匹配的行的其他列值可以轉換為一個常量來處理。 如將主鍵或者唯一索引置於where列表中,MySQL就能將該查詢轉換為一個常量 ([email protected]) [sakila]> create table t1(id int,ename varchar(20) unique); Query OK, 0 rows affected (0.05 sec) ([email protected]) [sakila]> insert into t1 values(1,'robin'),(2,'jack'),(3,'henry'); Query OK, 3 rows affected (0.00 sec) Records: 3  Duplicates: 0  Warnings: 0 ([email protected]) [sakila]> explain select * from (select * from t1 where ename='robin')x; +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ | id | select_type | table      | type  | possible_keys | key  | key_len | ref  | rows | Extra | +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ |  1 | PRIMARY    | <derived2> | system | NULL          | NULL  | NULL    | NULL  |    1 | NULL  | |  2 | DERIVED    | t1        | const  | ename        | ename | 23      | const |    1 | NULL  | +----+-------------+------------+--------+---------------+-------+---------+-------+------+-------+ 2 rows in set (0.00 sec) 7、type=NULL MySQL不用訪問表或者索引就可以直接得到結果 ([email protected]) [sakila]> explain select sysdate(); +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ |  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used | +----+-------------+-------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec)</derived2></code></code>

相關推薦

mysql 索引長度區分度

一點 分享 分析 blog 是不是 程序 觀察 name test 首先 索引長度和區分度是相互矛盾的, 索引長度太短,那麽區分度就很低,吧索引長度加長,區分度就高,但是索引也是要占內存的,所以我們需要找到一個平衡點; 那麽這個平衡點怎麽來定? 比如用戶表有個

MySQL 索引設計概要

ctu 緩沖 數據頁面 block 過多 頁面 key 偏差 disk 在關系型數據庫中設計索引其實並不是復雜的事情,很多開發者都覺得設計索引能夠提升數據庫的性能,相關的知識一定非常復雜。 然而這種想法是不正確的,索引其實並不是一個多麽高深莫測的東西,只要我們掌握一定的方

MySQL 索引分類每個作用

ext lena drop 基本 username 索引 pri 限制 dex 對一些大型的網站,索引的作用很明顯,MySQL索引的建立對於MySQL的高效運行是很重要的,索引可以大大提高MySQL的檢索速度。 1.普通索引 這是最基本的索引,它沒有任何限制

MySQL 索引分類適用場景

一、 MySQL: 索引以B樹格式儲存 Memory儲存引擎可以選擇Hash或BTree索引,Hash索引只能用於=或<=>的等式比較。 1、普通索引:create index on Tablename(列的列表) alter table TableName add

mysql索引型別索引方式

1.什麼是索引 在MySQL中,索引(index)也叫做“鍵(key)”,它是儲存引擎用於快速找到記錄的一種資料結構。 2.索引的分類 在MySQL中,通常我們所指的索引型別,有以下幾種: 主鍵索引(PRIMARY KEY) 也簡稱主鍵。它可以提高查詢效率,並提供唯一性約

說說MySQL索引MyISAMInnoDB的區別

如題,廢話不多說, 首先兩者都是使用B+樹作為底層的資料結構的。 下圖是MyISAM索引的原理圖(圖是網上找的。。。): 這個圖就很明顯了: 1,葉節點的data域存放的是資料記錄的地址。MyISAM的索引與行記錄是分開儲存的,叫做非聚集索引(U

MySQL索引設計背後的資料結構及演算法詳解

在我們公司的DB規範中,明確規定: 1、建表語句必須明確指定主鍵 2、無特殊情況,主鍵必須單調遞增 對於這項規定,很多研發小夥伴不理解。本文就來深入分析MySQL索引設計背後的資料結構和演算法,從而幫你釋疑以下幾個問題: 1、為什麼InnoDB表需要主鍵? 2、為什麼建議InnoDB表主鍵是單調

MySQL索引設計需要考慮哪些因素?

索引小知識 篇幅有限,索引的基本知識我們就不贅述了,在此,我們嘗試說明其中的一個小點-----B+樹與B樹的區別到底是什麼。 InnoDB是使用B+樹來實現其索引功能的。在B+樹中,內節點(非葉子節點)儲存了行資料的鍵,而葉子節點儲存了所有的行資料,而B樹的每個節點都儲存了真實的資料。這種資料結構,決定了

Mysql索引概念儲存過程

索引 索引演算法 1.二叉樹索引,時間複雜度O(lgN)。 2.雜湊表,時間複雜度O(1)。 索引原則: 1.不過度索引 2.索引條件列(where後面最頻繁的條件比較適宜索引) 3.索引雜湊值,過於集中的值不要索引。例如:給“男”“女”索引,意義不大。 索引缺點:

深入理解MySQL索引原理實現——為什麼索引可以加速查詢?

說到索引,很多人都知道“索引是一個排序的列表,在這個列表中儲存著索引的值和包含這個值的資料所在行的實體地址,在資料十分龐大的時候,索引可以大大加快查詢的速度,這是因為使用索引後可以不用掃描全表來定位某行的資料,而是先通過索引表找到該行資料對應的實體地址然後訪問相應的資料。”但

mysql索引入門實戰(二) 環境準備

DROP TABLE IF EXISTS `task`; CREATE TABLE `task` ( `id` varchar(32) NOT NULL, `task_pid` varchar(32) DEFAULT NULL COMMENT '上級ID', `t

mysql索引設計的注意事項(大量示例,收藏再看)

mysql索引設計的注意事項(大量示例,收藏再看) 目錄 一、索引的重要性 二、執行計劃上的重要關注點 (1).全表掃描,檢索行數 (2).key,using index(覆蓋索引) (3).通過key_len確定究竟使用了複合索引的幾個索引欄位 (4) order by和Using fil

MySQL索引介紹實戰

索引是什麼 MySQL官方對索引的定義為:索引(Index)是幫助MySQL高效獲取資料的資料結構。 可以得到索引的本質:索引是資料結構,索引的目的是提高查詢效率,可以類比英語新華字典,根據目錄定位詞語 如果沒有目錄呢,就需要從A到Z,去遍歷的查詢一遍,一個一個找和直接根據目錄定位到資料,差的就是天壤之別 &

MySQL索引設計、使用優化

mysql索引 查詢 strcmp 鍵值 HA 特定 好處 b- 進行 原文:http://bbs.landingbj.com/t-0-243071-1.html MySQL索引概述 所有MySQL列類型可以被索引。對相關列使用索引是提高SELECT操作性能的最佳途徑。

mysql 開發基礎系列15 索引設計使用

hash索引 drop myisam mar 至少 不同類 不同 執行 例如 一.概述   所有mysql 列類型都可以被索引,是提高select查詢性能的最佳方法。 根據存儲引擎可以定義每個表的最大索引數和最大索引長度,每種引擎對每個表至少支持16個索引,總索引長度至少為

MySQL(七)------ 索引設計使用

       索引是資料庫中用來提高效能的最常用工具,下面簡單介紹一下索引的型別和設計原則。 一、索引概述 常用引擎的索引方式 特點 MyISAM InnoDB MEMORY

《深入淺出MySQL》讀書筆記四:索引設計使用

一、概述 所有的MySQL列型別都可以被索引,對相關列使用索引是提高SELECT效能的最佳途徑。 根據儲存引擎的不同,表的最大索引數和最大索引長度有所不同,每種儲存引擎對每個表至少支援16個索引,總索引長度至少為 256位元組。 MyISAM和InnoDB 使用  BTREE索

Mysql索引設計使用

<code class="hljs oxygene"><code class="hljs asciidoc">1、all -- 環境描述 ([email protected]) [sakila]> show variables like 'version'; +---

mysql索引的正確設計適用

 1. 索引的儲存分類 MyISAM儲存引擎的表的資料和索引時自動分開儲存的,各自是獨立的一個檔案;InnoDB儲存引擎的表的資料和索引時儲存在同一表空間裡面,但可以有多個檔案組成。 MySQL中索引的儲存型別目前只有兩種(BTREE和HASH),具體和表的儲存引擎相關;

MySQL索引使用方法性能優化

lec 兩種 允許 聯合查詢 網站 大小 磁盤 會有 har 關於MySQL索引的好處,如果正確合理設計並且使用索引的MySQL是一輛蘭博基尼的話,那麽沒有設計和使用索引的MySQL就是一個人力三輪車。對於沒有索引的表,單表查詢可能幾十萬數據就是瓶頸,而通常大型網站單日就可