欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

oracle數(shù)據(jù)庫(kù)支持物化視圖--不是基于基表的虛表,而是根據(jù)表實(shí)際存在的實(shí)表,即物化視圖的數(shù)據(jù)存儲(chǔ)在非易失的存儲(chǔ)設(shè)備上。
下面實(shí)驗(yàn)創(chuàng)建ON COMMIT 的FAST刷新模式,在mysql中用觸發(fā)器實(shí)現(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ā)器有一個(gè)bug,就是在一種產(chǎn)品的最后一個(gè)訂單被刪除的時(shí)候,Order_mv表的更新不能實(shí)現(xiàn),不知道這算不算是mysql的一個(gè)bug。當(dāng)然,如果這個(gè)也可以直接用sql語(yǔ)句生成數(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;

相關(guān)文章

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

    MySQL binlog日志清理的方案分享

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

    Mysql排序的特性詳情

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

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

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

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

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

    一文帶你徹底了解MySQL事務(wù)機(jī)制

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

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

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

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

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

    淺析mysql union和union all

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

    Win中安裝mysql的詳細(xì)步驟

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

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

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

最新評(píng)論