Bug #80148 found_rows() is not working as expected without sql_calc_found_rows
Submitted: 26 Jan 2016 7:41 Modified: 31 May 2016 15:59
Reporter: Su Dylan Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.7.8 OS:Any
Assigned to: CPU Architecture:Any

[26 Jan 2016 7:41] Su Dylan
Description:
Output:
=====
mysql> create table t1(c1 int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1),(2),(3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> (select c1 from t1) union all (select c1 from t1) limit 2;
+------+
| c1   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            6 |
+--------------+
1 row in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.8-rc  |
+-----------+
1 row in set (0.00 sec)

Problem:
=====
2 is expected, since sql_calc_found_rows is not specified.

How to repeat:

drop table if exists t1;
create table t1(c1 int);
insert into t1 values(1),(2),(3);
(select c1 from t1) union all (select c1 from t1) limit 2;
select found_rows();

Suggested fix:
2 is returned for select found_rows().
[26 Jan 2016 16:19] MySQL Verification Team
I have tested your report and I have repeated it even with much earlier versions of 5.7.  I even repeated it with 5.6.12.

Correct result returned by "SELECT FOUND_ROWS()" should be indeed 2.

Verified as reported.
[31 May 2016 15:59] Paul DuBois
Posted by developer:
 
Noted in 5.7.14 changelog.

In the absence of SQL_CALC_FOUND_ROWS, FOUND_ROWS() for a UNION
statement always returned the actual number of rows found even when
LIMIT was present.