开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜