[Solved] Limit in mysql is timing out

EverSQL Database Performance Knowledge Base

Limit in mysql is timing out

Database type:

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

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `cms_question_report` ADD INDEX `cms_report_idx_doa` (`doa`);
The optimized query:
SELECT
        * 
    FROM
        `cms_question_report` 
    WHERE
        `cms_question_report`.`doa` < '2014-12-16 11:48:13'

Related Articles



* original question posted on StackOverflow here.