In this execution plan, the following query is spending a lot of time, 3.780 seconds, to perform only a index scan on order_line table (line 11 of execution plan).
Primary keys of tables:
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
Query:
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
How can I improve the performance of this query? What materialized view do you recommend? Is it a good option?
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;
You may try to use expression index for
ascii(substr(c_state,1,1))
. You have sequential scan on customers.