环境
软件 | 版本 |
---|---|
mysql | 5.5.53 |
laravel | 5.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/>';
}