search
HomeDatabaseMysql TutorialSQL TRACE和TKPROF的使用步骤

1.设置参数文件 设置三个参数timed_statistics、user_dump_dest、max_dump_file_size。 timed_staticstices 用于启动或禁止对定

1.设置参数文件 timed_staticstices

用于启动或禁止对定时统计信息(如CPU时间、占用时间),以及动态性能表中多种统计信息的收集功能

SQL>alter session set timed_statistics = true;

SQL>alter system set timed_statistics = true;

max_dump_file_size

当实例层启用SQL TRACE的时候,在每次请求服务器的时候,都将在跟踪文件中产生一个文本行,这些文件的最大尺寸受限于初始化参数的设置。默认为500(blocks)。若里面的数据被截断则增大SIZE。若为UNLIMITED则意味着没有上限。

user_dump_dest

设置跟踪文件的存储位置。默认为admin/用户/udump;

SQL>alter system set user_dump_dest = 'newdir';

2.启动SQL TRACE实用工具

对会话启动关闭SQL TRACE

SQL>alter session set sql_trace = true;

SQL>alter session set sql_trace = false;

SQL>exec SYS.DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION =(SID,SERIAL#,TRUE);

SQL>exec SYS.DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION =(SID,SERIAL#,,FALSE);

对用户实例启动关闭SQL TRACE

SQL>alter system set sql_trace = true;

SQL>alter system set sql_trace = false;

3.使用tkprof格式化trace文件     

Usage

tkprof inputfile outputfile [optional | parameters ] 

(tkprof tracefile outputfile [explain= ] [table= ] [print= ] [insert= ] [sys= ] [sort= ])

例:1.tkprof tracefile outfile [explain=user/password] [options...];

例:2.tkprof uat_ora_14936.trc trace.txt sort=(prsdsk,exedsk,fchdsk) print=10 explain=apps/apps table=apps.temp_plan_table_a insert=storea.sql sys=no

tkprof参数和选项

  • explain=user/password 执行explain命令将结果放在SQL trace的输出文件中  
  • table=schema.table 指定tkprof处理sql trace文件时临时表的模式名和表名  
  • insert=scriptfile 创建一个文件名为scriptfile的文件,包含了tkprof存放的输出sql语句  
  • sys=[yes/no] 确定系统是否列出由sys用户产生或重调的sql语句  
  • print=number 将仅生成排序后的第一条sql语句的输出结果  
  • record=recordfile 这个选项创建一个名为recorderfile的文件,包含了所有重调用的sql语句  
  • sort=sort_option 按照指定的方法对sql trace的输出文件进行降序排序  
  • sort_option 选项  
  •    prscnt  按解析次数排序  
  •    prscpu  按解析所花cpu时间排序  
  •    prsela  按解析所经历的时间排序  
  •    prsdsk  按解析时物理的读操作的次数排序  
  •    prsqry  解析时以一致模式读取数据块的次数排序  
  •    prscu   按解析时以当前读取数据块的次数进行排序  
  •    execnt  按执行次数排序  
  •    execpu  按执行时花的cpu时间排序  
  •    exeela  按执行所经历的时间排序  
  •    exedsk  按执行时物理读操作的次数排序  
  •    exeqry  按执行时以一致模式读取数据块的次数排序  
  •    execu   按执行时以当前模式读取数据块的次数排序  
  •    exerow  按执行时处理的记录的次数进行排序  
  •    exemis  按执行时库缓冲区的错误排序  
  •    fchcnt  按返回数据的次数进行排序  
  •    fchcpu  按返回数据cpu所花时间排序  
  •    fchela  按返回数据所经历的时间排序  
  •    fchdsk  按返回数据时的物理读操作的次数排序  
  •    fchqry  按返回数据时一致模式读取数据块的次数排序  
  •    fchcu   按返回数据时当前模式读取数据块的次数排序  
  •    fchrow  按返回数据时处理的数据数量排序  
  • 4.举例:

    trace其他session

    查询某用户的session的SID及SERIAL#

    $sqlplus / as sysdba

    SQL>select s.USERNAME,s.SID,s.SERIAL#,s.COMMAND from v$session s

    where s.USERNAME='ETOH' ; --注意用户名用大写

    SQL>exec dbms_system.SET_SQL_TRACE_IN_SESSION(12,73,true);

    等待被跟踪session活动一段时间

    SQL>exec dbms_system.SET_SQL_TRACE_IN_SESSION(12,73,false);

    --查询生成的.trc文件号

    SQL>SELECT d.value || '/' || lower(rtrim(i.instance, chr(0))) || '_ora_' ||

          p.spid || '.trc' trace_file_name

     from (select p.spid

             from v$session s, v$process p

            where s.sid = &sid

              and s. SERIAL# = &serial#

    and p.addr = s.paddr) p,

          (select t.instance

             from v$thread t, v$parameter v

            where v.name = 'thread'

              and (v.value = 0 or t.thread# = to_number(v.value))) i,

          (select value from v$parameter where name = 'user_dump_dest') d;

    --使用tkprof生成分析文件

    $tkprof /u01/app/Oracle/admin/center/udump/_ora_24012.trc/u01/app/oracle/admin/center/udump/center_ora_24012.txt aggregate=yes sys=no sort=fchela(此例中将执行最耗时的sql放在分析文件的开头)


    trace本session

    查询本session信息

    SQL>SELECT Sid, Serial# FROM V$session WHERE sid = Sys_Context ( 'USERENV' , 'SID' );

    SQL>alter session set sql_trace=true;

    SQL>#SQL Statements#

    SQL>alter session set sql_trace=false;

    查询生成的trace文件名

    SQL>SELECT d.value || '/' || lower(rtrim(i.instance, chr(0))) || '_ora_' ||

          p.spid || '.trc' trace_file_name

     from (select p.spid

             from v$mystat m, v$session s, v$process p

            where m.statistic# = 1

              and s.sid = m.sid

              and p.addr = s.paddr) p,

          (select t.instance

             from v$thread t, v$parameter v

            where v.name = 'thread'

              and (v.value = 0 or t.thread# = to_number(v.value))) i,

          (select value from v$parameter where name = 'user_dump_dest') d;

    tkprof格式化处理

    $tkprofcenter_ora_24012.trccenter_ora_24012.txt {一系列参数}

    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 Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

    MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

    Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

    ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

    What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

    MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

    MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

    Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

    MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

    Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

    MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

    MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

    MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

    ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

    MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

    ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

    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 Article

    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.

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software