mysql 索引、没有索引测试对比

28 篇文章 0 订阅
11 篇文章 0 订阅

环境

软件版本
mysql5.5.53
laravel5.6
系统window

结果

例子结果
(1)uname无索引查询2.573300(看记录所在位置,位置越靠后查询时间越长,靠前的查询时间和有索引的没有区别)
(1)uname普通索引(normal)查询0.019300(快135倍)
(2)id唯一索引(unique)查询0.016700
(3)uname-age无索引查询3.430100
(3)uname-age普通组合索引(normal)查询0.020300(快171倍)

数据准备

参考

1.表创建
CREATE TABLE usertb(   
id serial,    
uname  varchar(200) ,   
ucreatetime  datetime  ,   
age  int(11))   
ENGINE=MYISAM 
DEFAULT CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci
AUTO_INCREMENT=1   
ROW_FORMAT=COMPACT; 
2.存储过程
delimiter $$ 
SET AUTOCOMMIT = 0$$   
 
create  procedure test1()  
begin
declare v_cnt decimal (10)  default 0 ; 
dd:loop            
        insert  into usertb values         
        (null,concat('用户',RAND()*1000000),'2010-01-01 00:00:00',FLOOR(6+RAND() * 30))                 
                ;                   
        commit;                     
        set v_cnt = v_cnt+1;                            
            if  v_cnt = 10000000 then leave dd;                           
            end if;          
        end loop dd ; 
end;$$   
 
delimiter ;    
3.执行存储过程
call test1; 

示例代码

不要一起运行,不然得到结果不准确

public function test()
    {

//        $this->time(function (){
//            $model = new Usertb();
//            $data = $model->where('uname','用户9999064')->first();
//        },'uname无索引查询');

//        $this->time(function (){
//            $model = new Usertb();
//            $data = $model->where('uname','用户9999064')->first();
//        },'uname普通索引查询');

//        $this->time(function (){
//            $model = new Usertb();
//            $data = $model->where('id','9999064')->first();
//        },'id唯一索引查询');

//        $this->time(function (){
//            $model = new Usertb();
//            $data = $model->where('uname','用户9999064')->where('age',20)->first();
//        },'uname-age无索引查询');

//        $this->time(function (){
//            $model = new Usertb();
//            $data = $model->where('uname','用户9999064')->where('age',20)->first();
//        },'uname-age普通索引查询');
    }

    /**
     * 时间计算
     * Created by PhpStorm.
     * User: EricPan
     * Date: 2020/2/24
     * Time: 14:03
     * @param $callback
     * @param $str
     */
    public function time($callback,$str)
    {
        $t1 = microtime(true); //获取程序1,开始的时间

        $callback();

        $t2 = microtime(true); //获取程序1,开始的时间
        echo $str.':'.bcsub($t2,$t1,6).'<br/>';
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值