Lets say that I have a table with some key, accounts, and hits.
Which is faster? I guess my question is if postgres's (or any sql for that matter) has an optimizer intelligent to see identical functions in a query
SELECT key, accounts, hits,1.0*hits/accounts as ratio FROM
(
SELECT key, COUNT(DISTINCT accounts) as accounts, SUM(hits) as hits
FROM table
GROUP BY key
) a;
OR
SELECT key, COUNT(DISTINCT accounts) as accounts, SUM(hits) as hits, 1.0*SUM(hits)/COUNT(DISTINCT accounts) as ratio
FROM table
GROUP BY key;
I'd love to hear anything you have to say or resources you can provide on understanding this sort of thing. Thanks!