I’ve tested the following seemingly simple query on MySQL 5.0, 5.1, 5.5 and found it to be extremely slow.
select * from entry where session_id in
(select session_id from entry where created_at > [some timestamp])
Multiple entry’s can have the same session ID, but different created_at timestamps.
The query is meant to grab all entry’s that have at least one entry from the same session_id whose created_at is greater than the specified timestamp.
I’ve seen others speak of MySQL subquery performance issues with similar queries, and that MySQL considers the subquery a dependent query and it is doing a full table scan on the outer query. Suggested workarounds were something like:
select * from entry where session_id in
(select session_id from
(select session_id from entry where created_at > [some timestamp])
as temp)
However, this hack doesn’t work for me and makes it even slower.
Any ideas on how to rewrite this query?
Depending on your data distribution, use this
(create an index on
(session_id, created_at)), or this:(create two separate indexes on
created_atandsession_id)