oracle优化求生指南脚本记录
1.查找未使用索引
/* Formatted on 2020/5/12 下午 03:32:39 (QP5 v5.163.1008.3004) */ WITH IN_PLAN_OBJECTS AS (SELECT DISTINCT OBJECT_NAME FROM V$SQL_PLAN WHERE OBJECT_OWNER = USER) SELECT TABLE_NAME, INDEX_NAME, CASE WHEN OBJECT_NAME IS NULL THEN 'NO' ELSE 'YES' END AS IN_CACHED_PLAN FROM USER_INDEXES LEFT OUTER JOIN IN_PLAN_OBJECTS ON (INDEX_NAME = OBJECT_NAME);
或
/*对所有索引添加MONITOR*/ BEGIN FOR r IN (SELECT index_name FROM user_indexes) LOOP EXECUTE IMMEDIATE 'ALTER INDEX ' || r.index_name || ' MONITORING USAGE'; END LOOP; END; SELECT INDEX_NAME, TABLE_NAME, USED, START_MONITORING FROM V$OBJECT_USAGE WHERE MONITORING = 'YES';
2.查找数据库中应该修改为绑定变量减少硬解析的SQL
/* Formatted on 2020/5/13 下午 05:43:03 (QP5 v5.163.1008.3004) */ WITH FORCE_MATCHES AS ( SELECT FORCE_MATCHING_SIGNATURE, COUNT (*) MATCHES, MAX (SQL_ID || CHILD_NUMBER) MAX_SQL_CHILD, DENSE_RANK () OVER (ORDER BY COUNT (*) DESC) RANKING FROM V$SQL WHERE FORCE_MATCHING_SIGNATURE <> 0 AND PARSING_SCHEMA_NAME <> 'SYS' GROUP BY FORCE_MATCHING_SIGNATURE HAVING COUNT (*) > 5) SELECT SQL_ID, MATCHES, PARSING_SCHEMA_NAME SCHEMA, SQL_TEXT FROM V$SQL JOIN FORCE_MATCHES ON (SQL_ID || CHILD_NUMBER = MAX_SQL_CHILD) WHERE RANKING <= 10 ORDER BY MATCHES DESC;