Postgres efficiency
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!
Yes, it is. That is what function volatility is for.
http://www.postgresql.org/docs/current/static/sql-createfunction.html
See VOLATILE vs STABLE vs IMMUTABLE.
精彩评论