Jump to content

my mysql query optimization,execute query fast


Kamlesh_Rathore

Recommended Posts

SELECT train_no,train_name
FROM trains
WHERE train_no
IN (

SELECT s1.train_no
FROM train_schedule AS s1
INNER JOIN train_schedule AS s2
WHERE s1.stn_code = 'JU'
AND s2.stn_code = 'JP'
AND s1.distance < s2.distance
AND s1.train_no = s2.train_no
) ORDER BY train_no LIMIT 0 , 30


this query takes 50 seconds to executes on my server
i want quick response

please help what kind of changes i should done

Edited by Kamlesh_Rathore
Link to comment
Share on other sites

Does this help

SELECT t.train_no, t.train_name
FROM trains t
    INNER JOIN train_schedule AS s1 
        ON t.train_no = s1.train_no AND s1.stn_code = 'JU'
    INNER JOIN train_schedule AS s2
        ON t.train_no = s2.train_no AND s2.stn_code = 'JP'
WHERE s1.distance < s2.distance
ORDER BY train_no 
LIMIT 0 , 30

If not, can you post a test dump of the two  tables

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.