I had as basis the following Query, running very slow on large tables

SELECT
    fields_list
FROM t1
INNER JOIN t2 on t2.id = t1.t2_id
...
INNER JOIN tn on tN.id = t1.tN_id
ORDER BY t1.id DESC
LIMIT 0 , my_limit


Optimisation on indexes did not gave me a significant result , the query used to run slowly again :(

The dead body was in the ORDER BY clause of the SELECT query

The Solution
Creating a simple view ( or just using sub queries)
CREATE VIEW v_name AS SELECT id from t1 ORDER BY t1.id DESC LIMIT 0 , my_limit

and than implementing an INNER JOIN in the main Query

SELECT
    fields_list
FROM t1
INNER JOIN t2 on t2.id = t1.t2_id
...
INNER JOIN tn on tN.id = t1.tN_id
INNER JOIN v_name as v on v.id = t1.id


This little otimisation made my query running very fast

Tags
Comments
Write the first comment
Leave a trace
Name *
Email *
Website
Anti SPAM * Code (2 + 5) =
Leave me a comment *
 
All comments are subject to editorial review
Post being viewed right now
Item date: 04.02.2009
Views: 465
Item date: 15.05.2009
Views: 631
Item date: 27.09.2009
Views: 1002
Item date: 08.03.2009
Views: 1141
Item date: 12.04.2009
Views: 1663
Item date: 10.04.2009
Views: 1474
Item date: 16.06.2009
Views: 637
Item date: 13.05.2009
Views: 827
Item date: 22.08.2009
Views: 2101