I am not a db speccialist, so question =)
How to optimize such:
select count(DISTINCT userid)
from users
where date_trunc('month',login_date)=date_trunc('month','2012-01-12'::date)
Table rows count is less then 1.5 million. I have an index on login_date, although query execution time is same as without it. Also field userid is primary key.
On server machine this query takes some more than 2000 ms. The first guy who can help bring out the best performance will get reputation++ 😀
—————-a’b’c’d’e’f’g’h’ SOLUTION———————————-
CREATE OR REPLACE FUNCTION my_date_trunc_month(some_date DATE)
RETURNS DATE
AS $$
BEGIN
return date_trunc('month',$1);
END;
$$LANGUAGE plpgsql
IMMUTABLE;
CREATE INDEX computedIdx ON gameuser_daily_activity (my_date_trunc_month(login_date));
select count(DISTINCT gameuser_fk) from gameuser_daily_activity where my_date_trunc_month(login_date)=my_date_trunc_month('2012-01-12'::date)
AND it takes 110 ms 🙂
Define this index:
CREATE INDEX computedIdx ON users (date_trunc('month',login_date))