運維經常使用的SQL

運維經常使用的SQL

根據本身的工做經驗,列出一些運維或者DBA經常使用的sql,用於分析SQL消耗狀況,索引信息,kill session,用戶權限查詢等等信息sql

一、統計索引大小

SELECT A.SEGMENT_NAME, SUM(BYTES) / (1024 * 1024 * 1024) AS "size(G)" FROM DBA_SEGMENTS A, DBA_USERS B WHERE A.OWNER = B.USERNAME AND A.OWNER IN ('XXX_01') --索引所屬owner AND B.ACCOUNT_STATUS = 'OPEN' AND A.SEGMENT_TYPE IN ('INDEX', 'INDEX PARTITION') AND A.SEGMENT_NAME IN ('XXX_02') --替換成你要查的索引名

二、查看sql某段時間內的消耗,執行計劃信息

這是我最經常使用的sql,特別推薦數據庫

SELECT * FROM (SELECT TO_CHAR(BEGIN_INTERVAL_TIME, 'yyyy-mm-dd hh24:mi:ss') TIME, S.SQL_ID, S.PLAN_HASH_VALUE, (SELECT SQL_TEXT FROM DBA_HIST_SQLTEXT ST WHERE ST.SQL_ID = S.SQL_ID) SQL_TEXT, SUM(S.EXECUTIONS_DELTA) EXECUTIONS, ROUND((SUM(ELAPSED_TIME_DELTA) / SUM(S.EXECUTIONS_DELTA)) / 1000000, 2) PER_ELAPSED_TIME, ROUND((SUM(S.CPU_TIME_DELTA) / SUM(S.EXECUTIONS_DELTA)) / 1000000, 0) PER_CPU_TIME, ROUND(SUM(S.DISK_READS_DELTA) / SUM(S.EXECUTIONS_DELTA), 0) PER_DISK_READS, ROUND(SUM(S.BUFFER_GETS_DELTA) / SUM(S.EXECUTIONS_DELTA), 0) PER_BUFFER_GETS, ROUND(SUM(S.IOWAIT_DELTA) / SUM(S.EXECUTIONS_DELTA) / 1000000, 0) PER_IOWAIT, ROUND(SUM(ELAPSED_TIME_DELTA) / 1000000, 2) ELAPSED_TIME, ROUND(SUM(S.CPU_TIME_DELTA) / 1000000, 2) CPU_TIME, SUM(S.DISK_READS_DELTA) DISK_READS, SUM(S.BUFFER_GETS_DELTA) BUFFER_GETS, ROUND(SUM(S.IOWAIT_DELTA) / 1000000, 2) IOWAIT FROM DBA_HIST_SNAPSHOT SN, DBA_HIST_SQLSTAT S WHERE S.SNAP_ID = SN.SNAP_ID --and s.parsing_schema_name = 'XXX_01' AND S.SQL_ID = 'XXX_02' --替換成你要查詢的sql_id AND S.EXECUTIONS_DELTA <> 0 AND TO_DATE(TO_CHAR(BEGIN_INTERVAL_TIME, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss') >= TO_DATE('2017-05-16 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND TO_DATE(TO_CHAR(BEGIN_INTERVAL_TIME, 'yyyy-mm-dd hh24:mi:ss'), 'yyyy-mm-dd hh24:mi:ss') < = TO_DATE('2017-05-17 12:00:00', 'yyyy-mm-dd hh24:mi:ss') GROUP BY TO_CHAR(BEGIN_INTERVAL_TIME, 'yyyy-mm-dd hh24:mi:ss'), S.SQL_ID, S.PLAN_HASH_VALUE, S.PARSING_SCHEMA_NAME ORDER BY S.SQL_ID, TO_CHAR(BEGIN_INTERVAL_TIME, 'yyyy-mm-dd hh24:mi:ss'), S.PLAN_HASH_VALUE, S.PARSING_SCHEMA_NAME); 

 

sql消耗

三、批量kill sql

當數據庫不停的有高消耗sql進來的時候,能夠用這個語句進行批量killmarkdown

DECLARE
  CURSOR C_MAIN IS
    SELECT * FROM V$SESSION WHERE USERNAME = 'XXX_01' --執行用戶 AND STATUS = 'ACTIVE' --AND SQL_HASH_VALUE = '' AND SQL_ID = 'XXX_02'; --sql_id V_MAIN C_MAIN%ROWTYPE; BEGIN OPEN C_MAIN; LOOP FETCH C_MAIN INTO V_MAIN; EXIT WHEN C_MAIN%NOTFOUND; PRC_KILL_SESSION(V_MAIN.USERNAME, V_MAIN.SID, V_MAIN.SERIAL#); END LOOP; CLOSE C_MAIN; END;

四、取當前執行計劃SQLPLAN

SELECT PLAN_HASH_VALUE, TIMESTAMP, ID, LPAD(' ', DEPTH * 4) || OPERATION OPERATION, OPTIONS, OBJECT_OWNER, OBJECT_NAME, OPTIMIZER, COST, CARDINALITY, BYTES, CPU_COST, IO_COST, TEMP_SPACE, ACCESS_PREDICATES, T.FILTER_PREDICATES FROM V$SQL_PLAN T WHERE SQL_ID = 'XXX_01' --替換成sql_id ORDER BY PLAN_HASH_VALUE, TIMESTAMP, ID;

五、取歷史執行計劃SQLPLAN

SELECT PLAN_HASH_VALUE, TIMESTAMP, ID, LPAD(' ', DEPTH * 4) || OPERATION OPERATION, OPTIONS, OBJECT_OWNER, OBJECT_NAME, OPTIMIZER, COST, CARDINALITY, BYTES, CPU_COST, IO_COST, TEMP_SPACE, ACCESS_PREDICATES FROM DBA_HIST_SQL_PLAN WHERE SQL_ID = 'XXX_01' --替換成sql_id /* and plan_hash_value = '887817209'*/ ORDER BY PLAN_HASH_VALUE, ID;

 

 

六、查看存儲過程

SELECT * FROM DBA_SOURCE WHERE NAME=upper('MY_PACKAGE_DEMO');

七、獲取綁定變量

SELECT VALUE_STRING FROM V$SQL_BIND_CAPTURE WHERE SQL_ID = 'XXX_01'; --可獲取歷史sql綁定變量 SELECT SNAP_ID, NAME, POSITION, VALUE_STRING, LAST_CAPTURED, WAS_CAPTURED FROM DBA_HIST_SQLBIND WHERE SQL_ID = 'XXX_01' --AND SNAP_ID = '133030'; --快照

 

八、查表佔用的大小

SELECT T.SEGMENT_NAME, BYTES / 1024 / 1024 "Space(MB)" FROM DBA_SEGMENTS T WHERE SEGMENT_NAME = UPPER('TABLE_NAME');

 

九、表和索引的統計信息

--查看錶的統計信息 SELECT A.OWNER, A.TABLE_NAME, A.NUM_ROWS, A.LAST_ANALYZED, A.STATTYPE_LOCKED FROM DBA_TAB_STATISTICS A WHERE A.TABLE_NAME IN ('YOUR_TABLE_01', 'YOUR_TABLE_02', 'YOUR_TABLE_03'); --查看索引的統計信息 SELECT T.OWNER, T.INDEX_NAME, T.TABLE_NAME, T.LAST_ANALYZED, T.STATTYPE_LOCKED FROM DBA_IND_STATISTICS T WHERE T.INDEX_NAME IN ('IX_INDEX_01', 'IX_INDEX_02', 'IX_INDEX_03', 'PK_INDEX_01');
相關文章
相關標籤/搜索