Postgres效率

时间:2011-05-12 15:46:19

标签: postgresql

让我们说我有一张桌子,里面有一些钥匙,帐号和点击。

哪个更快?我想我的问题是,如果postgres(或任何sql)有一个智能优化器来查看查询中相同的函数

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;

我很想听到你要说的任何话,或者你可以提供的资源来理解这类事情。谢谢!

1 个答案:

答案 0 :(得分:3)

是的,确实如此。这就是函数波动的含义。

http://www.postgresql.org/docs/current/static/sql-createfunction.html

参见VOLATILE vs STABLE vs IMMUTABLE。