分析sql效能

2021-04-07 01:39:51 字數 1739 閱讀 6657

--很多時候,我們不太清楚自己寫的sql語句好還是不好,往往資料量一大,程式執行變慢。其實在sql/plus裡可以很清晰的分析出sql語句的執行計畫,它可以提醒我們來建立索引或改變sql語句的寫法。

--先在sys使用者下執行@/oracle_home/rdbms/sqlplus/admin/plustrce.sql

--內容:

set echo on

drop role plustrace;

create role plustrace;

grant select on v_$sesstat to plustrace;

grant select on v_$statname to plustrace;

grant select on v_$session to plustrace;

grant plustrace to dba with admin option;

set echo off

--產生plustrace角色,然後在sys使用者下把此角色賦予一般使用者

--sql> grant plustrace to db_user;

--先找到/oracle_home/rdbms/admin/utlxplan.sql,然後在當前使用者sql>下執行,它建立乙個plan_table,用來儲存分析sql語句的結果。

create table plan_table (

statement_id varchar2(30),

timestamp date,

remarks varchar2(80),

operation varchar2(30),

options varchar2(30),

object_node varchar2(128),

object_owner varchar2(30),

object_name varchar2(30),

object_instance numeric,

object_type varchar2(30),

optimizer varchar2(255),

search_columns number,

id numeric,

parent_id numeric,

position numeric,

cost numeric,

cardinality numeric,

bytes numeric,

other_tag varchar2(255),

partition_start varchar2(255),

partition_stop varchar2(255),

partition_id numeric,

other long,

distribution varchar2(30));

--在sql/plus的視窗執行以下命令

set time on; (說明:開啟時間顯示)

set autotrace on; (說明:開啟自動分析統計,並顯示sql語句的執行結果)

set autotrace traceonly; (說明:開啟自動分析統計,不顯示sql語句的執行結果)

--接下來你就執行測試sql語句,看到其分析統計結果了。一般來講,我們的sql語句應該避免對大表的全表掃瞄。

--關閉以上功能,在sql/plus的視窗執行以下命令

set time off; (說明:關閉時間顯示)

set autotrace off; (說明:關閉自動分析統計)

分析SQL語句的效能

由於要分析sql profiler捕獲的sql 語句的效能,需要找出執行頻率高,用時長的語句。sql profiler生成的表如下 create table dbo lijidownload rownumber int identity not null,eventclass int null,te...

分析SQL語句的效能

1.檢視執行時間和cpu占用時間 set statistics time on select from 表set statistics time off 結果 2.檢視查詢對i 0的操作情況 set statistics io on select from 表set statistics io of...

利用EXPLAIN分析sql語句的效能

使用explain關鍵字可以模擬優化器執行sql查詢語句,從而知道mysql是如何處理你的sql語句,可以幫助選擇更好的索引和寫出更優化的查詢語句。explain 的每個輸出行包括下面的列 select查詢的序列號,包含一組數字,表示查詢中執行select子句或操作表的順序。有三種情況 select...