No, donβt do anything special. Your problem can be easily solved by changing the problem.
The problem with your current implementation is that the function must execute on every row of the joined table for every row in the main table, which has 2 unpleasant effects:
- he is slow. You must execute the function nxm times
- you cannot use the index (if there is one and should be) in the datetime column of the joined table
But there is an easy way to avoid all this muck. Calculate the beginning and end of the year as the time from the year value and use it to join another table in your datetime between these two values. It:
- Only calculate start and end dates once per row in the main table.
- allow the use of the index on the datetime column (you must add it if it is not already)
I donβt really understand that you are right enough to use real code, so here is what the pseudo-request will look like:
select * from table1 join table2 on table2.datetime between <calculate start of year from table1.year> and <calculate end of year from table1.year>
source share