电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

ORACLE UPDATE 語句語法與性能分析的一點(diǎn)看法

瀏覽:128日期:2023-11-19 15:11:40
 為了方便起見,建立了以下簡單模型,和構(gòu)造了部分測試數(shù)據(jù): 在某個業(yè)務(wù)受理子系統(tǒng)BSS中, --客戶資料表 create table customers ( customer_idnumber(8);not null,; -- 客戶標(biāo)示 city_name;;varchar2(10) not null,; -- 所在城市 customer_type char(2);;;not null,; -- 客戶類型 ... ) create unique index PK_customers on customers (customer_id) 由于某些原因,客戶所在城市這個信息并不什么準(zhǔn)確,但是在 客戶服務(wù)部的CRM子系統(tǒng)中,通過主動服務(wù)獲取了部分客戶20%的所在 城市等準(zhǔn)確信息,于是你將該部分信息提取至一張臨時表中: create table tmp_cust_city ( customer_id;number(8) not null, citye_name;;varchar2(10) not null, customer_type; char(2)not null ) 1) 最簡單的形式 --經(jīng)確認(rèn)customers表中所有customer_id小于1000均為'北京' --1000以內(nèi)的均是公司走向全國之前的本城市的老客戶:) update customers set;city_name='北京' where; customer_id<10002) 兩表(多表)關(guān)聯(lián)update -- 僅在where字句中的連接 --這次提取的數(shù)據(jù)都是VIP,且包括新增的,所以順便更新客戶類別 update customers; a;;;;-- 使用別名 set;customer_type='01' --01 為vip,00為普通 where; exists (select 1 fromtmp_cust_city b where; b.customer_id=a.customer_id )3) 兩表(多表)關(guān)聯(lián)update -- 被修改值由另一個表運(yùn)算而來 update customers a-- 使用別名 set;city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id) where; exists (select 1 fromtmp_cust_city b where; b.customer_id=a.customer_id ) -- update 超過2個值 update customers a-- 使用別名 set;(city_name,customer_type)=(select b.city_name,b.customer_type fromtmp_cust_city b where; b.customer_id=a.customer_id) where; exists (select 1 fromtmp_cust_city b where; b.customer_id=a.customer_id ) 注重在這個語句中, =(select b.city_name,b.customer_type fromtmp_cust_city b where; b.customer_id=a.customer_id ) 與 (select 1 fromtmp_cust_city b where; b.customer_id=a.customer_id ) 是兩個獨(dú)立的子查詢,查看執(zhí)行計(jì)劃可知,對b表/索引掃描了2篇; 假如舍棄where條件,則默認(rèn)對A表進(jìn)行全表 更新,但由于(select b.city_name from tmp_cust_city b where where; b.customer_id=a.customer_id) 有可能不能提供'足夠多'值,因?yàn)閠mp_cust_city只是一部分客戶的信息, 所以報(bào)錯(假如指定的列--city_name可以為NULL則另當(dāng)別論): 01407, 00000, 'cannot update (%s) to NULL'// *Cause:// *Action:一個替代的方法可以采用: update customers a-- 使用別名 set;city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name) 或者 set;city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知') -- 當(dāng)然這不符合業(yè)務(wù)邏輯了4) 上述3)在一些情況下,因?yàn)锽表的紀(jì)錄只有A表的20-30%的紀(jì)錄數(shù), 考慮A表使用INDEX的情況,使用cursor也許會比關(guān)聯(lián)update帶來更好的性能: set serveroutput ondeclare cursor city_cur is select customer_id,city_name ;fromtmp_cust_city order by customer_id;begin for my_cur in city_cur loop;;;;;update customers set;city_name=my_cur.city_name where; customer_id=my_cur.customer_id; /** 此處也可以單條/分批次提交,避免鎖表情況 **/--;;if mod(city_cur%rowcount,10000)=0 then--;;;;;dbms_output.put_line('----');--;;;;;commit;--;;end if; end loop;end;5) 關(guān)聯(lián)update的一個特例以及性能再探討 在Oracle的update語句語法中,除了可以update表之外,也可以是視圖,所以有以下1個特例: update (select a.city_name,b.city_name as new_name fromcustomers a, tmp_cust_city b where; b.customer_id=a.customer_id ) set;city_name=new_name 這樣能避免對B表或其索引的2次掃描,但前提是 A(customer_id) b(customer_id)必需是unique index 或primary key。否則報(bào)錯: 01779, 00000, 'cannot modify a column which maps to a non key-preserved table'// *Cause: An attempt was made to insert or update columns of a join view which// map to a non-key-preserved table.// *Action: Modify the underlying base tables directly.6)oracle另一個常見錯誤 回到3)情況,由于某些原因,tmp_cust_city customer_id 不是唯一index/primary key update customers a-- 使用別名 set;city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id) where; exists (select 1 fromtmp_cust_city b where; b.customer_id=a.customer_id ) 當(dāng)對于一個給定的a.customer_id (select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id) 返回多余1條的情況,則會報(bào)如下錯誤: 01427, 00000, 'single-row subquery returns more than one row'// *Cause:// *Action:一個比較簡單近似于不負(fù)責(zé)任的做法是 update customers a-- 使用別名 set;city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)如何理解 01427 錯誤,在一個很復(fù)雜的多表連接update的語句,經(jīng)常因考慮不周,出現(xiàn)這個錯誤, 仍已上述例子來描述,一個比較簡便的方法就是將A表代入 值表達(dá)式 中,使用group by 和 having 字句查看重復(fù)的紀(jì)錄 (select b.customer_id,b.city_name,count(*) from tmp_cust_city b,customers a where b.customer_id=a.customer_id group by b.customer_id,b.city_name having count(*)>=2 )
標(biāo)簽: Oracle 數(shù)據(jù)庫
主站蜘蛛池模板: 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | 土壤养分检测仪_肥料养分检测仪_土壤水分检测仪-山东莱恩德仪器 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 方源木业官网-四川木门-全国木门专业品牌 | 德国UST优斯特氢气检漏仪-德国舒赐乙烷检测仪-北京泽钏 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 水篦子|雨篦子|镀锌格栅雨水篦子|不锈钢排水篦子|地下车库水箅子—安平县云航丝网制品厂 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 搪瓷搅拌器,搪玻璃搅拌器,搪玻璃冷凝器_厂家-淄博越宏化工设备 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 柔软云母板-硬质-水位计云母片组件-首页-武汉长丰云母绝缘材料有限公司 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | EFM 022静电场测试仪-套帽式风量计-静电平板监测器-上海民仪电子有限公司 | 西安耀程造价培训机构_工程预算实训_广联达实作实操培训 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 | 电销卡_稳定企业大语音卡-归属地可选-世纪通信 | 深圳品牌设计公司-LOGO设计公司-VI设计公司-未壳创意 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 石膏基自流平砂浆厂家-高强石膏基保温隔声自流平-轻质抹灰石膏粉砂浆批发-永康市汇利建设有限公司 | 长沙网站建设制作「网站优化推广」-网页设计公司-速马科技官网 | 钢结构厂房造价_钢结构厂房预算_轻钢结构厂房_山东三维钢结构公司 | 吲哚菁绿衍生物-酶底物法大肠菌群检测试剂-北京和信同通科技发展有限公司 | sfp光模块,高速万兆光模块工厂-性价比更高的光纤模块制造商-武汉恒泰通 | 桑茶-七彩贝壳桑叶茶 长寿茶| 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 合肥办公室装修 - 合肥工装公司 - 天思装饰 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 耐酸碱泵-自吸耐酸碱泵型号「品牌厂家」立式耐酸碱泵价格-昆山国宝过滤机有限公司首页 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 高尔夫球杆_高尔夫果岭_高尔夫用品-深圳市新高品体育用品有限公司 | 厂房出租_厂房出售_产业园区招商_工业地产&nbsp;-&nbsp;中工招商网 | 400电话_400电话申请_888元包年_400电话办理服务中心_400VIP网 |