我剛開始看執行計劃的時候,還覺得是mysql計劃如何去執行對數據庫的增刪改查,相似於觸發器之類的東西,但通過學習才發現執行計劃真的是跟觸發器一毛錢關係都沒有。mysql
什麼是執行計劃呢?我我的理解,執行計劃是告訴數據庫操做人員這條sql語句將要如何去查詢。說白了,若是說sql語句是具體的執行行動,那麼執行計劃就是行動的預算,好比說執行的時候會不會用到索引,會不會用到分區等。sql
咱們先建立一張表: 數據庫
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` int(11) DEFAULT NULL, `sex` bit(1) DEFAULT b'0', PRIMARY KEY (`id`) )
咱們執行一條語句: 學習
select * from student where name="abc";
如何查看執行計劃呢,很簡單,在執行的sql語句以前加上「desc」或「explain」便可。咱們具體查看下:spa
desc select * from student where name="abc" \G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: student partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 7 filtered: 14.29 Extra: Using where
各個字段的含義:code
最後,若是在name字段上加上索引,會形成執行計劃的變化,咱們也能夠經過兩個執行計劃的不一樣來加深對以上的字段的理解:索引
alter table student add index idx_name (name); desc select * from student where name = "abc" \G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: student partitions: NULL type: ref possible_keys: idx_name key: idx_name key_len: 23 ref: const rows: 1 filtered: 100.00 Extra: NULL
由此也能夠看出索引在查詢過程當中所起的做用和所佔的份量it