国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

mysql觸發(fā)器實現(xiàn)oracle物化視圖示例代碼

2020-01-18 23:24:10
字體:
供稿:網(wǎng)友

oracle數(shù)據(jù)庫支持物化視圖--不是基于基表的虛表,而是根據(jù)表實際存在的實表,即物化視圖的數(shù)據(jù)存儲在非易失的存儲設(shè)備上。
下面實驗創(chuàng)建ON COMMIT 的FAST刷新模式,在mysql中用觸發(fā)器實現(xiàn)insert , update , delete 刷新操作
1、基礎(chǔ)表創(chuàng)建,Orders 表為基表,Order_mv為物化視圖表

復(fù)制代碼 代碼如下:

mysql> create table Orders(
-> order_id int not null auto_increment,
-> product_name varchar(30)not null,
-> price decimal(10,0) not null ,
-> amount smallint not null ,
-> primary key (order_id));
Query OK, 0 rows affected
mysql> create table Order_mv(
-> product_name varchar(30) not null,
-> price_sum decimal(8.2) not null,
-> amount_sum int not null,
-> price_avg float not null,
-> order_cnt int not null,
-> unique index(product_name));
Query OK, 0 rows affected

2、insert觸發(fā)器
復(fù)制代碼 代碼如下:

delimiter $$
create trigger tgr_Orders_insert
after insert on Orders
for each row
begin
set @old_price_sum=0;
set @old_amount_sum=0;
set @old_price_avg=0;
set @old_orders_cnt=0;

select ifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
from Order_mv
where product_name=new.product_name
into @old_price_sum,@old_amount_sum,@old_price_avg,@old_orders_cnt;

set @new_price_sum=@old_price_sum+new.price;
set @new_amount_sum=@old_amount_sum+new.amount;
set @new_orders_cnt=@old_orders_cnt+1;
set @new_price_avg=@new_price_sum/@new_orders_cnt;

replace into Order_mv
values(new.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
end;
$$
delimiter ;

3、update觸發(fā)器
復(fù)制代碼 代碼如下:

delimiter $$
create trigger tgr_Orders_update
before update on Orders
for each row
begin
set @old_price_sum=0;
set @old_amount_sum=0;
set @old_price_avg=0;
set @old_orders_cnt=0;

set @cur_price=0;
set @cur_amount=0;

select price,amount from Orders where order_id=new.order_id
into @cur_price,@cur_amount;

select ifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
from Order_mv
where product_name=new.product_name
into @old_price_sum,@old_amount_sum,@old_price_avg,@old_orders_cnt;

set @new_price_sum=@old_price_sum-@cur_price+new.price;
set @new_amount_sum=@old_amount_sum-@cur_amount+new.amount;
set @new_orders_cnt=@old_orders_cnt;
set @new_price_avg=@new_price_sum/@new_orders_cnt;

replace into Order_mv
values(new.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
end;
$$
delimiter ;

4、delete觸發(fā)器
復(fù)制代碼 代碼如下:

delimiter $$
create trigger tgr_Orders_delete
after delete on Orders
for each row
begin
set @old_price_sum=0;
set @old_amount_sum=0;
set @old_price_avg=0;
set @old_orders_cnt=0;

set @cur_price=0;
set @cur_amount=0;

select price,amount from Orders where order_id=old.order_id
into @cur_price,@cur_amount;

select ifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
from Order_mv
where product_name=old.product_name
into @old_price_sum,@old_amount_sum,@old_price_avg,@old_orders_cnt;

set @new_price_sum=@old_price_sum - old.price;
set @new_amount_sum=@old_amount_sum - old.amount;
set @new_orders_cnt=@old_orders_cnt - 1;

if @new_orders_cnt>0 then
set @new_price_avg=@new_price_sum/@new_orders_cnt;
replace into Order_mv
values(old.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
else
delete from Order_mv where product_name=@old.name;
end if;
end;
$$
delimiter ;

5、這里delete觸發(fā)器有一個bug,就是在一種產(chǎn)品的最后一個訂單被刪除的時候,Order_mv表的更新不能實現(xiàn),不知道這算不算是mysql的一個bug。當(dāng)然,如果這個也可以直接用sql語句生成數(shù)據(jù),而導(dǎo)致的直接后果就是執(zhí)行效率低。
復(fù)制代碼 代碼如下:

-> insert into Order_mv
-> select product_name ,sum(price),sum(amount),avg(price),count(*) from Orders
-> group by product_name;

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 林芝县| 鹤山市| 鄂州市| 康平县| 冕宁县| 印江| 光山县| 兴海县| 亳州市| 化州市| 天津市| 邓州市| 泽库县| 古丈县| 禹州市| 鄂托克前旗| 柳江县| 贵港市| 商丘市| 石河子市| 田东县| 翁牛特旗| 南宁市| 胶南市| 克拉玛依市| 建瓯市| 平昌县| 盈江县| 两当县| 石城县| 政和县| 桓仁| 成安县| 邵武市| 玛纳斯县| 涞源县| 巴彦县| 孝感市| 桐庐县| 龙南县| 明星|