-- создание MV
grant create materialized view to ALEX;
-- оптимизация запросов с учетом MV
grant query rewrite to ALEX;
create table SHOPS(
id_shop number(5) PRIMARY KEY,
name_shop varchar2(45) not null
);
create table SALES(
id_shop number(5),
id_good number(5),
sales_good decimal(8,2)
)
alter table SALES
add constraint PK_SALES primary key(id_shop, id_good)
add constraint FK_SHOPS_SALES foreign key (id_shop)
references SHOPS(ID_SHOP)
CREATE MATERIALIZED VIEW MV_SALES
BUILD IMMEDIATE
REFRESH
COMPLETE
ON COMMIT
AS
select sh.id_shop, sum(sa.sales_good)
from SHOPS sh,
SALES sa
where sh.id_shop = sa.id_shop
group by sh.id_shop;
dbms_stats.gather_table_stats(USER, 'MV_SALES') ;
dbms_mview.refresh('MV_SALES', 'A');
SQL> select * from Alex.Mv_Sales
2 /
ID_SHOP SUM(SA.SALES_GOOD)
------- ------------------
1 41,15
2 41,57
3 66,51
SQL> update SALES
2 set sales_good = sales_good + 100
3 where id_shop = 1 and id_good = 1
4 /
1 row updated
SQL> select * from Alex.Mv_Sales
2 /
ID_SHOP SUM(SA.SALES_GOOD)
------- ------------------
1 141,15
2 41,57
3 66,51