0

Today while I was writing a complex query, accidently I found that even I have set LIMIT in my query MySQL server returns the total number of rows for COUNT.


Example:

SELECT COUNT(*) FROM `log` LIMIT 10;

Output:

5219

But if I run the query without COUNT it returns only 10 rows. My question is,

Why does MySQL ignore LIMIT when the COUNT is present?

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80

2 Answers2

1

LIMIT is for returning a subset of the total results, in your case the result is only one line so no effect

Yves M.
  • 29,855
  • 23
  • 108
  • 144
trapper
  • 11,716
  • 7
  • 38
  • 82
0

The purpose of LIMIT clause to select a limited number of records whereas count is aggregate function, which will return result of aggregation. It won't make any sense to use LIMIT and COUNT together as LIMIT may return any random n records.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • `LIMIT` have never returned random rows for me, results are always returned in an order even without a primary key. – Roshana Pitigala Dec 19 '18 at 04:47
  • @RoshanaPitigala that is the reason I said, **may**. Because, as per documentation, *MySQL uses indexes in some cases when normally it would prefer to do a full table scan* – Ravi Dec 19 '18 at 04:53