I have two tables: one has a datetime column, the other has a date column.
I want to join the two tables like this:
SELECT
t1.dt,
t2.d
FROM
table1 t1,
JOIN
table2 t2
ON
DATE(t1.dt) = t2.date
But obviously, this kills the index on t1.dt.
What’s the best strategy to get around this? The simplest approach would be to add a new column to t1 (and set it equal to date(t1.dt)), but I don’t like the idea of duplicating data.
Is there a neat way to get around this?
Your datetime column consists of a date and a time part. As you would like to index the date part individually, it would make sense to split the datetime column into two individual column, one for the time part and the other one for the date part. The date column can then be indexed and the index can be used in your query. With this approach you do not duplicate any data at all.