导航

计算机二级Mysql索引的三个原则

环球网校·2020-10-23 14:40:13浏览27 收藏13

请输入下面的图形验证码

提交验证

预约成功

我知道了
摘要 在计算机二级Mysql中索引重要吗?有多重要,体现在哪里,不清楚的小伙伴看看环球网校(环球青藤旗下品牌)小编整理的‘计算机二级Mysql索引的三个原则’,希望可以帮助你了解计算机二级Mysql中的索引。其他更多计算机二级考试科目资讯敬请关注环球网校计算机二级频道。

计算机二级Mysql索引的三个原则

索引的重要性

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

假如你用新华字典来查找“张”这个汉字,不使用目录的话,你可能要从新华字典的页找到后一页,可能要花二个小时。字典越厚呢,你花的时间就越多。现在你使用目录来查找“张”这个汉字,张的首字母是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)

//创建一个存储过程,为插数据方便

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%"; //查看一下,记录执行的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; //开启后,是为了对比加了索引后的执行时间

Query OK, 0 rows affected (0.00 sec)

以上就是环球网校小编整理的关于“计算机二级Mysql索引的三个原则”,小伙伴们在领取证书时需要了解清楚哦。环球网校友情提示:对于想要参加计算机二级考试的小伙伴们,可“ 免费预约短信提醒”届时环球网校会及时为大家推送2020年计算机二级考试相关信息提醒,请大家持续关注哦!

展开剩余
资料下载
历年真题
精选课程
老师直播

注册电脑版

版权所有©环球网校All Rights Reserved