亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2014年02月08日 15:27:01   作者:  
mysql觸發(fā)器實現(xiàn)oracle物化視圖即不是基于基表的虛表,而是根據(jù)表實際存在的實表,需要的朋友可以參考下

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

復制代碼 代碼如下:

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ā)器
復制代碼 代碼如下:

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ā)器
復制代碼 代碼如下:

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ā)器
復制代碼 代碼如下:

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。當然,如果這個也可以直接用sql語句生成數(shù)據(jù),而導致的直接后果就是執(zhí)行效率低。
復制代碼 代碼如下:

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

相關文章

  • MySQL binlog日志清理的方案分享

    MySQL binlog日志清理的方案分享

    Binlog日志非常重要,但是占用的磁盤空間也很大,我們也需要定期的去清理二進制日志,在MySQL數(shù)據(jù)庫中,提供了自動清理Binlog日志的參數(shù),本文給大家詳細介紹了MySQL binlog日志清理方案,需要的朋友可以參考下
    2024-01-01
  • Mysql排序的特性詳情

    Mysql排序的特性詳情

    這篇文章主要介紹Mysql排序的特性,新寫了一個功能,自測和測試環(huán)境測試都沒問題,但在生產(chǎn)環(huán)境會出現(xiàn)偶發(fā)問題。于是,加班到12點一直排查問題,終于定位了的問題原因:Mysql Limit查詢優(yōu)化導致?,F(xiàn)抽象出問題模型及解決方案,分析給大家,避免大家踩坑,需要的朋友可以參考一下
    2021-10-10
  • mysql tmp_table_size優(yōu)化之設置多大合適

    mysql tmp_table_size優(yōu)化之設置多大合適

    這篇文章主要介紹了mysql tmp_table_size優(yōu)化問題,很多朋友都會問tmp_table_size設置多大合適,其實既然你都搜索到這篇文章了,一般大于64M比較好,當然你也可以可以根據(jù)自己的機器內(nèi)容配置增加,一般64位的系統(tǒng)能充分利用大內(nèi)存
    2016-05-05
  • mysql?8.0.28安裝配置方法圖文教程(壓縮包方式)

    mysql?8.0.28安裝配置方法圖文教程(壓縮包方式)

    這篇文章主要為大家詳細介紹了mysql?8.0.28安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 一文帶你徹底了解MySQL事務機制

    一文帶你徹底了解MySQL事務機制

    一個事情由n個單元組成,這n個單元在執(zhí)行過程中,要么同時成功,要么同時失敗,這就把n個單元放在了一個事務之中,這篇文章主要給大家詳細介紹MySQL的事務機制,感興趣的同學歡迎閱讀本文
    2023-06-06
  • Mysql系列SQL查詢語句書寫順序及執(zhí)行順序詳解

    Mysql系列SQL查詢語句書寫順序及執(zhí)行順序詳解

    這篇文章主要為大家介紹了Mysql系列SQL查詢語句的書寫順序及執(zhí)行順序示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • MySQL查詢進階操作從函數(shù)到表連接的使用

    MySQL查詢進階操作從函數(shù)到表連接的使用

    這篇文章主要介紹了MySQL查詢進階從函數(shù)到表連接的使用,包括mysql函數(shù)的使用,MySQL的分組分頁及查詢關鍵字的執(zhí)行順序,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • 淺析mysql union和union all

    淺析mysql union和union all

    union 是對數(shù)據(jù)進行并集操作,不包括重復行,同時進行默認排序而Union all 是對數(shù)據(jù)進行并集操作,包括重復行,不進行排序,下面給大家詳細介紹mysql union和union all,感興趣的朋友一起看看吧
    2017-10-10
  • Win中安裝mysql的詳細步驟

    Win中安裝mysql的詳細步驟

    這篇文章主要為大家詳細介紹了Win中安裝mysql的詳細步驟,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 教你使用VS?Code的MySQL擴展管理數(shù)據(jù)庫的方法

    教你使用VS?Code的MySQL擴展管理數(shù)據(jù)庫的方法

    這篇文章主要介紹了使用VS?Code的MySQL擴展管理數(shù)據(jù)庫,在本文告訴你如何用VS?Code的擴展程序管理MySQL數(shù)據(jù)庫,包括連接到MySQL、新建數(shù)據(jù)庫和表、修改字段定義、簡單的查詢方法以及導入導出,需要的朋友可以參考下
    2022-01-01

最新評論