在不少分页的程序中都这样写:mysql
#查出符合条件的记录总数 SELECT COUNT(*) from [table] WHERE ......; #查询当页要显示的数据 SELECT * FROM [table] WHERE ...... limit M,N;
可是从Mysql4.0.0开始,咱们能够选择使用另一个方式:sql
SELECT SQL_CALC_FOUND_ROWS * FROM [table] WHERE ...... limit M, N; SELECT FOUND_ROWS(); #SQL_CALC_FOUND_ROWS 告诉MySQL将sql所处理的行数记录下来 #FOUND_ROWS() 则取到了这个纪录。
虽然也是两个语句,可是只执行了一次主查询,因此效率比原来要高不少。this
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() 是很是有用的!
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'
#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)
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()返回的时显示的行数!rest
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倍的行数code