---
title: "mysql小结"
author: "_____bEn-beN"
date: "2014-10-28T14:35:00Z"
tags: []
categories: ["编程语言"]
slug: "mysql"
draft: false
lastmod: "2014-10-28T14:35:00Z"
id: 2148803
url: "https://510891769.iteye.com/blog/2148803"
---

查询业务中，获取首页数据时需要返回满足条件数据的总数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;

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

 

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

 

**写法一的总时长为：0.531 + 0.624 = 1.155；**

**写法二的总时长为：0.639；**

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