开发者

Postgres efficiency

开发者 https://www.devze.com 2023-03-05 23:47 出处:网络
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 funct

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

精彩评论

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

关注公众号