首頁 - 網(wǎng)校 - 萬題庫 - 美好明天 - 直播 - 導航
您現(xiàn)在的位置: 考試吧 > 計算機等級考試 > 計算機二級考試 > MySQL > 復習資料 > 正文

全國計算機二級MySQL數(shù)據(jù)庫程序設計考試知識點(5)

來源:考試吧 2018-07-31 15:50:41 要考試,上考試吧! 萬題庫
“全國計算機二級MySQL數(shù)據(jù)庫程序設計考試知識點(5)”供考生參考。更多計算機等級考試模擬試題等信息請訪問考試吧計算機等級考試網(wǎng)。

  點擊查看:全國計算機二級MySQL數(shù)據(jù)庫程序設計知識點匯總

  mysql索引的三個原則

  一,索引的重要性

  索引用于快速找出在某個列中有一特定值的行。不使用索引,MySQL必須從第1條記錄開始然后讀完整個表直到找出相關的行。表越大,花費的時間越多。如果表中查詢的列有一個索引,MySQL能快速到達一個位置去搜尋到數(shù)據(jù)文件的中間,沒有必要看所有數(shù)據(jù)。注意如果你需要訪問大部分行,順序讀取要快得多,因為此時我們避免磁盤搜索。

  假如你用新華字典來查找“張”這個漢字,不使用目錄的話,你可能要從新華字典的第一頁找到最后一頁,可能要花二個小時。字典越厚呢,你花的時間就越多,F(xiàn)在你使用目錄來查找“張”這個漢字,張的首字母是z,z開頭的漢字從900多頁開始,有了這條線索,你查找一個漢字可能只要一分鐘,由此可見索引的重要性。但是索引建的是不是越多越好呢,當然不是,如果一本書的目錄分成好幾級的話,我想你也會暈的。

  二,準備工作

  //準備二張測試表

  mysql> CREATE TABLE `test_t` (

  -> `id` int(11) NOT NULL auto_increment,

  -> `num` int(11) NOT NULL default '0',

  -> `d_num` varchar(30) NOT NULL default '0',

  -> PRIMARY KEY (`id`)

  -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

  Query OK, 0 rows affected (0.05 sec)

  mysql> CREATE TABLE `test_test` (

  -> `id` int(11) NOT NULL auto_increment,

  -> `num` int(11) NOT NULL default '0',

  -> PRIMARY KEY (`id`)

  -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

  Query OK, 0 rows affected (0.05 sec)

  //創(chuàng)建一個存儲過程,為插數(shù)據(jù)方便

  mysql> delimiter |

  mysql> create procedure i_test(pa int(11),tab varchar(30))

  -> begin

  -> declare max_num int(11) default 100000;

  -> declare i int default 0;

  -> declare rand_num int;

  -> declare double_num char;

  ->

  -> if tab != 'test_test' then

  ->selectcount(id) into max_num from test_t;

  -> while i < pa do

  -> if max_num < 100000 then

  -> select cast(rand()*100 as unsigned) into rand_num;

  -> select concat(rand_num,rand_num) into double_num;

  -> insert into test_t(num,d_num)values(rand_num,double_num);

  -> end if;

  -> set i = i +1;

  -> end while;

  -> else

  -> select count(id) into max_num from test_test;

  -> while i < pa do

  -> if max_num < 100000 then

  -> select cast(rand()*100 as unsigned) into rand_num;

  -> insert into test_test(num)values(rand_num);

  -> end if;

  -> set i = i +1;

  -> end while;

  -> end if;

  -> end|

  Query OK, 0 rows affected (0.00 sec)

  mysql> delimiter ;

  mysql> show variables like "%pro%"; //查看一下,記錄執(zhí)行的profiling是不是開啟動了,默認是不開啟的

  +---------------------------+-------+

  | Variable_name | Value |

  +---------------------------+-------+

  | profiling | OFF |

  | profiling_history_size | 15 |

  | protocol_version | 10 |

  | slave_compressed_protocol | OFF |

  +---------------------------+-------+

  4 rows in set (0.00 sec)

  mysql> set profiling=1; //開啟后,是為了對比加了索引后的執(zhí)行時間

  Query OK, 0 rows affected (0.00 sec)

  三,實例

  1,單表數(shù)據(jù)太少,索引反而會影響速度

  mysql> call i_test(10,'test_t'); //向test_t表插入10條件

  Query OK, 1 row affected (0.02 sec)

  mysql> select num from test_t where num!=0;

  mysql> explain select num from test_t where num!=0\G;

  *************************** 1. row ***************************

  id: 1

  select_type: SIMPLE

  table: test_t

  type: ALL

  possible_keys: NULL

  key: NULL

  key_len: NULL

  ref: NULL

  rows: 10

  Extra: Using where

  1 row in set (0.00 sec)

  ERROR:

  No query specified

  mysql> create index num_2 on test_t (num);

  Query OK, 10 rows affected (0.19 sec)

  Records: 10 Duplicates: 0 Warnings: 0

  mysql> select num from test_t where num!=0;

  mysql> explain select num from test_t where num!=0\G;

  *************************** 1. row ***************************

  id: 1

  select_type: SIMPLE

  table: test_t

  type: index

  possible_keys: num_2

  key: num_2

  key_len: 4

  ref: NULL

  rows: 10

  Extra: Using where; Using index

  1 row in set (0.00 sec)

  ERROR:

  No query specified

  mysql> show profiles;

  +----------+------------+---------------------------------------------+

  | Query_ID | Duration | Query |

  +----------+------------+---------------------------------------------+

  | 1 | 0.00286325 | call i_test(10,'test_t') | //插入十條數(shù)據(jù)

  | 2 | 0.00026350 | select num from test_t where num!=0 |

  | 3 | 0.00022250 | explain select num from test_t where num!=0 |

  | 4 | 0.18385400 | create index num_2 on test_t (num) | //創(chuàng)建索引

  | 5 | 0.00127525 | select num from test_t where num!=0 | //使用索引后,差不多是沒有使用索引的0.2倍

  | 6 | 0.00024375 | explain select num from test_t where num!=0 |

  +----------+------------+---------------------------------------------+

  6 rows in set (0.00 sec)

  解釋:

  id:表示sql執(zhí)行的順序

  select_type:SIMPLE,PRIMARY,UNION,DEPENDENT UNION,UNION RESULT,SUBQUERY,DEPENDENT SUBQUERY,DERIVED不同的查詢語句會有不同的select_type

  table:表示查找的表名

  type:表示使用索引類型,或者有無使用索引.效率從高到低const、eq_reg、ref、range、index和ALL,其實這個根你sql的寫法有直接關系,例如:能用主鍵就用主鍵,where后面的條件加上索引,如果是唯一加上唯一索引等

  possible_keys:可能存在的索引

  key:使用索引

  key_len:使用索引的長度

  ref:使用哪個列或常數(shù)與key一起從表中選擇行,一般在多表聯(lián)合查詢時會有。

  rows:查找出的行數(shù)

  Extra:額外說明

  前段時間寫過一篇博文mysqldistinct和group by誰更好,里面有朋友留言,說測試結果根我當時做的測試結果不一樣,當時我打比方解釋了一下,今天有時間,以例子的形勢,更直觀的表達出索引的工作原理。

  2,where后的條件,order by ,group by 等這樣過濾時,后面的字段最好加上索引。根據(jù)實際情況,選擇PRIMARY KEY、UNIQUE、INDEX等索引,但是不是越多越好,要適度。

  3,聯(lián)合查詢,子查詢等多表操作時關連字段要加索引

  mysql> call i_test(10,'test_test'); //向test_test表插入10條數(shù)據(jù)

  Query OK, 1 row affected (0.02 sec)

  mysql> explain select a.num as num1,b.num as num2 from test_t as a left join tes

  t_test as b on a.num=b.num\G;

  *************************** 1. row ***************************

  id: 1

  select_type: SIMPLE

  table: a

  type: index

  possible_keys: NULL

  key: num_2

  key_len: 4

  ref: NULL

  rows: 10

  Extra: Using index

  *************************** 2. row ***************************

  id: 1

  select_type: SIMPLE

  table: b

  type: ref

  possible_keys: num_1

  key: num_1

  key_len: 4

  ref: bak_test.a.num //bak_test是數(shù)據(jù)庫名,a.num是test_t的一個字段

  rows: 1080

  Extra: Using index

  2 rows in set (0.01 sec)

  ERROR:

  No query specified

  數(shù)據(jù)量特別大的時候,最好不要用聯(lián)合查詢,即使你做了索引。

掃描/長按二維碼幫助考試通關
2018年等考報考資訊
2018等考各科通關技巧
2018各科最新預測試卷
2018等考各科備考資料

微信搜索"考試吧"了解更多考試資訊、下載備考資料

  相關推薦:

  2018年9月全國計算機等級考試試題匯總(各科目)

  2018年全國計算機等級考試必做試題匯總(各科目)

  2018年全國計算機等級考試復習知識點匯總(各科目)

  2018全國計算機等級考試備考經(jīng)驗及應試技巧匯總

  歷年真題匯總|考試吧策劃:2018年計算機等級考試報考指南

0
收藏該文章
0
收藏該文章
文章搜索
萬題庫小程序
萬題庫小程序
·章節(jié)視頻 ·章節(jié)練習
·免費真題 ·?荚囶}
微信掃碼,立即獲!
掃碼免費使用
版權聲明:如果計算機等級考試網(wǎng)所轉載內容不慎侵犯了您的權益,請與我們聯(lián)系800@exam8.com,我們將會及時處理。如轉載本計算機等級考試網(wǎng)內容,請注明出處。
Copyright © 2004- 考試吧計算機等級考試網(wǎng) 出版物經(jīng)營許可證新出發(fā)京批字第直170033號 
京ICP證060677 京ICP備05005269號 中國科學院研究生院權威支持(北京)
在線模擬試題
考證通關殺器
考試最新資訊
一次通關技巧