sql profile tuning-auto and manual
sql profile tuning-auto and manual
BEGIN
FOR i IN 1..70000 LOOP
INSERT INTO t1 VALUES(i, 'AAAAAAAAAA', 'BBBBBBBB', 'CCCCCCCCCC',
'DDDDDDDDDD');
INSERT INTO t2 VALUES(i, 'AAAAAAAAAA', 'BBBBBBBB', 'CCCCCCCCCC',
'DDDDDDDDDD');
END LOOP;
COMMIT;
END;
/
FROM t1
BEGIN
DBMS_SQLTUNE.EXECUTE_TUNING_TASK('t1_t2_tuning_task');
END;
/
- View Recommendations
- Note: if we do not get stats in above report then we need to gather the stats first for both table
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => 'SYS',
tabname => 'T1',
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO'
);
END;
/
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(
ownname => 'SYS',
tabname => 'T2',
estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt => 'FOR ALL COLUMNS SIZE AUTO'
);
END;
/
- Still if not able to see stats then first check if stats is available or not
EXEC DBMS_SQLTUNE.DROP_TUNING_TASK('t1_t2_tuning_task');
DECLARE
l_task_name VARCHAR2(30);
BEGIN
l_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(
sql_text => 'SELECT /*+ USE_NL(t1 t2) */ COUNT(*) FROM t1 JOIN t2 ON
t1.c1 = t2.c1',
user_name => 'SYS',
scope => DBMS_SQLTUNE.SCOPE_COMPREHENSIVE,
time_limit => 60,
task_name => 't1_t2_tuning_task',
description => 'Tuning task after stats gather for T1 and T2'
);
BEGIN
DBMS_SQLTUNE.EXECUTE_TUNING_TASK(task_name => 't1_t2_tuning_task');
END;
/
BEGIN
DBMS_SQLTUNE.ACCEPT_SQL_PROFILE(
task_name => 't1_t2_tuning_task',
task_owner => 'SYS',
rec_id => <put_rec_id_here>,
replace => TRUE
);
END;
/
- Run your original query without the hint to confirm the SQL profile is kicking in:
▪ It should now be very fast, using a hash join, thanks to the SQL profile.
SELECT COUNT(*)
FROM t1
JOIN t2 ON t1.c1 = t2.c1;
Note- if you are a normal user and want to do tuning task at your end then you should have ADVISOR and
SELECT_CATALOG_ROLE to a particular user
- Command
GRANT ADVISOR TO your_user;
GRANT SELECT_CATALOG_ROLE TO your_user;
- You can auto tuning only when you have sql tuning is installled in your oracle version
- Command to check
Manually tuning
END;
- What to do?
- Try forcing a hash join, which is generally better for large table joins:
FROM t1
JOIN t2 ON t1.c1 = t2.c1;
FROM t1
SET PAGESIZE 50
FROM user_tables