1. SQL_CALC_FOUND_ROWS简述

在很多分页的程序中都这样写:

#查出符合条件的记录总数
SELECT COUNT(*) from [table] WHERE ......;
#查询当页要显示的数据
SELECT * FROM [table] WHERE ...... limit M,N;

但是从Mysql4.0.0开始,我们可以选择使用另外一个方式:

SELECT SQL_CALC_FOUND_ROWS * FROM [table] WHERE ......  limit M, N;
SELECT FOUND_ROWS(); #SQL_CALC_FOUND_ROWS 告诉MySQL将sql所处理的行数记录下来
#FOUND_ROWS() 则取到了这个纪录。

虽然也是两个语句,但是只执行了一次主查询,所以效率比原来要高很多。

2.SQL_CALC_FOUND_ROWS FOUND_ROWS()文档中英对照

FOUND_ROWS()
A SELECT statement may include a LIMIT clause to restrict the number of rows the server returns
to the client. In some cases, it is desirable to know how many rows the statement would have
returned without the LIMIT, but without running the statement again. To obtain this row count,
include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward: SELECT语句中经常可能用LIMIT限制返回行数。有时候可能想要知道如果没有LIMIT会返回多少行,但又不想再执行一次相同
语句。那么,在SELECT查询中包含SQL_CALC_FOUND_ROWS选项,然后执行FOUND_ROWS()就可以了: mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10; mysql> SELECT FOUND_ROWS(); The second SELECT returns a number indicating how many rows the first SELECT would have returned
had it been written without the LIMIT clause. 第二个SELECT将返回第一条SELECT如果没有LIMIT时返回的行数。 In the absence of the SQL_CALC_FOUND_ROWS option in the most recent SELECT statement,
FOUND_ROWS() returns the number of rows in the result set returned by that statement. 如果在前一条语句中没有使用SQL_CALC_FOUND_ROWS选项,FOUND_ROWS()将返回前一条语句实际返回的行数。 The row count available through FOUND_ROWS() is transient and not intended to be available
past the statement following the SELECT SQL_CALC_FOUND_ROWS statement. If you need to
refer to the value later, save it: FOUND_ROWS()得到的数字是临时的,执行下一条语句就会失效。如果想要这个数字,就要将它保存下来: mysql> SELECT SQL_CALC_FOUND_ROWS * FROM ... ;
mysql> SET @rows = FOUND_ROWS(); If you are using SELECT SQL_CALC_FOUND_ROWS, mysql must calculate how many rows are in the full
result set. However, this is faster than running the query again without LIMIT, because the
result set need not be sent to the client. 如果使用 SELECT SQL_CALC_FOUND_ROWS,MySQL必须计算所有结果集的行数。尽管这样,总比再执行一次不使用LIMIT
的查询要快多了,因为结果集不需要返回客户端。 SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the
number of rows that a query returns, but also determine the number of rows in the full result
set without running the query again. 当你想要限制查询的返回行数的同时又想得到查询的完整结果集合的行数,但又不想重复执行一次查询,那么
SQL_CALC_FOUND_ROWS and FOUND_ROWS() 是非常有用的!

3.UNION 语句下的用法

3.1 SQL_CALC_FOUND_ROWS只能出现在UNION的第一个SELECT中,否则编译报错!

mysql> (select  SQL_CALC_FOUND_ROWS  * from  actor  limit 1)  union    (select SQL_CALC_FOUND_ROWS  * from  actor limit 1);
ERROR 1234 (42000): Incorrect usage/placement of 'SQL_CALC_FOUND_ROWS'

3.2 limit中 select 中的情况

#union all
mysql> (select SQL_CALC_FOUND_ROWS * from actor limit 1) union all (select * from actor limit 1);
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
2 rows in set (0.00 sec) mysql> select FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)

union

mysql> (select  SQL_CALC_FOUND_ROWS  * from  actor  limit 1)  union    (select * from  actor limit 1);
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
1 row in set (0.00 sec) mysql> select FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)

可以看到,limit中 select 中时,FOUND_ROWS()返回的时显示的行数!

3.2 limit中 UNION 外的情况

mysql> (select  SQL_CALC_FOUND_ROWS  * from  actor  )  union    (select   * from  actor ) order by actor_id limit 2  ;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
2 rows in set (0.00 sec) mysql> select FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 200 |
+--------------+
1 row in set (0.00 sec) mysql> (select SQL_CALC_FOUND_ROWS * from actor ) union all (select * from actor ) order by actor_id limit 2 ;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
+----------+------------+-----------+---------------------+
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
| 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
+----------+------------+-----------+---------------------+
2 rows in set (0.01 sec) mysql> select FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 400 |
+--------------+
1 row in set (0.00 sec)

可以看到,limit中 UNION 外时,FOUND_ROWS()返回的所有的行数! UNION ALL 会返回2倍的行数

MySQL中SQL_CALC_FOUND_ROWS的用法的更多相关文章

  1. mysql中event的用法详解

    一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...

  2. mysql中limit的用法实例解析

    mysql中limit的用法解析. 在mysql中,select * from table limit m,n.其中m是指记录开始的index,从0开始,n是指从第m条开始,取n条. 例如: mysq ...

  3. Mysql中limit的用法详解

    Mysql中limit的用法详解 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,为我们提供了limit这样一个功能. SELECT * FROM table LIMIT [offset ...

  4. mysql中explain的用法

    mysql中explain的用法 最近在做性能测试中经常遇到一些数据库的问题,通常使用慢查询日志可以找到执行效果比较差的sql,但是仅仅找到这些sql是不行的,我们需要协助开发人员分析问题所在,这就经 ...

  5. MySQL中CONCAT()的用法

    MySQL中CONCAT()的用法 在日常开发过程中,特别是在书写接口的时候,经常会遇到字符串拼接的情况,比如在返回图片数据时,数据库里往往存储的是相对路径,而接口里一般是存放绝对地址,这就需要字符串 ...

  6. mysql中FIND_IN_SET函数用法

    本篇文章主要介绍mysql中FIND_IN_SET函数用法,用来精确查询字段中以逗号分隔的数据 以及其与 like 和 in 的区别 1.问题发现 之前在做工作任务时有这么一个需求:需要用接口所传的服 ...

  7. mySQL中replace的用法

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪   mysql replace实例说明: ...

  8. Mysql中autocommit的用法

    定义 Mysql文档原文:SET autocommit disables or enables the default autocommit mode for the current session. ...

  9. Mysql中EXISTS关键字用法、总结

    在做教务系统的时候,一个学生(alumni_info)有多个教育经历(alumni_education),使用的数据库是mysql,之前使用左链接查询的,发现数据量才只有几万条时,查询就很慢了,早上想 ...

随机推荐

  1. 用I/O口模拟总线时序

    在做总线通信过程中,我们很少会用到这样方法,一般在我们选择MCU的时候都会带有你所需要的通信接口.但是,对于一些简单的通信应该用的场合,一般在一些传感器的数据通信过程中,传感器厂商会将通信协议做一些改 ...

  2. LeetCode OJ:Roman to Integer(转换罗马字符到整数)

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  3. flash播放器插件与flash播放器的区别

    flash插件是一个网页ActiveX控件,而flash播放器是一个exe的可执行程序.前者用于播放网页中的falsh动画,而后者用于播放本地swf格式文件.

  4. canvas - 柱子效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. Arcgis for Js实现graphiclayer的空间查询(续)

    上文中,实现了简单的针对graphiclayer的空间查询工作,在本节,将更加详细的介绍针对graphiclayer的空间查询.首先,空间查询的方式:提供多种类型的空间查询,包括点周边.线周边.面内等 ...

  6. Manual Install Cocos2d-x vc template on Windows 7

    Manual Installation Process Download the template file from HERE and extract it. Open the file CCApp ...

  7. 从无到有开发自己的Wordpress博客主题---创建主题

    上一篇教程,我们已经安装了Wordpress,我们可以成功的登录到Wordpress后台,接下来的任务就是创建我们自己的主题. 要想创建一个Wordpress主题,就必须按照Wordpress的规则, ...

  8. 你必须知道的495个C语言问题,学习体会三

    本文是 本系列的第三篇,本文主要对C语言的表达式做个小结 先从两个坑爹的表达式说起:i++ 与++i 上大学的时候,学长告诉我,这两个表达式,意义是一样的,后来老师纠正说,还是有区别的,于是让我们记住 ...

  9. Java泛型小记

    Automobile类: public class Automobile { private String name; public Automobile(String name){ this.nam ...

  10. Hibernate中 一 二级缓存及查询缓存(1)

    最近趁有空学习了一下Hibernate的缓存,其包括一级缓存,二级缓存和查询缓存(有些是参照网络资源的): 一.一级缓存     一级缓存的生命周期和session的生命周期一致,当前sessioin ...