search
HomeDatabaseMysql TutorialOracle 11g新SQL Trace 10046方法

10046是每一个研究Oracle、进行SQL调优的朋友非常熟悉的工具。10046和10053两个诊断事件,可以方便的帮助我们了解Oracle CBO优化

10046是每一个研究Oracle、进行SQL调优的朋友非常熟悉的工具。10046和10053两个诊断事件,可以方便的帮助我们了解Oracle CBO优化器行为和SQL执行行为。在商业非开源的Oracle情况下,我们很多的Internal知识都是源于这两个利器。

进入11g之后,Oracle提供了10046的替代Trace方法,原有event方法依然支持。本篇就着重介绍一下新的SQL Trace手段。

相关阅读:Oracle SQL Trace 和 10046 事件跟踪

1、环境和背景介绍

我们依然选择Oracle 11gR2作为实验对象,同时创建实验数据表T

SQL> select * from v$version;

 

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

PL/SQL Release 11.2.0.1.0 - Production

CORE      11.2.0.1.0        Production

 

TNS for Linux: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0 – Production

 

 

创建数据表,并且清理shared pool和buffer cache信息。

 

 

SQL> create table t as select * from dba_objects;

Table created

 

SQL> create index idx_t_id on t(object_id);

Index created

 

SQL> exec dbms_stats.gather_table_stats(user,'T',cascade => true);

PL/SQL procedure successfully completed

 

SQL> alter system flush shared_pool;

System altered

 

SQL> alter system flush buffer_cache;

System altered

 

 

2、SQL_TRACE方法

 

首先我们查看新接口方法的默认手段。在之前的Oracle版本中,我们有大致上下面几种手段。

 

ü Alter session set events;

ü Dbms_跟踪包;

ü Oradebug设置跟踪事件;

ü 初始化参数sql_trace;

 

应该说,这几种方法对于Oracle的跟踪非常彻底。在事件10046作用的范围内,所有的SQL,除了目标SQL还有recursive SQL,都会被记录下来到跟踪文件。所以,,我们明明发出了一条SQL语句,但是跟踪文件里面包括了很多对数据字典的检索。由此,我们经常需要使用tkprof进行raw文件处理。

 

我们先看下新接口方法使用。先定位到Trace文件位置。

 

 

SQL> select value from v$diag_info where;

VALUE

-------------------------------------------------------------------------

/u01/diag/rdbms/wilson/wilson/trace/wilson_ora_3663.trc

 

 

开启跟踪。

 

--标记

SQL> alter session set tracefile_identifier='10046';

会话已更改。

 

SQL> alter session set timed_statistics = true;

会话已更改。

 

SQL> alter session set statistics_level=all;

会话已更改。

 

SQL> alter session set max_dump_file_size = unlimited;

会话已更改。

 

--跟踪接口

SQL> alter session set events 'sql_trace level 12';

 

会话已更改。

 

SQL> select /*+demo*/count(*) from t where object_id=1000;

 

 COUNT(*)

----------

        1

 

SQL> alter session set events 'sql_trace off';

会话已更改。

10046有若干的跟踪level,其中level 12包括了所有信息,一般我们作为初学者,把尽可能多的信息获取到比较方便。在sql_trace跟踪接口中,我们可以设置level取值。

目标SQL在其中执行。在我们看SQL Trace文件之前,我们先从shared pool中找到这个缓存SQL的sql_id。这个id做什么用,我们先留一个话头。

SQL> select sql_id, executions from v$sqlarea where sql_text like 'select /*+demo*/count(*)%';

SQL_ID      EXECUTIONS

------------- ----------

94wk1cqs4g2f5        1

我们可以在目录中找到Trace File了。

[root@bspdev ~]# su - oracle

[oracle@bspdev ~]$ cd /u01/diag/rdbms/wilson/wilson/trace/

[oracle@bspdev trace]$ ls -l | grep 3663

-rw-r----- 1 oracle oinstall  16783 Aug 22 05:55 wilson_ora_3663_10046.trc

-rw-r----- 1 oracle oinstall    158 Aug 22 05:55 wilson_ora_3663_10046.trm

打开Trace文件,可以发现与目标SQL相关的Recursive SQL都在其中。

*** 2013-08-22 05:54:47.257

WAIT #1: nam='SQL*Net message from client' ela= 66502048 driver id=1413697536 #bytes=1 p3=0 obj#=-1 tim=1377122087257296

CLOSE #1:c=0,e=10,dep=0,type=1,tim=1377122087257461

=====================

PARSING IN CURSOR #2 len=202 dep=1 uid=0 ct=3 lid=0 tim=1377122087259383 hv=3819099649 ad='525e44f4' sqlid='3nkd3g3ju5ph1'

select obj#,type#,ctime,mtime,stime, status, dataobj#, flags, oid$, spare1, spare2 from obj$ where owner#=:1 and name=:2 and namespace=:3 and remoteowner is null and linkname is null and subname is null

END OF STMT

(省略……)

CLOSE #2:c=0,e=14391,dep=1,type=3,tim=1377122087295194

=====================

PARSING IN CURSOR #1 len=52 dep=0 uid=0 ct=3 lid=0 tim=1377122087365631 hv=2957478341 ad='525a33fc' sqlid='94wk1cqs4g2f5'

select /*+demo*/count(*) from t where object_id=1000

END OF STMT

(省略……)

CLOSE #1:c=0,e=36,dep=0,type=0,tim=1377122102531891

至此,SQL_TRACE新接口和原来的10046方法就相同了。SQL_TRACE的新功能体现在有针对性SQL语句的跟踪上。

更多详情请继续阅读第2页的精彩内容:

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How can you monitor MySQL server health and performance?How can you monitor MySQL server health and performance?Apr 26, 2025 am 12:15 AM

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

Compare and contrast MySQL and MariaDB.Compare and contrast MySQL and MariaDB.Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function