MySQL subquery really slow… Workaround?

Depending on your data distribution, use this SELECT e. * FROM ( SELECT session_id, MAX(created_at) FROM entry GROUP BY session_id HAVING MAX(created_at) > $mytimestamp ) ed JOIN entry e ON e. Session_id = ed.

Session_id (create an index on (session_id, created_at) ), or this: SELECT DISTINCT e. * FROM entry ed JOIN entry e ON e. Session_id = ed.

Session_id WHERE ed. Created_at > $mytimestamp (create two separate indexes on created_at and session_id ).

Depending on your data distribution, use this SELECT e. * FROM ( SELECT session_id, MAX(created_at) FROM entry GROUP BY session_id HAVING MAX(created_at) > $mytimestamp ) ed JOIN entry e ON e. Session_id = ed.

Session_id (create an index on (session_id, created_at)), or this: SELECT DISTINCT e. * FROM entry ed JOIN entry e ON e. Session_id = ed.

Session_id WHERE ed. Created_at > $mytimestamp (create two separate indexes on created_at and session_id).

You da man. Thanks! I started off with something like the 2nd query, but left out distinct.

I didn't know you can apply distinct to a whole result set with a wildcard. Awesome. – n00b May 13 at 22:05.

How about: SELECT DISTINCT e2. * FROM entry e1 INNER JOIN entry e2 ON e1. Session_id = e2.

Session_id WHERE e1. Created_at > some timestamp If you don't already have them, indexes on created_at and session_id would probably be helpful as well.

You would also need group by e2. Id or you will get duplicates. – Imre L May 13 at 22:02 Thanks.

That's what I initially tried, actually. This was giving me duplicates. Didn't occur to me to use group by.

Or you can use distinct e2. *. – n00b May 13 at 22:05 Added correction.

– Joe Stefanelli May 13 at 23:02.

I was having a problem with the double subquery trick too, btw I just found out that using this worked for me (based on your query): select * from entry where session_id in (select (select session_id from entry where created_at > some timestamp)) In my case the original query could work for hours using a join or the "normal" double subquery trick, with the modified double subquery it took 0 secs :).

This is very useful because very easy to apply! However it's strange this behavior of MySQL... – Pisu Oct 3 at 16:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions