Hey there,
I've been chasing a problem for the project that I'm working on and was able to narrow it down to the ORDER BY clause in my query.
The algorithm looks like this, I take a range of time selected by the user and I need to do some calculations on the items within that range of time. However, before I calculate, I need to query for the most recent row of a specified item before that time range. So my Query statement ends up looking something like this:
SELECT * FROM table WHERE ....... ORDER BY date_time DESC LIMIT 1.
I'm pretty sure the problem is the ORDER BY clause orders the whole db before doing my WHERE clauses and the LIMIT thus it takes an extremely long time when my db is decently large. This is not acceptable and renders the LIMIT clause almost useless with respect to saving time.
If anyone knows of a way to get around this that can boost my execution time, please let me know! Thanks.
I've been chasing a problem for the project that I'm working on and was able to narrow it down to the ORDER BY clause in my query.
The algorithm looks like this, I take a range of time selected by the user and I need to do some calculations on the items within that range of time. However, before I calculate, I need to query for the most recent row of a specified item before that time range. So my Query statement ends up looking something like this:
SELECT * FROM table WHERE ....... ORDER BY date_time DESC LIMIT 1.
I'm pretty sure the problem is the ORDER BY clause orders the whole db before doing my WHERE clauses and the LIMIT thus it takes an extremely long time when my db is decently large. This is not acceptable and renders the LIMIT clause almost useless with respect to saving time.
If anyone knows of a way to get around this that can boost my execution time, please let me know! Thanks.
Comment