Timmy
Timmy

Reputation: 12828

Optimize SQL Query on calculated value?

I have a query that requires a where statement on a calculated value:

select * from table where date( timestamp ) = ?

An explain on this query yields the expected ALL select type, which is not ideal. Using MySQL, what's the best way to optimize this?

Upvotes: 0

Views: 133

Answers (2)

MindStalker
MindStalker

Reputation: 14864

select * from table where  timestamp  = UNIX_TIMESTAMP('?');

Upvotes: 1

Tom H
Tom H

Reputation: 47454

Another option might be to rewrite the query such that the calculations are all done on the other side of the equation. For example:

timestamp >= <some date> AND timestamp < <some date + 1>

In this query, "some date" would be midnight of that date.

Upvotes: 3

Related Questions