原文地址:https://docs.oracle.com/en/database/oracle/oracle-database/12.2/tgsql/managing-sql-tuning-sets.html#GUID-91D1B886-A6D7-40B8-93D5-112B8C6E6AFE
其实,这个话题是一道OCM考试题,只是考试的时候,考场环境会提供已经做好的sql set ,只需要数据泵导入,加压后就可以进行sql性能分析了 。今天看文档,自己做个sql set 。
先上个图 。
-- 手工执行STS的步骤
1 create a new STS
2 load the STS with SQL statements and associated metadata
3 Optionally,display the contents of the STS
4 Optionally,update or delete the contents of the STS
5 create a tuning task with the STS as input
6 optionally,transport the STS to another database
7 drop the STS when finshed
-- 创建新的sts,并确认新创建的sts
BEGIN
DBMS_SQLTUNE.CREATE_SQLSET (
sqlset_name => 'SQLT_WKLD_STS'
, description => 'STS to store SQL from the private SQL area'
);
END;
COLUMN NAME FORMAT a20
COLUMN COUNT FORMAT 99999
COLUMN DESCRIPTION FORMAT a30
SELECT NAME, STATEMENT_COUNT AS "SQLCNT", DESCRIPTION
FROM USER_SQLSET;
SELECT NAME, STATEMENT_COUNT AS "SQLCNT", DESCRIPTION
FROM USER_SQLSET;SYS@test>SYS@test>SYS@test> 2
NAME SQLCNT DESCRIPTION
-------------------- ---------- ------------------------------
SQLACCESS1911833_sts 1144 Obtain workload from cursor cache
SQLACCESS4709044_sts 1167 Obtain workload from cursor cache
SQLT_WKLD_STS 0 STS to store SQL from the private SQL area
SYS@test>
-- 加载sts
DECLARE
c_sqlarea_cursor DBMS_SQLTUNE.SQLSET_CURSOR;
BEGIN
OPEN c_sqlarea_cursor FOR
SELECT VALUE(p)
FROM TABLE(
DBMS_SQLTUNE.SELECT_CURSOR_CACHE(
' module = ''SQLT_WKLD'' AND parsing_schema_name = ''SH'' ') -- 要把这里的module 注释掉
) p;
-- load the tuning set
DBMS_SQLTUNE.LOAD_SQLSET (
sqlset_name => 'SQLT_WKLD_STS'
, populate_cursor => c_sqlarea_cursor
);
END;
/
SH@test>DECLARE
c_sqlarea_cursor DBMS_SQLTUNE.SQLSET_CURSOR;
BEGIN
OPEN c_sqlarea_cursor FOR
SELECT VALUE(p)
FROM TABLE(
DBMS_SQLTUNE.SELECT_CURSOR_CACHE(
'parsing_schema_name = ''SH''') -- 要把这里的module 注释掉
) p;
-- load the tuning set
DBMS_SQLTUNE 2 3 4 5 6 7 8 9 10 11 .LOAD_SQLSET (
sqlset_name => 'SQLT_WKLD_STS'
, populate_cursor => c_sqlarea_cursor
);
END; 12 13 14 15
16 /
PL/SQL procedure successfully completed.
SH@test>
--显示sts ,一直没有结果 ,原因,是因为上面的语句,moudle语句,
COLUMN SQL_TEXT FORMAT a30
COLUMN SCH FORMAT a3
COLUMN ELAPSED FORMAT 999999999
SELECT SQL_ID, PARSING_SCHEMA_NAME AS "SCH", SQL_TEXT,
ELAPSED_TIME AS "ELAPSED", BUFFER_GETS
FROM TABLE( DBMS_SQLTUNE.SELECT_SQLSET( 'SQLT_WKLD_STS' ) );
SH@test>COLUMN SQL_TEXT FORMAT a30
COLUMN SCH FORMAT a3
COLUMN ELAPSED FORMAT 999999999
SELECT SQL_ID, PARSING_SCHEMA_NAME AS "SCH", SQL_TEXT,
ELAPSED_TIME AS "ELAPSED", BUFFER_GETS
FROM TABLE( DBMS_SQLTUNE.SELECT_SQLSET( 'SQLT_WKLD_STS' ) );SH@test>SH@test>SH@test> 2 3
SQL_ID SCH SQL_TEXT ELAPSED BUFFER_GETS
------------- --- ------------------------------ ---------- -----------
35whmj2v6jq1z SH DECLARE 87309 696
c_sqlarea_cursor DBMS_SQLTUNE.
SQLSET_CURSOR;
BEGIN
OPEN c_sqlarea_cursor
37rpuv6jc0wbh SH select sum(quantity_sold) from 78757 1755
sales s, products p
where s.prod_id = p.prod_id
a
SQL_ID SCH SQL_TEXT ELAPSED BUFFER_GETS
------------- --- ------------------------------ ---------- -----------
74mn6d6kzp1xf SH SELECT VALUE(P) FROM TABLE( DB 75917 417
MS_SQLTUNE.SELECT_CURSOR_CACHE
( 'AND parsing_schem
9fhs269dd284z SH select * from sales where quan 6054661 7659
tity_sold < 5
union select * from sales wher
e quan
a219bj5sat1yd SH SELECT VALUE(P) FROM TABLE( DB 55290 530
MS_SQLTUNE.SELECT_CURSOR_CACHE
SQL_ID SCH SQL_TEXT ELAPSED BUFFER_GETS
------------- --- ------------------------------ ---------- -----------
( 'parsing_schema_na
ax51s81797wdk SH SELECT ABS(MAX(PRIVILEGE)) FRO 6905 125
M SYS.SYSTEM_PRIVILEGE_MAP WHE
RE NAME = :B1
bzmnj0nbvmz8t SH select * from sales where amou 59653 1635
nt_sold = 4
cz3qhr9d52sw5 SH SELECT /*+ first_rows(1) */ s 47702 222
ql_id, force_matching_signatur
SQL_ID SCH SQL_TEXT ELAPSED BUFFER_GETS
------------- --- ------------------------------ ---------- -----------
e, sql_text, cast(NU
dkak0z66jw4vw SH select promo_name, count(*) c 630218 236
from promotions p, sales s whe
re s.promo_id = p.pr
gc81btz8wjs7s SH BEGIN 12903 219
DBMS_SQLTUNE.CREATE_SQLSET (
sqlset_name => 'SQLT_WKLD_STS'
, description
SQL_ID SCH SQL_TEXT ELAPSED BUFFER_GETS
------------- --- ------------------------------ ---------- -----------
10 rows selected.
SH@test>
--完了之后,再次查询sql tune set里面包含的SQL语句,有10个
SH@test>SELECT NAME, STATEMENT_COUNT AS "SQLCNT", DESCRIPTION
FROM USER_SQLSET; 2
NAME SQLCNT DESCRIPTION
-------------------- ---------- ------------------------------
SQLT_WKLD_STS 10 STS to store SQL from the priv
ate SQL area
SH@test>
-- 传输sts ,这里会生成一个表,sql tuning的那些语句后续会存放在这个表里面
BEGIN
DBMS_SQLTUNE.CREATE_STGTAB_SQLSET (
table_name => 'my_12c_staging_table'
, schema_name => 'sh'
, db_version => DBMS_SQLTUNE.STS_STGTAB_12_2_VERSION
);
END;
/
SH@test>BEGIN
DBMS_SQLTUNE.CREATE_STGTAB_SQLSET (
table_name => 'my_12c_staging_table'
, schema_name => 'sh'
, db_version => DBMS_SQLTUNE.STS_STGTAB_12_2_VERSION
);
END; 2 3 4 5 6 7
8 /
PL/SQL procedure successfully completed.
SH@test>
-- 进行pack打包,打包完毕后,就可以看到表中有数据了。然后就可以了expdp导出
BEGIN
DBMS_SQLTUNE.PACK_STGTAB_SQLSET (
sqlset_name => 'SQLT_WKLD_STS'
, sqlset_owner => 'sh'
, staging_table_name => 'my_12c_staging_table'
, staging_schema_owner => 'sh'
, db_version => DBMS_SQLTUNE.STS_STGTAB_12_2_VERSION
);
END;
-- 注意,sqlset_name 要大写
SH@test>BEGIN
DBMS_SQLTUNE.PACK_STGTAB_SQLSET (
sqlset_name => 'SQLT_WKLD_STS'
, sqlset_owner => 'sh'
, staging_table_name => 'my_12c_staging_table'
, staging_schema_owner => 'sh'
, db_version => DBMS_SQLTUNE.STS_STGTAB_12_2_VERSION
);
END; 2 3 4 5 6 7 8 9
10 /
PL/SQL procedure successfully completed.
SH@test>
SH@test>select count(*) from my_12c_staging_table;
COUNT(*)
----------
50
SH@test>
-- 数据泵导出表,因为是12c,想在11g中所以要加上version参数
expdp sh/sh directory=DUMP dumpfile=sts.dmp tables=my_12c_staging_table version=11.2.0.4
[oracle@test trace]$ expdp sh/sh directory=DUMP dumpfile=sts.dmp tables=my_12c_staging_table version=11.2.0.4
Export: Release 12.2.0.1.0 - Production on Wed Mar 20 14:32:26 2019
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
Warning: Oracle Data Pump is exporting from a database that supports long identifiers to a version that does not support long identifiers.
Starting "SH"."SYS_EXPORT_TABLE_01": sh/******** directory=DUMP dumpfile=sts.dmp tables=my_12c_staging_table version=11.2.0.4
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Processing object type TABLE_EXPORT/TABLE/TABLE
. . exported "SH"."MY_12C_STAGING_TABLE" 96.29 KB 50 rows
Master table "SH"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SH.SYS_EXPORT_TABLE_01 is:
/data/dump/sts.dmp
Job "SH"."SYS_EXPORT_TABLE_01" successfully completed at Wed Mar 20 14:34:17 2019 elapsed 0 00:01:37
[oracle@test trace]$
-- 在新环境中导入,然后再unpack
[oracle@atrslinux1 dump]$ impdp sh/sh directory=DUMP dumpfile=sts.dmp
Import: Release 11.2.0.4.0 - Production on Wed Mar 20 06:42:08 2019
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
?????/????? "SH"."SYS_IMPORT_FULL_01"
?? "SH"."SYS_IMPORT_FULL_01": sh/******** directory=DUMP dumpfile=sts.dmp
?????? TABLE_EXPORT/TABLE/TABLE
?????? TABLE_EXPORT/TABLE/TABLE_DATA
. . ??? "SH"."MY_12C_STAGING_TABLE" 96.29 KB 50 ?
?? "SH"."SYS_IMPORT_FULL_01" ?? ??? 3? 20 06:42:21 2019 elapsed 0 00:00:11 ????
[oracle@atrslinux1 dump]$
--进行unpack
SQL> exec dbms_sqltune.unpack_stgtab_sqlset(sqlset_name => '%',replace => true,staging_table_name => 'MY_12C_STAGING_TABLE',staging_schema_owner => 'SH');
PL/SQL procedure successfully completed.
SQL>
-- 然后,在EM上,就可以看到STS的信息了
-- 删除sts
exec DBMS_SQLTUNE.DROP_SQLSET( sqlset_name => 'SQLT_WKLD_STS' );
SELECT COUNT(*) FROM USER_SQLSET WHERE NAME = 'SQLT_WKLD_STS';
--end
-- 官方文档上的一个小插曲,还是我理解错了?
END