设为首页 加入收藏

TOP

添加mysql索引的3条原则
2014-11-24 03:16:28 来源: 作者: 【 】 浏览:2
Tags:添加 mysql 索引 原则

  一,索引的重要性

  索引用于快速找出在某个列中有一特定值的行。不使用索引,MySQL必须从第1条记录开始然后读完整个表直到找出相关的行。表越大,花费的时间越多。如果表中查询的列有一个索引,MySQL能快速到达一个位置去搜寻到数据文件的中间,没有必要看所有数据。注意如果你需要访问大部分行,顺序读取要快得多,因为此时我们避免磁盘搜索。

  假如你用新华字典来查找“张”这个汉字,不使用目录的话,你可能要从新华字典的第一页找到最后一页,可能要花二个小时。字典越厚呢,你花的时间就越多。现在你使用目录来查找“张”这个汉字,张的首字母是z,z开头的汉字从900多页开始,有了这条线索,你查找一个汉字可能只要一分钟,由此可见索引的重要性。但是索引建的是不是越多越好呢,当然不是,如果一本书的目录分成好几级的话,我想你也会晕的。

  二,准备工作

  
  1. //准备二张测试表
  2. mysql> CREATE TABLE `test_t` (
  3. -> `id` int(11) NOT NULL auto_increment,
  4. -> `num` int(11) NOT NULL default 0,
  5. -> `d_num` varchar(30) NOT NULL default 0,
  6. -> PRIMARY KEY (`id`)
  7. -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  8. Query OK, 0 rows affected (0.05 sec)
  9. mysql> CREATE TABLE `test_test` (
  10. -> `id` int(11) NOT NULL auto_increment,
  11. -> `num` int(11) NOT NULL default 0,
  12. -> PRIMARY KEY (`id`)
  13. -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  14. Query OK, 0 rows affected (0.05 sec)
  15. //创建一个存储过程,为插数据方便
  16. mysql> delimiter |
  17. mysql> create procedure i_test(pa int(11),tab varchar(30))
  18. -> begin
  19. -> declare max_num int(11) default 100000;
  20. -> declare i int default 0;
  21. -> declare rand_num int;
  22. -> declare double_num char;
  23. ->
  24. -> if tab != test_test then
  25. -> select count(id) into max_num from test_t;
  26. -> while i < pa do
  27. -> if max_num < 100000 then
  28. -> select cast(rand()*100 as unsigned) into rand_num;
  29. -> select concat(rand_num,rand_num) into double_num;
  30. -> insert into test_t(num,d_num)values(rand_num,double_num);
  31. -> end if;
  32. -> set i = i +1;
  33. -> end while;
  34. -> else
  35. -> select count(id) into max_num from test_test;
  36. -> while i < pa do
  37. -> if max_num < 100000 then
  38. -> select cast(rand()*100 as unsigned) into rand_num;
  39. -> insert into test_test(num)values(rand_num);
  40. -> end
    <script type="text/java script">BAIDU_CLB_fillSlot("771048");
    点击复制链接 与好友分享! 回本站首页
    <script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
    分享到: 更多
    <script type="text/java script" id="bdshare_js" data="type=tools&uid=12732"> <script type="text/java script" id="bdshell_js"> <script type="text/java script"> var bds_config = {'snsKey':{'tsina':'2386826374','tqq':'5e544a8fdea646c5a5f3967871346eb8'}}; document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js cdnversion=" + Math.ceil(new Date()/3600000)
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇教你在MySQL 5.0以上版本中配置主.. 下一篇MySQL数据库主从同步第四版

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)