c mysql 延时_Mysql 优化之延迟索引和分页优化

什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!

分页是一个很常见功能,select   **  from tableName limit  ($page -  1 )  * $n ,$n

通过一个存储过程进行测试:

create table smth1 (

id int auto_increment ,

ver int(11) default null,

content varchar(1000) not null,

intro varchar(1000) not null,

primary key(id),

key idver(id,ver)

)engine = innodb default charset = utf8;

create procedure smthTest1()

begin

declare num int default 100001;

while num < 1000000 do

set num := num +1;

insert into smth1 values (num ,num,'我是步迎飞','我是谁');

end while ;

end;

查询:

mysql> show profiles;

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

| Query_ID | Duration | Query |

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

| 1 | 0.002006 | select id ,content from smth1 limit 1000,10 |

| 2 | 0.030106 | select id ,content from smth1 limit 5000,10 |

| 3 | 0.042428 | select id ,content from smth1 limit 9000,10 |

| 4 | 0.01297225 | select id ,content from smth1 limit 10000,10 |

| 5 | 0.13077625 | select id ,content from smth1 limit 20000,10 |

可见随着查询$page 变大,时间会越来越大!

怎样避免这种情况?

一般我们数据库里面数据都不会直接删除,数据时很宝贵的,不舍得删除,另一方便能提高查询数据

先利用索引查询出来数据,再进行联合查询不就行了

select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 1000 limit 10

) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 5000 limit 10

) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 9000 limit 10

) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 10000 limit 10

) as t on C.id = t.id ;

select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 20000 limit 10

) as t on C.id = t.id ;

进行效率分析,没有一个大于1s的

11 | 0.04538625 | select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 5000 limit 10

) as t on C.id = t.id |

| 12 | 0.023278 | select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 9000 limit 10

) as t on C.id = t.id |

| 13 | 0.02320425 | select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 10000 limit 10

) as t on C.id = t.id |

| 14 | 0.001938 | select C.id,C.content from smth1 C inner join

(

select id from smth1 where id > 20000 limit 10

) as t on C.id = t.id |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值