美文网首页
Performance & Troubleshooting sc

Performance & Troubleshooting sc

作者: 开心太平洋 | 来源:发表于2018-06-27 15:35 被阅读0次

    1.操作系统环境变量

    运行tpt脚本之前,唯一需要设置的环境变量只有一个SQLPATH,使其指向tpt脚本的目录路径,如下所示

    • Unix/Linux/MacOS

    将下面的环境变量加入shell的对应配置文件中,如.bash_profile,.profile,.zshrc等

    export SQLPATH=~/tpt
    
    • Windows
      通过环境变量配置界面增加SQLPATH环境变量,命令方式如下
    set SQLPATH=D:\tpt
    

    ==注意== 在Oracle 12c版本及以上的版本中,只设置SQLPATH环境变量是无法运行tpt脚本的,还需要设置一个操作系统 环境变量,变量名ORACLE_PATH,使其指向tpt目录的路径,即

    export ORACLE_PATH=~/tpt
    or
    set ORACLE_PATH=~/tpt
    

    2.验证tpt脚本是否正常运作

    验证tpt脚本是否正常运作的方法如下:

    • 正常运行
    image-20180627113826579.png

    通过sql plus登陆数据库以后,会显示whoami信息,即上图中最后三行显示的内容

    • 异常运行
    image-20180627114017305.png

    在没有设置好SQLPATH和ORACLE_PATH环境变量时,通过sqlplus命令登陆数据库后,只显示sqlplus版本信息,而不显示whoami信息。这是因为sqlplus登陆时默认会到$SQLPATH目录中查找并执行login.sql文件,而login.sql文件会调用whoami脚本文件i.sql来显示当前会话信息,如果不设置SQLPATH环境变量,就找不到login.sql文件并执行它。login.sql文件的文件内容如下

    -- calling init.sql which will set up sqlpus variables
    @init.sql
    -- i.sql is the "who am i" script which shows your session/instance info and
    -- also sets command prompt window/xterm title
    @i.sql
    
    -- you can put your own login scripts here
    

    Login.sql文件分别依次调用init.sqli.sql文件,init.sql文件定义了SQL PLUS中常用的变量信息,i.sql文件则显示whoami信息,并设置终端的提示信息。

    3.SQL PLUS环境变量

    SQL PLUS环境变量的初始工作主要集中在了init.sql中,下面详细解释一下这个脚本

    • 定义SQLPATH环境变量
    -- this must be here to avoid logon problems when SQLPATH env variable is unset
    def SQLPATH=""
    
    
    -- set SQLPATH variable to either Unix or Windows format
    
    def SQLPATH=$SQLPATH -- (Unix/Mac OSX)
    --def SQLPATH=%SQLPATH% -- (Windows)
    
    
    • 定义常量_start——默认打开文件的工具,windows下为start命令,Unix/Linux为firefox浏览器,MacOS下为Open命令
    -- def _start=start   -- Windows
    -- def _start=firefox -- Unix/Linux
    def _start=open -- MacOS
    
    • 定义常量_delete——默认删除文件的命令,Unix/MacOS下为rm -rf命令,windows下为del命令
    def _delete="rm -f" -- Unix/MacOSX
    -- def _delete="del" -- Windows
    
    • 定义常量_tpt_tempdir——tpt脚本的临时目录,当前的值为$SQLPATH/tmp目录
    def _tpt_tempdir=&SQLPATH/tmp
    
    • 定义tpt脚本内部需要使用的变量,_ti_sequence,_tptmode,_xt_seq,作用未知。
    -- some internal variables required for TPT scripts
    
        define _ti_sequence=0
        define _tptmode=normal
        define _xt_seq=0
    
    • 定义常量all——数据库中的所有会话ID,取自v$session视图中的sid字段
      define all='"select /*+ no_merge */ sid from v$session"'
    
    • 定义常量prev——当前会话的上一次执行SQL的sql_id,取自v$session中的prev_sql_id字段
      define prev="(select /*+ no_unnest */ prev_sql_id from v$session where sid = (select sid from v$mystat where rownum=1))"
    
    • 定义用于ASH脚本的时间范围常量,这些常量包括1分钟、5分钟、1小时、2小时、24小时、1天、当天0点,这些时间范围指的是指定时间到当前时间的时间范围 ,如1分钟指的是1分钟前~当前时间,当天0点指的当天0点~当前时间这个时间范围,常量名分别为min,minute,5min,hour,2hours,24hours,day,today,请大家对号入坐
      -- geeky shorcuts for producing date ranges for various ASH scripts
      define     min="sysdate-1/24/60 sysdate"
      define  minute="sysdate-1/24/60 sysdate"
      define    5min="sysdate-1/24/12 sysdate"
      define    hour="sysdate-1/24 sysdate"
      define   2hours="sysdate-1/12 sysdate"
      define  24hours="sysdate-1 sysdate"
      define      day="sysdate-1 sysdate"
      define    today="TRUNC(sysdate) sysdate"
    
    • 设置 sqlplus的环境配置,如linesize,pagesize等
    -- you should change linesize to match terminal width - 1 only
    -- if you don't have a terminal with horizontal scrolling
    -- capability (cmd.exe and Terminator terminal do have horizontal scrolling)
    
        set linesize 999
    
    -- set truncate after linesize on
    
        -- set truncate on
    
    -- set pagesize larger to avoid repeting headings
    
        set pagesize 5000
    
    -- fetch 10000000 bytes of long datatypes. good for
    -- querying DBA_VIEWS and DBA_TRIGGERS
    
        set long 10000000
        set longchunksize 10000000
    
    -- larger arraysize for faster fetching of data
    -- note that arraysize can affect outcome of experiments
    -- like buffer gets for select statements etc.
    
        set arraysize 500
    
    -- normally I keep this commented out, otherwise
    -- a DBMS_OUTPUT.GET_LINES call is made after all
    -- PL/SQL executions from sqlplus. this may distort
    -- execution statistics for experiments
    
        --set serveroutput on size unlimited
    
    -- to have less garbage on screen
    
        set verify off
    
    -- to trim trailing spaces from spool files
    
        set trimspool on
    
    -- to trim trailing spaces from screen output
    
        set trimout on
    
    -- don't use tabs instead of spaces for "wide blanks"
    -- this can mess up the vertical column locations in output
    
        set tab off
    
    • 设置describe命令的环境 ,使其更友好地显示数据库表的字段信息
    -- this makes describe command better to read and more
    -- informative in case of complex datatypes in columns
    
        set describe depth 1 linenum on indent on
    
    • 设置sqlplus中的默认编辑器,当前的值为vi编辑器
    -- you can make sqlplus run any command as your editor
    -- I could use "start notepad" on windows if you want to
    -- return control back to sqlplus immediately after launching
    -- notepad (so that you can continue typing in sqlplus
    
        define _editor="vi -c 'set notitle'"
    --  define _external_editor="/Applications/Terminator.app/Contents/MacOS/Terminator vi "
    
    • 定义trace文件路径变量trc,并获取当前会话的trace文件路径
    -- assign the tracefile name to trc variable
    
        def trc=unknown
    
        column tracefile noprint new_value trc
    
    
        -- its nice to have termout off here as otherwise this would be
        -- displayed on the screen
        set termout off
        
        select value ||'/'||(select instance_name from v$instance) ||'_ora_'||
               (select spid||case when traceid is not null then '_'||traceid else null end
                    from v$process where addr = (select paddr from v$session
                                                 where sid = (select sid from v$mystat
                                                            where rownum = 1
                                                       )
                                            )
               ) || '.trc' tracefile
        from v$parameter where name = 'user_dump_dest';
    
    • 设置默认的日期格式为'YYYY-MM-DD HH24:MI:SS'
    -- make default date format nicer
    
        alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
    
    • 设置sqlplus中的prompt信息
    -- include username and connect identifier in prompt
    
    --  column pr new_value _pr
    --  select initcap('&_user@&_connect_identifier> ') pr from dual;
    --  set sqlprompt "&_pr"
    --  column _pr clear
    
    • 对一些常用字段的显示进行格式化
    -- format some more columns for common DBA queries
    
        col first_change# for 99999999999999999
        col next_change# for 99999999999999999
        col checkpoint_change# for 99999999999999999
        col resetlogs_change# for 99999999999999999
        col plan_plus_exp for a100
        col value_col_plus_show_param ON HEADING  'VALUE'  FORMAT a100
        col name_col_plus_show_param ON HEADING 'PARAMETER_NAME' FORMAT a60
    
    • 设置html格式,通过调用脚本htmlset文件来进行设置
    -- set html format
    
    @@htmlset nowrap "&_user@&_connect_identifier report"
    
    • 设置日志文件的名称,日志文件保存在logs目录中,此文件包含了sqlplus 命令的输入输出内容,文件命名格式为数据库实例名-日期-时间.log,如node80-20180424-143031.log
    -- set seminar logging file
    
    DEF _tpt_tempfile=sqlplus_tmpfile
    
    col seminar_logfile new_value seminar_logfile
    col tpt_tempfile new_value _tpt_tempfile
    
    select
        to_char(sysdate, 'YYYYMMDD-HH24MISS') seminar_logfile
      , instance_name||'-'||to_char(sysdate, 'YYYYMMDD-HH24MISS') tpt_tempfile
    from v$instance;
    
    def seminar_logfile=&SQLPATH/logs/&_tpt_tempfile..log
    
    • 将sqlplus命令的输出以追加的方式保存在日志文件中。
    -- spool sqlplus output
    spool &seminar_logfile append
    
    set editfile afiedit.sql
    
    -- set up a default ref cursor for Snapper V4 begin/end snapshotting
    -- var snapper refcursor
    
    -- reset termout back to normal
    
        set termout on
    

    4. whoami脚本

    whoami脚本i.sql定义我是谁信息,具体显示信息如下

    image-20180627141903699.png

    显示的信息包括当前登陆的用户名,数据库实例名、主机名、SID及SERIAL#,数据库版本、数据库启动时间、当前会话进程ID等信息,同时定义了一些关于当前会话信息的常量信息,如mysid,_i_spid,_i_opid等,具体内容可以参考i.sql文件内容。最后i.sql脚本文件通过window操作系统的titledoskey命令以及Unix/MacOS的echo命令来设置终端的标签显示信息,具体实现的脚本内容如下

    -- Windows CMD.exe specific stuff
    
    --host title &_i_user@&_i_conn [sid=&mysid ser#=&_i_serial spid=&_i_spid inst=&_i_inst host=&_i_host cpid=&_i_cpid opid=&_i_opid]
    --host doskey /exename=sqlplus.exe desc=set lines 80 sqlprompt ""$Tdescribe $*$Tset lines 299 sqlprompt "SQL> "
    
    -- short xterm title
    host echo -ne "\033]0;&_i_user@&_i_inst &mysid[&_i_spid]\007"
    -- long xterm title
    --host echo -ne "\033]0;host=&_i_
    

    显示效果如下显示

    image-20180627144217058.png

    上面的标签内容则依次显示了当前登陆的用户名、实例名称、当前会话SID以及当前会话的操作系统进程ID。

    5.相关参考及引用文档

    • tpt/login.sql
    • tpt/init.sql
    • tpt/i.sql

    相关文章

      网友评论

          本文标题:Performance & Troubleshooting sc

          本文链接:https://www.haomeiwen.com/subject/baobyftx.html