Statistics gathering and SQL Tuning Advisor
1. https://www.pythian.com/blog/statistics-gathering-and-sql-tuning-advisor/
Our monitoring software found a long running job on one of our client’s databases. The job was an Oracle’s auto task running statistics gathering for more than 3 hours. I was curious to know why it took so long and used a query to ASH to find out the most common SQL during the job run based on the module name. Results were surprising to me: top SQL was coming with SQL Tuning Advisor comment.
Here is the SQL I used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
SQL> select s.sql_id, t.sql_text, s.cnt 2 from 3 ( select * 4 from 5 ( 6 select sql_id, count (*) cnt 7 from v$active_session_history 8 where action like 'ORA$AT_OS_OPT_SY%' 9 group by sql_id 10 order by count (*) desc 11 ) 12 where rownum <= 5 13 ) s, 14 dba_hist_sqltext t 15 where s.sql_id = t.sql_id; SQL_ID SQL_TEXT CNT ------------- -------------------------------------------------------------------------------- ---------- 020t65s3ah2pq select substrb(dump(val,16,0,32),1,120) ep, cnt from ( select /*+ no_expand_table 781 byug0cc5vn416 /* SQL Analyze(1) */ select /*+ full(t) no_parallel(t) no_parallel_index(t) 43 bkvvr4azs1n6z /* SQL Analyze(1) */ select /*+ full(t) no_parallel(t) no_parallel_index(t) 21 46sy4dfg3xbfn /* SQL Analyze(1) */ select /*+ full (t) no_parallel(t) no_parallel_index(t) 1559 |
So
most queries are coming with “SQL Analyze” comment right in the
beginning of SQL which is running from DBMS_STATS call, which is
confusing. After some bug search I have found a MOS Doc ID 1480132.1
which includes a PL/SQL stack trace from a DBMS_STATS procedure call,
and it is going up to DBMS_SQLTUNE_INTERNAL, which means DBMS_STATS has a
call to the SQL Tuning package; very odd:
1
2
3
4
5
6
7
8
|
SQL> select * from dba_dependencies where name = 'DBMS_STATS_INTERNAL' and referenced_name = 'DBMS_SQLTUNE_INTERNAL' ; OWNER NAME TYPE REFERENCED_OWNER REFERENCED_NAME ------------------------------ REFERENCED_TYPE
------------------
SYS DBMS_STATS_INTERNAL PACKAGE BODY SYS DBMS_SQLTUNE_INTERNAL PACKAGE |
Turns
out, this call has nothing to do with the SQL Tuning. It is just a call
to a procedure in this package, which happen to look like an SQL from
SQL Tuning Advisor. I have traced a GATHER_TABLE_STATS call in a test
database first with SQL trace and then with DBMS_HPROF and here is how the call tree looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
SELECT RPAD(' ', (level-1)*2, ' ') || fi.owner || '.' || fi.module AS name, fi.function, pci.subtree_elapsed_time, pci.function_elapsed_time, pci.calls FROM dbmshp_parent_child_info pci JOIN dbmshp_function_info fi ON pci.runid = fi.runid AND pci.childsymid = fi.symbolid WHERE pci.runid = 1 CONNECT BY PRIOR childsymid = parentsymid START WITH pci.parentsymid = 27; NAME ---------------------------------------- ... SYS.DBMS_STATS_INTERNAL SYS.DBMS_ADVISOR SYS.DBMS_SQLTUNE_INTERNAL SYS.DBMS_SQLTUNE_INTERNAL SYS.DBMS_LOB SYS.DBMS_LOB SYS.DBMS_LOB SYS.DBMS_SQLTUNE_INTERNAL SYS.DBMS_UTILITY COMMA_TO_TABLE 35 35 1 SYS.DBMS_SQLTUNE_UTIL0 SYS.DBMS_SQLTUNE_UTIL0 SYS.XMLTYPE SYS.DBMS_SQLTUNE_UTIL0 SYS.DBMS_UTILITY SYS.DBMS_UTILITY SYS.SQLSET_ROW SYS.XMLTYPE SYS.DBMS_STATS_INTERNAL SYS.DBMS_STATS_INTERNAL ... |
So
there is a procedure DBMS_SQLTUNE_INTERNAL.GATHER_SQL_STATS which is
being called by DBMS_STATS_INTERNAL, and this procedure actually runs a
SQL like this:
1
|
/* SQL Analyze(0) */ select /*+ to_char( count ( "ID" )),to_char(substrb(dump( min ( "ID" ),16,0,32),1,120)),to_char(substrb(dump( max ( "ID" ),16,0,32),1,120)),to_char( count ( "X" )),to_char(substrb(dump( min ( "X" ),16,0,32),1,120)),to_char(substrb(dump( max ( "X" ),16,0,32),1,120)),to_char( count ( "Y" )),to_char(substrb(dump( min ( "Y" ),16,0,32),1,120)),to_char(substrb(dump( max ( "Y" ),16,0,32),1,120)),to_char( count ( "PAD" )),to_char(substrb(dump( min ( "PAD" ),16,0,32),1,120)),to_char(substrb(dump( max ( "PAD" ),16,0,32),1,120)) from "TIM" . "T1" t /* NDV,NIL,NIL,NDV,NIL,NIL,NDV,NIL,NIL,NDV,NIL,NIL*/ |
Which is basically approximate NDV calculation.
So, nothing to be afraid of, it’s just the way the code is organized:
DBMS_STATS uses API of SQL Tuning framework when you are using
DBMS_STATS.AUTO_SAMPLE_SIZE as the ESTIMATE_PERCENT (which is the
default & recommended value in 11g+).
2.
http://blog.csdn.net/stevendbaguo/article/details/50856002
接到现场数据库维护厂商发出的警告,有一条 insert语句执行了几千秒。
开发排查,从select * from dba_source s where s.text like '%XXX%';查到是一个存储过程,于是找到存储过程,将其改名。
但问题没有解决,隔天又收到警告。
难道是存储过程改名有问题,我远程到现场看了下,并没有问题。再次查看数据库报告:
SQL Module 是 DBMS_SCHEDULER,确定无疑是Oracle JOB调用,奇怪的是SQL语句有 /* SQL Analyze(194, 1) */ 这样的提示。
如果想定位到这条SQL,最好是找到这条SQL历史执行的session,希望找到蛛丝马迹。
select * from dba_hist_active_sess_history s where s.sql_id='29yrhxqdk2d1q';
TOP_LEVEL_SQL_ID 为5zruc4v6y32f9:说明是sql_id为5zruc4v6y32f9的调用的这条sql
MODULE:DBMS_SCHEDULER
PLSQL_ENTRY_OBJECT_ID:9013
根据上面的信息再进行查询:
9013;--结果是owner=sys
object_name=dbms_sqltune
5zruc4v6y32f9
DECLARE
job BINARY_INTEGER := :job;
next_date TIMESTAMP WITH TIME ZONE := :mydate;
broken BOOLEAN := FALSE;
job_name VARCHAR2(30) := :job_name;
job_subname VARCHAR2(30) := :job_subname;
job_owner VARCHAR2(30) := :job_owner;
job_start TIMESTAMP WITH TIME ZONE := :job_start;
job_scheduled_start TIMESTAMP WITH TIME ZONE := :job_scheduled_start;
window_start TIMESTAMP WITH TIME ZONE := :window_start;
window_end TIMESTAMP WITH TIME ZONE := :window_end;
chain_id VARCHAR2(14) := :chainid;
credential_owner varchar2(30) := :credown;
credential_name varchar2(30) := :crednam;
destination_owner varchar2(30) := :destown;
destination_name varchar2(30) := :destnam;
job_dest_id varchar2(14) := :jdestid;
log_id number := :log_id;
BEGIN
DECLARE
ename VARCHAR2(30);
BEGIN
ename := dbms_sqltune.execute_tuning_task('SYS_AUTO_SQL_TUNING_TASK');
END;
:mydate := next_date;
IF broken THEN
:b := 1;
ELSE
:b := 0;
END IF;
END;
这是数据库SQL优化的包,自己调用的,终于明白 /*
SQL Analyze(194, 1) */的意思了,是标记这种优化包调用的。
3.
http://blog.itpub.net/23718752/viewspace-1252180/
在昨天晚上10点开始,数据库的性能开始下降,出现了一些j00开头的进程。
而且持续了比较长的时间,简单分析了一下,对应的进程执行的sql语句如下。
####### Process Information from OS level as below ########
oraccbs1 16184 1 23 22:00 ? 00:03:09 ora_j000_PETCUS1
oraccbs1 20829 12754 0 22:14 pts/12 00:00:00 ksh showpid.sh 16184
##############################################
SID SERIAL# USERNAME OSUSER
MACHINE PROCESS TERMINAL TYPE
LOGIN_TIME
---------- ---------- --------------- ---------------
-------------------- --------------- --------------- ----------
-------------------
1139 1071 SYS oraccbs1
xxxxxx 16184 UNKNOWN USER
2014-08-14 22:00:43
.
SQL_ID SQL_TEXT
------------------------------ ------------------------------------------------------------
6fwshkjydz9ph /* SQL Analyze(1) */ select /*+ full(t) no_parallel(t) n
o_parallel_index(t) dbms_stats cursor_sharing_exact use_weak
_name_resl dynamic_sampling(0) no_monitoring no_substrb_pad
*/to_char(count("PERIOD_KEY")),to_char(substrb(dump(min("PE
RIOD_KEY"),16,0,32),1,120)),to_char(substrb(dump(max("PERIOD
_KEY"),16,0,32),1,120)),to_char(count("PARTITION_ID")),to_ch
ar(substrb(dump(min("PARTITION_ID"),16,0,32),1,120)),to_char
(substrb(dump(max("PARTITION_ID"),16,0,32),1,120)),to_char(c
ount("DEBIT_ID")),to_char(substrb(dump(min("DEBIT_ID"),16,0,
32),1,120)),to_char(substrb(dump(max("DEBIT_ID"),16,0,32),1,
120)),to_char(count("SYS_CREATION_DATE")),to_char(substrb(du
mp(min("SYS_CREATION_DATE"),16,0,32),1,120)),to_char(substrb
(dump(max("SYS_CREATION_DATE"),16,0,32),1,120)),to_char(coun
t("SYS_UPDATE_DATE")),to_char(substrb(dump(min("SYS_UPDATE_D
ATE"),16,0,32),1,120)),to_char(substrb(dump(max("SYS_UPDATE_
DATE"),16,0,32),1,120)),to_char(count("OPERATOR_ID")),to_cha
r(substrb(dump(min("OPERATOR_ID"),16,0,3
PREV_SQL_ID SQL_TEXT
------------------------------ ------------------------------------------------------------
fhzj09a7fnrnb SELECT DBTIMEZONE, LENGTH(DBTIMEZONE) FROM SYS.DUAL
看起来是在做一些后台的维护工作。在11g中引入了几个schedule job,简单查询,结果如下。可以看到在每天都设置了对应的window。
在10点开始做一些工作。以下是禁用以后的状态。
SQL> execute DBMS_AUTO_TASK_ADMIN.DISABLE;
PL/SQL procedure successfully completed.
SQL> select * from DBA_AUTOTASK_WINDOW_CLIENTS;
WINDOW_NAME
WINDOW_NEXT_TIME
WINDO AUTOTASK OPTIMIZE SEGMENT_ SQL_TUNE HEALTH_M
------------------------------
---------------------------------------------------------------------------
----- -------- -------- -------- -------- --------
MONDAY_WINDOW 18-AUG-14 10.00.00.000000 PM
ASIA/PHNOM_PENH FALSE DISABLED ENABLED
ENABLED ENABLED DISABLED
TUESDAY_WINDOW 19-AUG-14 10.00.00.000000 PM
ASIA/PHNOM_PENH FALSE DISABLED ENABLED
ENABLED ENABLED DISABLED
WEDNESDAY_WINDOW 20-AUG-14 10.00.00.000000 PM
ASIA/PHNOM_PENH FALSE DISABLED ENABLED
ENABLED ENABLED DISABLED
THURSDAY_WINDOW 14-AUG-14 10.00.00.000000 PM
ASIA/PHNOM_PENH TRUE DISABLED ENABLED
ENABLED ENABLED DISABLED
FRIDAY_WINDOW 15-AUG-14 10.00.00.000000 PM
ASIA/PHNOM_PENH FALSE DISABLED ENABLED
ENABLED ENABLED DISABLED
SATURDAY_WINDOW 16-AUG-14 06.00.00.000000 AM
ASIA/PHNOM_PENH FALSE DISABLED ENABLED
ENABLED ENABLED DISABLED
SUNDAY_WINDOW 17-AUG-14 06.00.00.000000 AM
ASIA/PHNOM_PENH FALSE DISABLED ENABLED
ENABLED ENABLED DISABLED
7 rows selected.
其实3个主要的后台job可以选择关掉。
SQL> select client_name,status from DBA_AUTOTASK_CLIENT;
CLIENT_NAME STATUS
---------------------------------------------------------------- --------
auto optimizer stats collection ENABLED
auto space advisor ENABLED
sql tuning advisor ENABLED
使用如下的pl/sql来禁用。
begin
DBMS_AUTO_TASK_ADMIN.DISABLE(client_name => 'auto optimizer stats collection',
operation => NULL,
window_name => NULL);
end;
/
begin
DBMS_AUTO_TASK_ADMIN.DISABLE(client_name => 'auto space advisor',
operation => NULL,
window_name => NULL);
end;
/
begin
DBMS_AUTO_TASK_ADMIN.DISABLE(client_name => 'sql tuning advisor',
operation => NULL,
window_name => NULL);
end;
/
禁用以后,状态如下:
SQL> select client_name,status from DBA_AUTOTASK_CLIENT;
CLIENT_NAME STATUS
---------------------------------------------------------------- --------
auto optimizer stats collection DISABLED
auto space advisor DISABLED
sql tuning advisor DISABLED
但是不会当时生效,需要过一段时间,所以需要提前禁用。
Statistics gathering and SQL Tuning Advisor的更多相关文章
- 【转】使用SQL Tuning Advisor STA优化SQL
SQL优化器(SQL Tuning Advisor STA)是Oracle10g中推出的帮助DBA优化工具,它的特点是简单.智能,DBA值需要调用函数就可以给出一个性能很差的语句的优化结果.下面介绍一 ...
- Oracle 10G强大的SQL优化工具:SQL Tuning Advisor
p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120%; orphans: 2; widow ...
- 深入了解SQL Tuning Advisor(转载)
1.前言:一直以来SQL调优都是DBA比较费力的技术活,而且很多DBA如果没有从事过开发的工作,那么调优更是一项头疼的工作,即使是SQL调优很厉害的高手,在SQL调优的过程中也要不停的分析执行计划.加 ...
- Oracle SQL Tuning Advisor 测试
如果面对一个需要优化的SQL语句,没有很好的想法,可以先试试Oracle的SQL Tuning Advisor. SQL> select * from v$version; BANNER --- ...
- 使用SQL Profile及SQL Tuning Advisor固定运行计划
SQL Profile就是为某一SQL语句提供除了系统统计信息.对象(表和索引等)统计信息之外的其它信息,比方执行环境.额外的更准确的统计信息,以帮助优化器为SQL语句选择更适合的执行计划. SQL ...
- SQL Tuning 基础概述08 - SQL Tuning Advisor
SQL调优顾问 SQL Tuning Advisor的使用案例: 1.构建测试表T 2.定义调整任务 3.修改调整任务参数 4.执行调整任务 5.监控调整任务 6.查看调整任务建议 7.删除调整任务 ...
- 如何用 SQL Tuning Advisor (STA) 优化SQL语句
在Oracle10g之前,优化SQL是个比较费力的技术活,不停的分析执行计划,加hint,分析统计信息等等.在10g中,Oracle推出了自己的SQL优化辅助工具: SQL优化器(SQL Tuning ...
- sql tuning advisor使用
DB tuning advisor是创建优化任务,对某些sql数据库进行分析,并尽量给出优化建议的一个强大的数据库工具. 自己平时几乎没用过这玩意,所以来测一测用法,其实对于一些sql一筹莫展的时候跑 ...
- 使用ORACLE SQL Tuning advisor快速优化低效的SQL语句
ORACLE10G以后版本的SQL Tuning advisor可以从以下四个方面给出优化方案 (1)为统计信息丢失或失效的对象收集统计信息 (2)考虑优化器的任何数据偏差.复杂谓词或失效的统计信 ...
随机推荐
- Swift简单介绍 教程
Swift是什么? Swift是苹果于WWDC 2014公布的编程语言.这里引用The Swift Programming Language的原话: Swift is a new programmi ...
- cf246 ENew Reform (并查集找环)
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each ...
- iOS 开发小常识 开发笔记
一 自定义push方法 /* 参数说明 * controllerName : push的目标页 例:@“testcontroll” ---注意不带.h * isNibPage ...
- 自己定义验证器——用Struts2框架以框架师的思维灵活做好该事情
面对的问题:自己定义一个18位身份验证器.编写验证器.在validators.xml文件里进行注冊.在验证配置文件里使用? 第一部分:理解Struts2中自带的验证器 第二部分:如何通过server( ...
- linux系统编程:线程同步-相互排斥量(mutex)
线程同步-相互排斥量(mutex) 线程同步 多个线程同一时候訪问共享数据时可能会冲突,于是须要实现线程同步. 一个线程冲突的演示样例 #include <stdio.h> #includ ...
- 关于axis2.1.6与websphere7的包冲突问题的解决方式
1,复制axis2.1.6内的module目录内的全部文件到lib 并改动扩展名为.jar 2,删除module目录(可选,不删除也能够) 3,部署到was 4,设置was相应应用程序的类载入方案为父 ...
- 哨兵和docker容器
1,redis哨兵的配置 redis-6379配置文件内容如下 cat redis-6379.conf port 6379 daemonize yes logfile "6379.log&q ...
- Infrastructure for container projects.
Linux Containers https://linuxcontainers.org/
- 包管理 import debug 模块管理 module
import sys, os this_file_abspath = os.path.dirname(os.path.abspath(__file__)) ProjectUtil_path = '{} ...
- jsp useBean
<jsp:uesBean id="test" scope="page" class="test.useBeanTest"> 用于 ...