`
_____bEn-beN
  • 浏览: 13652 次
社区版块
存档分类
最新评论

mysql小结

 
阅读更多

查询业务中,获取首页数据时需要返回满足条件数据的总数count,并返回满足分页pageSize条数数据。

 

写法一

第一步,获取满足条件总数

if (i_in_page = 1) then

    select count(*) into i_o_count from t_comment t where t.userid = '12311221212';

end if;

 

第二步,获取分页条数据

select t.albumid from t_comment t where t.userid = '12311221212' limit 10;

必须进行两次查询操作才能获取到业务数据。

 

写法二

在获取满足条件数据时,统计满足条数的数据总量

select SQL_CALC_FOUND_ROWS t.albumid from t_comment t where t.userid = '12311221212' limit 10;

select found_rows() into i_o_count;

只需一次表查询即可满足业务。

 

以下为实际语句执行效率对比图:

实际执行数据对比.jpg

 

写法一的总时长为:0.531 + 0.624 = 1.155

写法二的总时长为:0.639

通过1000次的测试,写法二的响应速度明显比写法一快。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics