【发布时间】:2014-12-10 23:28:21
【问题描述】:
在这个execution plan 中,以下查询花费了大量时间,3.780 秒,仅对 order_line 表(执行计划的第 11 行)执行索引扫描。
表的主键:
Customer PK - c_w_id, c_d_id, c_id
OOrder PK - o_w_id, o_d_id, o_id
Order_line PK - ol_w_id, ol_d_id, ol_o_id, ol_number
Nation PK - n_nationkey
查询:
select c_id, c_last, sum(ol_amount) as revenue, c_city, c_phone, n_name
from customer, oorder, order_line, nation
where c_id = o_c_id
and c_w_id = o_w_id
and c_d_id = o_d_id
and ol_w_id = o_w_id
and ol_d_id = o_d_id
and ol_o_id = o_id
and o_entry_d >= '2007-01-02 00:00:00.000000'
and o_entry_d <= ol_delivery_d
and n_nationkey = ascii(substr(c_state,1,1))
group by c_id, c_last, c_city, c_phone, n_name
order by revenue desc
如何提高此查询的性能?你推荐什么物化视图?这是一个好的选择吗?
CREATE MATERIALIZED VIEW mview AS
select c_id, c_last, ol_amount, c_city, c_phone, o_entry_d, ol_delivery_d, c_state
from customer, oorder, order_line
where c_id = o_c_id
and c_w_id = o_w_id
and c_d_id = o_d_id
and ol_w_id = o_w_id
and ol_d_id = o_d_id
and ol_o_id = o_id;
【问题讨论】:
-
你能做一个
explain select ...并发布输出吗? -
您可以在发布的执行计划中看到这一点……在哪里说 TEXT。谢谢
-
很酷的 postgresql 查询解释网站 :)
-
我也有同样的看法,真的很好:)
标签: sql postgresql database-indexes