Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This SELECT query takes 180 seconds to finish

Tags:

sql

select

mysql

UPDATE:

Just to mention it on a more visible place. When I changed IN for =, the query execution time went from 180 down to 0.00008 seconds. Ridiculous speed difference.


This SQL query takes 180 seconds to finish! How is that possible? is there a way to optimize it to be faster?

SELECT IdLawVersionValidFrom 
FROM question_law_version 
WHERE IdQuestionLawVersion IN 
  (
  SELECT MAX(IdQuestionLawVersion) 
  FROM question_law_version 
  WHERE IdQuestionLaw IN 
    (
    SELECT MIN(IdQuestionLaw) 
    FROM question_law 
    WHERE IdQuestion=236 AND IdQuestionLaw>63
    )
  )

There are only about 5000 rows in each table so it shouldn't be so slow.

like image 802
Richard Knop Avatar asked Aug 05 '10 15:08

Richard Knop


People also ask

How can I tell how long a SQL query will take?

Using Client StatisticsGo to Menu >> Query >> Select Include client Statistics. Execute your query. In the results panel, you can see a new tab Client Statistics. Go to the Client Statistics tab to see the execution time.

Why is limit offset slow?

Why OFFSET is so slow? Well, in most cases, low offset queries are not slow. The problem starts with high OFFSET values. If your query is using the following limit clause: “LIMIT 50000, 20”, it's actually requesting the database to go through 50,020 rows and throw away the first 50,000.

How does mysql calculate query execution time?

Once executed, you can check the query execution time using the below query: show profiles; You will be able to see the duration of query execution in seconds. These ways are fine when you want to measure the query time for one or a few queries.


1 Answers

(Posting my comment as an answer as apparently it did make a difference!)

Any difference if you change the IN to =?

If anyone wants to investigate this further I've just done a test and found it very easy to reproduce.

Create Table

CREATE TABLE `filler` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) 

Create Procedure

CREATE PROCEDURE `prc_filler`(cnt INT)
BEGIN
        DECLARE _cnt INT;
        SET _cnt = 1;
        WHILE _cnt <= cnt DO
                INSERT
                INTO    filler
                SELECT  _cnt;
                SET _cnt = _cnt + 1;
        END WHILE;
END

Populate Table

  call prc_filler(5000)

Query 1

SELECT id 
FROM filler 
WHERE id =  (SELECT MAX(id) FROM filler  WHERE id =   
 ( SELECT MIN(id) 
    FROM filler
    WHERE id between 2000 and 3000
    )
  )

Equals Explain Output http://img689.imageshack.us/img689/5592/equals.png

Query 2 (same problem)

SELECT id 
FROM filler 
WHERE id in  (SELECT MAX(id) FROM filler  WHERE id in   
 ( SELECT MIN(id) 
    FROM filler
    WHERE id between 2000 and 3000
    )
  )

In Explain Output http://img291.imageshack.us/img291/8129/52037513.png

like image 82
Martin Smith Avatar answered Sep 21 '22 22:09

Martin Smith