0

In a nutshell, this query returns 890738 entries

SELECT * 
FROM  `cms_question_report` 
WHERE  `doa` <  '2014-12-16 11:48:13' 

And it does it that in around 2 seconds

After trying to cut it into 4 chunks, this query times out and produces an error

SELECT * 
FROM  `cms_question_report` 
WHERE  `doa` <  '2014-12-16 11:48:13'
LIMIT 222684

Here's the error:

Error in Processing Request
Error code: 500
Error text: Internal Server Error

In my basic understanding shouldn't the second one run faster as it has a lower limit to the data it's fetching?

Another test:

SELECT * 
FROM  `cms_question_report` 
WHERE  `doa` <  '2014-12-16 11:48:13'
LIMIT 2

That worked smoothly

Naguib Ihab
  • 4,259
  • 7
  • 44
  • 80

2 Answers2

0

No it will not run faster. The db has to first get all the results and ORDER them before applying a LIMIT. I know this seems a waste of resource, why not just stop after getting LIMIT rows? Because they have to be ordered, even when that order is implicit because you have not specified an explicit order. The implicit order is the PK order of the main table in the query, but the WHERE clause means that the rows will not automaticaly be found in that order. So all rows have to be found, then ordered, then limited.

David Soussan
  • 2,698
  • 1
  • 16
  • 19
  • mm so it's actually taking more time. What do you mean by "why not just stop after getting LIMIT rows?", I have to have the WHERE clause. Is there another method to divide large chunk of data other than LIMIT? – Naguib Ihab Mar 17 '15 at 03:50
  • I meant the logic of the database engine of course, basic rhetorical english. Sorry of english is not your first language so you did not understand it. Why does the database not stop after finding limit rows? is the full expanded meaning. Have a look at the question @Roy linked and then the article link in the answers. – David Soussan Mar 17 '15 at 03:54
0

Found the solution thanks to Roy Shmuli. The query that solved the issue was:

SELECT Y.* FROM (SELECT id FROM `cms_question_report` WHERE doa < '2014-12-16 11:48:13' ORDER BY id LIMIT 295354) X JOIN `cms_question_report` Y ON X.id = Y.id

And I got it from this link: http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/

Naguib Ihab
  • 4,259
  • 7
  • 44
  • 80