• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用10046查看执行计划并读懂trace文件

mysql 搞代码 4年前 (2022-01-09) 63次浏览 已收录 0个评论

查看 sql 执行计划的方法有许多种, 10046 事件就是其中的一种. 与其他查看 sql 执行计划不同, 当我们遇到比较复杂的 sql 语句, 我们可以通过 10046 跟踪 sql 得到执行计划中每一个步骤的逻辑读, 物理读以及花费的时间等. 这种细粒度的跟踪对于我们分析 sql 性

查看 sql 执行计划的方法有许多种, 10046 事件就是其中的一种. 与其他查看 sql 执行计划不同, 当我们遇到比较复杂的 sql 语句, 我们可以通过 10046 跟踪 sql 得到执行计划中每一个步骤的逻辑读, 物理读以及花费的时间等. 这种细粒度的跟踪对于我们分析 sql 性能尤其有用.

一般来说, 使用 10046 事件得到 sql 执行计划的步骤如下:
1. 激活当前 session 10046 事件
2. 在当前 session 中执行 sql 语句
3. 关闭当前 session 10046 事件

执行完上述步骤后, 通常会自动生成一个 trace 文件. 在 oracle 11g 中, trace 文件一般放在$ORACLE_BASE/diag/rdbms/{database_name}/$ORACLE_SID/trace 目录下. 如果使用 oradebug 激活跟踪 10046后, 可以使用 oradebug tracefile_name 得到刚刚生成的 trace 文件的完整路径.

NAME                                 TYPE        VALUE------------------------------------ ----------- ------------------------------background_dump_dest                 string      g:\app\davidd\diag\rdbms\david                                                 \david\trace

刚刚提到的 oradebug 激活跟踪 10046 事件, 我想大部分 dba 都会使用. oradebug 是个功能强大非常好用的工具, 使用 oradebug help 将会看到它的功能很多

SQL> oradebug helpHELP           [command]                 		Describe one or all commandsSETMYPID                                 		Debug current processSETOSPID                          		Set OS pid of process to debugSETORAPID       ['force']        		Set Oracle pid of process to debugSETORAPNAME                    		Set Oracle process name to debugSHORT_STACK                              		Get abridged OS stackCURRENT_SQL                              		Get current SQLDUMP             [addr]  		Invoke named dumpDUMPSGA        [bytes]                   		Dump fixed SGADUMPLIST                                 		Print a list of available dumpsEVENT                              		Set trace event in processSESSION_EVENT                      		Set trace event in sessionDUMPVAR        <p>  [level]  		Print/dump a fixed PGA/SGA/UGA variableDUMPTYPE           		Print/dump an address with type infoSETVAR         <p>    		Modify a fixed PGA/SGA/UGA variablePEEK             [level]      		Print/Dump memoryPOKE                   		Modify memoryWAKEUP                           		Wake up Oracle processSUSPEND                                  		Suspend executionRESUME                                   		Resume executionFLUSH                                    		Flush pending writes to trace fileCLOSE_TRACE                              		Close trace fileTRACEFILE_NAME                           		Get name of trace fileLKDEBUG                                  		Invoke global enqueue service debuggerNSDBX                                    		Invoke CGS name-service debugger-G                		Parallel oradebug command prefix-R                		Parallel oradebug prefix (return outputSETINST              		Set instance list in double quotesSGATOFILE                  		Dump SGA to file; dirname in double quotesDMPCOWSGA       		 		Dump & map SGA as COW; dirname in double quotesMAPCOWSGA                  		Map SGA as COW; dirname in double quotesHANGANALYZE    [level] [syslevel]        		Analyze system hangFFBEGIN                                  		Flash Freeze the InstanceFFDEREGISTER                             		FF deregister instance from clusterFFTERMINST                               		Call exit and terminate instanceFFRESUMEINST                             		Resume the flash frozen instanceFFSTATUS                                 		Flash freeze status of instanceSKDSTTPCS                		Helps translate PCs to namesWATCH              Watch a region of memoryDELETE          watchpoint     Delete a watchpointSHOW            watchpoints        Show  watchpointsDIRECT_ACCESS   Fixed table accessCORE                                     		Dump core without crashing processIPC                                      		Dump ipc informationUNLIMIT                                  		Unlimit the size of the trace filePROCSTAT                                 		Dump process statisticsCALL            [arg1] ... [argn]  		Invoke function with arguments

使用 oradebug 跟踪 10046 命令

本文来源gaodai^.ma#com搞#代!码网

如下:

SQL> oradebug setmypidStatement processed.// 激活 10046 事件SQL> oradebug event 10046 trace name context forever,level 12;Statement processed.SQL> select /*+ leading(t3) use_merge(t4) */ *  2  from t3, t4  3  where t3.id = t4.t3_id and t3.n = 1100;10 rows selected.// 在当前 session 关闭 10046 事件SQL> oradebug event 10046 trace name context off;Statement processed.// 使用 oradebug tracefile_name 可以直接看到生成的 trace 文件的位置SQL> oradebug tracefile_name;g:\app\davidd\diag\rdbms\david\david\trace\david_ora_2176.trc

其中, 10046 按照收集信息的内容分为以下等级:


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用10046查看执行计划并读懂trace文件
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址