0

I have a nested MySQL query having relation in tables with over 500000 records in each. The query takes 60 seconds to fetch results and Indexing has been done in all tables.

Please suggest to reduce its execution time. Thanks in advance.

    SELECT t1.col1,t1.col2
    FROM table1 AS t1
    WHERE t1.col2 IN
    (
        SELECT DISTINCT(t2.col1) FROM table2 AS t2 WHERE t2.col2 IN
        (
            SELECT t3.col1
            FROM  table3 AS t3
            WHERE t3.col2 = '04' ORDER BY t3.col1 ASC
        )
        ORDER BY t2.col1 ASC
    )
3
  • why do you use ordering??? Commented Aug 23, 2016 at 12:49
  • 2
    You should only order your final result set. Try profiling the query to see what is causing the long execution time; It could well be the DISTINCT clause you have as they can often cause delay. In addition you could try using a JOIN instead of WHERE ... IN as that can often be faster. Commented Aug 23, 2016 at 12:53
  • seconding @MartinParkin, you should easily be able to rewrite the query to use joins instead of where...in Commented Aug 23, 2016 at 12:54

3 Answers 3

3

My earlier comment was:

You should only order your final result set. Try profiling the query to see what is causing the long execution time; It could well be the DISTINCT clause you have as they can often cause delay. In addition you could try using a JOIN instead of WHERE ... IN as that can often be faster.

Not tested code but further to my comment earlier, the same query using JOINs would look similar to:

SELECT  t1.col1,
        t1.col2
  FROM  table1 t1
    INNER JOIN table2 t2 ON t2.col1 = t1.col2
    INNER JOIN table3 t3 ON t3.col1 = t2.col2
  WHERE t3.col2 = '04'
  ORDER BY t2.col1, t3.col1

I would expect such a query to perform significantly faster than using WHERE ... IN.

Sign up to request clarification or add additional context in comments.

Comments

2

IN clause makes a full table scan. I believe you will have much better performance if you use inner join, like:

SELECT t1.col1,t1.col2
    FROM table1 AS t1
INNER JOIN table2 t2
    ON t1.col2=t2.col1
INNER JOIN table3 t3
    ON t2.col1=t3.col1
WHERE  t3.col2 = '04'
ORDER BY t3.col1 ASC,t2.col1 ASC

1 Comment

Nested select queries are always slow and heavy because they store intermediate data in memory. Switch your nested queries to a join (as it showed by @apomene), it will prevent memory load and improve execution speed.
0

You can also use EXISTS condition like

Select t1.col1,t1.col2 FROM table1 AS t1 WHERE EXISTS (
 Select t2.col1 FROM table2 as t2 where EXISTS (
  Select t3.col1 FROM table3 AS t3 where t3.col2 = '04' and t2.col2=t3.col1
 ) and t1.col2=t2.col1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.