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

您的位置:首頁技術文章
文章詳情頁

實用心得:Oracle中監(jiān)控索引的使用

瀏覽:9日期:2023-11-12 12:40:04
研究發(fā)現,Oracle數據庫使用的索引不會超過總數的25%,或者不易他們期望被使用的方式使用。通過監(jiān)控數據庫索引的使用,釋放那些未被使用的索引,從而節(jié)省維護索引的開銷,優(yōu)化性能。 1、在oracle8i中,確定使用了那個索引的方法意味著要對存在語共享SQL區(qū)中的所有語句運行EXPLIAN PALN,然后查詢計劃表中的OPERATION列,從而識別有OBJECT_OWNER和OBJECT_NAME列所確定的那個索引上的索引訪問。 下面是一個監(jiān)控索引使用的腳本,這個腳本僅僅是一個樣品,在某種條件下成立: 條件: 運行這個腳本的用戶擁有權限解釋所有的v$sqlarea中的sql,除了不是被SYS裝載的。 plan_table.remarks能夠別用來決定與特權習慣的錯誤。 對所有的共享池中SQL,參數OPTIMIZER_GOAL是一個常量,無視v$sqlarea.optimizer_mode。 兩次快照之間,統(tǒng)計資料被再次分析過。 沒有語句別截斷。 所有的對象都是局部的。 所有被引用的表或視圖或者是被運行腳本的用戶所擁有,或者完全有資格的名字或同義詞被使用。 自從上次快照以來,沒有不受'歡迎'的語句被沖洗出共享池(例如,在裝載)。 對于所有的語句, v$sqlarea.version_count = 1 (children)。 腳本: CODE: set echo off Rem Drop and recreate PLAN_TABLE for EXPLAIN PLAN drop table plan_table; create table PLAN_TABLE ( statement_id varchar2(30), timestamp date, remarks varchar2(80), operation varchar2(30), options varchar2(255), object_nodevarchar2(128), object_owner varchar2(30), object_namevarchar2(30), object_instancenumeric, object_type varchar2(30), optimizer varchar2(255), search_columns number, id numeric, parent_id numeric, position numeric, cost numeric, cardinality numeric, bytes numeric, other_tag varchar2(255), partition_start varchar2(255), partition_stop varchar2(255), partition_idnumeric, other long, distributionvarchar2(30), cpu_cost numeric, io_cost numeric, temp_space numeric, Access_predicates varchar2(4000), filter_predicates varchar2(4000));Rem Drop and recreate SQLTEMP for taking a snapshot of the SQLAREA drop table sqltemp; create table sqltemp ( ADDR VARCHAR2 (16),SQL_TEXT VARCHAR2 (2000),DISK_READS NUMBER,EXECUTIONS NUMBER,PARSE_CALLS NUMBER); set echo on Rem Create procedure to populate the plan_table by executing Rem explain plan...for 'sqltext' dynamically create or replace procedure do_explain ( addr IN varchar2, sqltext IN varchar2) as dummy varchar2 (1100); mycursor integer; ret integer; my_sqlerrm varchar2 (85); begin dummy:='EXPLAIN PLAN SET STATEMENT_ID=' ; dummy:=dummy''''addr''''' FOR 'sqltext; mycursor := dbms_sql.open_cursor; dbms_sql.parse(mycursor,dummy,dbms_sql.v7); ret := dbms_sql.execute(mycursor); dbms_sql.close_cursor(mycursor); commit; exception -- Insert errors into PLAN_TABLE... when others then my_sqlerrm := substr(sqlerrm,1,80); insert into plan_table(statement_id,remarks) values (addr,my_sqlerrm); -- close cursor if exception raised on EXPLAIN PLAN dbms_sql.close_cursor(mycursor); end; / Rem Start EXPLAINing all S/I/U/D statements in the shared pool declare -- exclude statements with v$sqlarea.parsing_schema_id = 0 (SYS) cursor c1 is select address, sql_text, DISK_READS, EXECUTIONS, PARSE_CALLS from v$sqlarea where command_type in (2,3,6,7) and parsing_schema_id != 0; cursor c2 is select addr, sql_text from sqltemp; addr2 varchar(16); sqltext v$sqlarea.sql_text%type; dreads v$sqlarea.disk_reads%type; execs v$sqlarea.executions%type; pcalls v$sqlarea.parse_calls%type; begin open c1; fetch c1 into addr2,sqltext,dreads,execs,pcalls; while (c1%found) loop insert into sqltemp values(addr2,sqltext,dreads,execs,pcalls); commit; fetch c1 into addr2,sqltext,dreads,execs,pcalls; end loop; close c1; open c2; fetch c2 into addr2, sqltext; while (c2%found) loop do_explain(addr2,sqltext); fetch c2 into addr2, sqltext; end loop; close c2; end; / Rem Generate a report of index usage based on the number of times Rem a SQL statement using that index was executed select p.owner, p.name, sum(s.executions) totexec from sqltemp s, (select distinct statement_id stid, object_owner owner, object_name name from plan_table where operation = 'INDEX') p where s.addr = p.stid group by p.owner, p.name order by 2 desc; Rem Perform cleanup on exit (optional) deletefrom plan_table wherestatement_id in (selectaddrfromsqltemp); drop table sqltemp;關于這個腳本,有幾個重要的地方需要注重,即它可能一起明顯的開銷,因此,應該在仔細地進行 權衡后才把它應用到繁忙的生產應用系統(tǒng)中區(qū)。2、 oracle9i中如何確定索引的使用情況 在oracle9i中,情況會簡單得多,因為有一個新得字典視圖V$SQL_PLAN存儲了實際計劃,這些計劃用于執(zhí)行共享SQL區(qū)中得語句。V$SQL_PLAN視圖很類似與計劃表,但V$SQL_PLAN使用ADDRESS和HASH_VALUE列 來識別語句, 而計劃表使用用戶提供得STATEMENT_ID來識別語句。下面的SQL顯示了在一個oracle9i數據庫中,由出現在共享SQL區(qū)中語句使用的所有索引。 select object_owner, object_name, options, count(*) from v$sql_plan where operation='INDEX' and object_owner!='SYS' group by object_owner, object_name, operation, options order by count(*) desc; 所有基于共享SQL區(qū)中的信心來識別索引使用情況的方法, 都可能會收集到不完整的信息。共享SQL區(qū)是一 個動態(tài)結構,除非能對它進行足夠頻繁的采樣, 否則在有關索引使用的情況的信息被收集之前,SQL語句可 能就已經(因為老化)被移出緩存了。oracle9i提供了解決這個問題的方案,即它為alter index提供了一個monitoring usage子句。當啟用monitoring usage 時,oralce記錄簡單的yes或no值,以指出在監(jiān)控間隔 期間某個索引是否被使用。 為了演示這個新特性,你可以使用下面的例子: (a) Create and populate a small test table (b) Create Primary Key index on that table (c) Query v$object_usage: the monitoring has not started yet (d) Start monitoring of the index usage (e) Query v$object_usage to see the monitoring in progress (f) Issue the SELECT statement which uses the index (g) Query v$object_usage again to see that the index has been used (h) Stop monitoring of the index usage (i) Query v$object_usage to see that the monitoring sDetailed steps: (a) Create and populate a small test table create table prodUCts ( prod_id number(3), prod_name_code varchar2(5)); insert into products values(1,'aaaaa'); insert into products values(2,'bbbbb'); insert into products values(3,'ccccc'); insert into products values(4,'ddddd'); commit; (b) Create Primary Key index on that table alter table products add (constraint products_pk primary key (prod_id)); (c) Query v$object_usage: the monitoring has not started yet column index_name format a12 column monitoring format a10 column used format a4 column start_monitoring format a19 column end_monitoring format a19 select index_name,monitoring,used,start_monitoring,end_monitoring from v$object_usage; no rows selected (d) Start monitoring of the index usage alter index products_pk monitoring usage; Index altered. (e) Query v$object_usage to see the monitoring in progress select index_name,monitoring,used,start_monitoring,end_monitoring from v$object_usage; INDEX_NAME MONITORING USED START_MONITORING END_MONITORING --------------------------------------------------------------- PRODUCTS_PK YES NO 04/25/2001 15:43:13 Note: Column MONITORING='YES', START_MONITORING gives the timestamp. (f) Issue the SELECT statement which uses the index First, make sure that index will be used for this statement. Create plan_table in your schema, as required by Oracle Autotrace utility: @$ORACLE_HOME/rdbms/admin/utlxplan Table created. Use Oracle Autotrace utility to oBTain the execution plan: set autotrace on explain select * from products where prod_id = 2; Execution Plan ------------------------------------------------------ 0 SELECT STATEMENT Optimizer=CHOOSE 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'PRODUCTS' 2 1 INDEX (UNIQUE SCAN) OF 'PRODUCTS_PK' (UNIQUE) set autotrace off Now, since you know the index will be used for this query, issue the actual SELECT statement: select * from products where prod_id = 2; PROD_ID PROD_ ---------- ----- 2 bbbbb (g) Query v$object_usage again to see that the index has been used select index_name,monitoring,used,start_monitoring,end_monitoring from v$object_usage; INDEX_NAME MONITORING USED START_MONITORING END_MONITORING ------------ ---------- ---- ------------------- ---- ------------ PRODUCTS_PK YES YES 04/25/2001 15:43:13 Note: Column USED='YES'. (h) Stop monitoring of the index usage alter index products_pk nomonitoring usage; Index altered. (i) Query v$object_usage to see that the monitoring stopped select index_name,monitoring,used,start_monitoring,end_monitoring from v$object_usage; INDEX_NAME MONITORING USED START_MONITORING END_MONITORING ------------ ---------- ---- ------------------- ------------------- PRODUCTS_PK NO YES 04/25/2001 15:43:13 04/25/2001 15:48:44 Note: Column MONITORING='NO', END_MONITORING gives the timestamp. 下面的PL/SQL塊對數據庫中的所有索引(SYS和SYSTEM擁有的索引除外)啟用監(jiān)控: declare l_sql varchar2(128); begin for rec in (select 'alter index 'owner.'.'index_name' monitoring usage' mon from dba_indexes where owner not in ('SYS', 'SYSTEM') and index_type='NORMAL') loop l_sql:=rec.mon; execute immediate l_sql; end loop; end; 下面我們來看一下Oracle 9i 這個新特性能不能識別在進行DML操作時外鍵列上索引的使用情況: 以9i中HR模式為例: 標準的dept和emp表是一個與外鍵關聯(lián)的父子表的例子。這個例子主要想看一下,在父表上刪除一個記錄,會不會調用子表上外鍵上的索引。 首先監(jiān)控HR模式下所有索引的使用,為了便于在主表上刪除一條記錄,不違反引用完整性約束。我們首先丟棄原有的約束,重新創(chuàng)建支持級聯(lián)刪除的約束. alter table employees add constraint emp_dept_fk foreign key (department_id) references departments on delete cascade;alter table job_history drop constraint jhist_emp_fk;alter table job_history add constraint jhist_emp_fk foreign key(employee_id) references employees on delete cascade;delete from departments where department_id=10;注重在此為了方便,我們刪除部門id為10的記錄。假如你刪除其他的部門,可能你還要更改表job_history中相關的約束。 現在我們看看索引使用的情況: select index_name, table_name, monitoring, used from v$object_usage where used='YES' INDEX_NAME TABLE_NAME MON USE ------------------------------ -------------------- --- --- DEPT_ID_PK DEPARTMENTSYES YES EMP_EMP_ID_PKEMPLOYEES YES YES EMP_DEPT_FKEMPLOYEES YES YES很明顯刪除父表上的記錄,也利用了子表中相關的索引。 v$object_usage 視圖的一個異常之處是, 它只能顯示屬于連接用戶的索引的信息。Oracle可能在將來會解決這個問題。假如您的數據庫只顯示連接用戶的對象使用信息,下面的視圖(必須被創(chuàng)建為SYS)可用于提供來自任何帳戶的所有被監(jiān)控的索引的信息: create or replace view V$ALL_OBJECT_USAGE(INDEX_NAME, TABLE_NAME, MONITORING, USED, START_MONITORING, END_MONITORING) as select io.name, t.name, decode(bitand(i.flags, 65536),0,'NO','YES'), decode(bitand(ou.flags,1),0,'NO','YES'), ou.start_monitoring, ou.end_monitoring from sys.obj$ io, sys.obj$ t, sys.ind$ i, sys.object_usage ou where i.obj#=ou.obj# and io.obj#=ou.obj# and t.obj#=i.bo#;grant select on v$all_object_usage to public;create public synonym v$all_object_usage for v$all_object_usage;3、最后我們簡單的說一下,如何監(jiān)控最近被使用的索引 下列查詢將列出最近被訪問的索引: column owner format a20 trunc column segment_name format a30 trunc select distinct b.owner, b.segment_namefrom x$bh a, dba_extents bwhere b.file_id=a.dbafil anda.dbablk between b.block_id and b.block_id+blocks-1 andsegment_type='INDEX' andb.owner not in ('SYS','SYSTEM');這個過程可能要耗費一定時間,建議在數據庫不太繁忙的時候運行。
標簽: Oracle 數據庫
主站蜘蛛池模板: 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 熔体泵|换网器|熔体齿轮泵|熔体计量泵厂家-郑州巴特熔体泵有限公司 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 | 智能气瓶柜(大型气瓶储存柜)百科 | 氧化铁红厂家-淄博宗昂化工 | 高扬程排污泵_隔膜泵_磁力泵_节能自吸离心水泵厂家-【上海博洋】 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 济南菜鸟驿站广告|青岛快递车车体|社区媒体-抖音|墙体广告-山东揽胜广告传媒有限公司 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 全自动翻转振荡器-浸出式水平振荡器厂家-土壤干燥箱价格-常州普天仪器 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 数字展示在线_数字展示行业门户网站| 全国国际学校排名_国际学校招生入学及学费-学校大全网 | 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 开业庆典_舞龙舞狮_乔迁奠基仪式_开工仪式-神挚龙狮鼓乐文化传媒 | 不锈钢电动球阀_气动高压闸阀_旋塞疏水调节阀_全立阀门-来自温州工业阀门巨头企业 | ph计,实验室ph计,台式ph计,实验室酸度计,台式酸度计 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | 国际学校_国际学校哪个好_国际课程学校-国际学校择校网 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 成都离婚律师|成都结婚律师|成都离婚财产分割律师|成都律师-成都离婚律师网 | SF6环境监测系统-接地环流在线监测装置-瑟恩实业 | 消泡剂-水处理消泡剂-涂料消泡剂-切削液消泡剂价格-东莞德丰消泡剂厂家 | 学叉车培训|叉车证报名|叉车查询|叉车证怎么考-工程机械培训网 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 微动开关厂家-东莞市德沃电子科技有限公司| nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 高铝矾土熟料_细粉_骨料_消失模_铸造用铝矾土_铝酸钙粉—嵩峰厂家 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 石家庄装修设计_室内家装设计_别墅装饰装修公司-石家庄金舍装饰官网 | 越南专线物流_东莞国际物流_东南亚专线物流_行通物流 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 |