by richardv · 13y ago · 0 comments
The project is pretty cool, but I don't think it's worth adding the dependency lock-in for the functions.
Simply, if you know ahead of time that you want GROUP BY date, then you should create a new column for each interval. `week`, `day`, `hour`.
That way you will have fast queries...
create index foo1 on t1 (date(timestamp))
create index foo2 on t2 (extract(week from timestamp))
Postgres can use almost any row expression as an index.EDIT: Technically chaz is right and a data warehouse puts all these date columns in a separate table you can join against.
group by date(col)
or group by date(col), hour(col), floor(minute(col)/5)
an index on col is enough, or at least the last time I checked for my specific queries it was. If you need something like group by hour-of-day you'll need to create an indexed column of course. (this is with mariadb but I think basic mysql handles it too)Mysql will still use the column for a covering index and run the query in a single-pass, but it will still generate a temporary table for the results (and do a really slow sort unless you do 'order by null'). It doesn't look like mysql has any way of doing date grouping without temporaries even in the trivial cases. I guess eliminating the sort was enough for my queries.
Using date()-like functions, date_format(), and left() all have identical query plans and roughly comparable performance.