Eva*_*oll 10 postgresql where-clause window-functions
Pg's Window函数的文档说:
窗口函数考虑的行是由查询的FROM子句生成的"虚拟表",由WHERE,GROUP BY和HAVING子句过滤,如果有的话.例如,任何窗口函数都看不到因为它不符合WHERE条件而被删除的行.查询可以包含多个窗口函数,这些函数通过不同的OVER子句以不同的方式对数据进行分片,但它们都作用于此虚拟表定义的相同行集合.
但是,我没有看到这一点.在我看来,选择过滤器非常靠近左边距和顶部(最后完成的事情).
=# EXPLAIN SELECT * FROM chrome_nvd.view_options where fkey_style = 303451;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Subquery Scan view_options (cost=2098450.26..2142926.28 rows=14825 width=180)
Filter: (view_options.fkey_style = 303451)
-> Sort (cost=2098450.26..2105862.93 rows=2965068 width=189)
Sort Key: o.sequence
-> WindowAgg (cost=1446776.02..1506077.38 rows=2965068 width=189)
-> Sort (cost=1446776.02..1454188.69 rows=2965068 width=189)
Sort Key: h.name, k.name
-> WindowAgg (cost=802514.45..854403.14 rows=2965068 width=189)
-> Sort (cost=802514.45..809927.12 rows=2965068 width=189)
Sort Key: h.name
-> Hash Join (cost=18.52..210141.57 rows=2965068 width=189)
Hash Cond: (o.fkey_opt_header = h.id)
-> Hash Join (cost=3.72..169357.09 rows=2965068 width=166)
Hash Cond: (o.fkey_opt_kind = k.id)
-> Seq Scan on options o (cost=0.00..128583.68 rows=2965068 width=156)
-> Hash (cost=2.21..2.21 rows=121 width=18)
-> Seq Scan on opt_kind k (cost=0.00..2.21 rows=121 width=18)
-> Hash (cost=8.80..8.80 rows=480 width=31)
-> Seq Scan on opt_header h (cost=0.00..8.80 rows=480 width=31)
(19 rows)
Run Code Online (Sandbox Code Playgroud)
这两个WindowAgg基本上将计划改为似乎永远不会更快完成的事情
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------
Subquery Scan view_options (cost=329.47..330.42 rows=76 width=164) (actual time=20.263..20.403 rows=42 loops=1)
-> Sort (cost=329.47..329.66 rows=76 width=189) (actual time=20.258..20.300 rows=42 loops=1)
Sort Key: o.sequence
Sort Method: quicksort Memory: 35kB
-> Hash Join (cost=18.52..327.10 rows=76 width=189) (actual time=19.427..19.961 rows=42 loops=1)
Hash Cond: (o.fkey_opt_header = h.id)
-> Hash Join (cost=3.72..311.25 rows=76 width=166) (actual time=17.679..18.085 rows=42 loops=1)
Hash Cond: (o.fkey_opt_kind = k.id)
-> Index Scan using options_pkey on options o (cost=0.00..306.48 rows=76 width=156) (actual time=17.152..17.410 rows=42 loops=1)
Index Cond: (fkey_style = 303451)
-> Hash (cost=2.21..2.21 rows=121 width=18) (actual time=0.432..0.432 rows=121 loops=1)
-> Seq Scan on opt_kind k (cost=0.00..2.21 rows=121 width=18) (actual time=0.042..0.196 rows=121 loops=1)
-> Hash (cost=8.80..8.80 rows=480 width=31) (actual time=1.687..1.687 rows=480 loops=1)
-> Seq Scan on opt_header h (cost=0.00..8.80 rows=480 width=31) (actual time=0.030..0.748 rows=480 loops=1)
Total runtime: 20.893 ms
(15 rows)
Run Code Online (Sandbox Code Playgroud)
发生了什么,我该如何解决?我正在使用Postgresql 8.4.8.以下是实际视图的作用:
SELECT o.fkey_style, h.name AS header, k.name AS kind
, o.code, o.name AS option_name, o.description
, count(*) OVER (PARTITION BY h.name) AS header_count
, count(*) OVER (PARTITION BY h.name, k.name) AS header_kind_count
FROM chrome_nvd.options o
JOIN chrome_nvd.opt_header h ON h.id = o.fkey_opt_header
JOIN chrome_nvd.opt_kind k ON k.id = o.fkey_opt_kind
ORDER BY o.sequence;
Run Code Online (Sandbox Code Playgroud)
不,PostgreSQL 只会在没有聚合的 VIEW 上下推 WHERE 子句。(窗口函数被认为是聚合)。
< x> 我认为这只是一个实现限制
< EvanCarroll> x:我想知道在这种情况下需要做些什么来推动 WHERE 子句。
<EvanCarroll> 计划者必须知道 WindowAgg 本身不会增加选择性,因此将 WHERE 推下是安全的?
<x>埃文卡罗尔;很多非常复杂的工作与规划师,我猜
和,
< a> 埃文卡罗尔:不。视图上的过滤条件适用于视图的输出,并且只有在视图不涉及聚合时才会被下推
| 归档时间: |
|
| 查看次数: |
2259 次 |
| 最近记录: |