-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathaadvdemo.sql
1602 lines (1025 loc) · 45.5 KB
/
aadvdemo.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- $Header: aadvdemo.sql 29-may-2007.13:15:19 achoi Exp $
--
-- aadvdemo.sql
--
-- Copyright (c) 2003, 2007, Oracle. All rights reserved.
--
-- NAME
-- aadvdemo.sql - SQLAccess Advisor documentation examples
--
-- DESCRIPTION
-- Runs documentation examples pertaining to the SQLAccess Advisor.
-- Chapter 17 in the Data Warehousing Guide.
--
-- NOTES
-- Before running this script, the directories ADVISOR_RESULTS
-- and TUNE_RESULTS need to be defined as first described in
-- Section 17.2.23. If they are not setup, the dbms_advisor.create_file
-- calls will not succeed.
--
-- MODIFIED (MM/DD/YY)
-- achoi 05/29/07 - fix order by
-- gssmith 02/09/07 - Remove Summary Advisor
-- gvincent 11/02/06 - downcase connects for secure verifiers
-- gssmith 05/09/06 - STS conversion
-- pabingha 02/02/05 - BUG 4148628 - disable search parameter
-- mmoy 09/01/04 - Add order by.
-- gssmith 07/20/04 - Add utlxaa to the script
-- lburgess 06/07/04 - add order by clause
-- gssmith 04/30/04 - Add demonstration of 10gR2 features
-- gssmith 04/20/04 - Adjust FAILED count
-- lburgess 11/04/03 - bug 3164691 fixed, uncomment testcase
-- lburgess 10/28/03 - lburgess_doc_examples
-- lburgess 09/17/03 - Created
---------------------------------------------------------------------------
--
-- RUNS_STANDALONE Yes
--
SET ECHO ON
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 80
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 100
----------------------------------------------------------------------
-- Data Warehousing Guide Examples from Chapter 17 - SQLAccess Advisor
----------------------------------------------------------------------
-- Section 17.2.2 SQLAccess Advisor Privileges
----------------------------------------------
-- Grant the ADVISOR privilege to the user.
connect /as sysdba;
grant ADVISOR to sh;
-- Connect to user
connect sh/sh;
-- Section 17.2.3 Creating Tasks
--------------------------------
VARIABLE task_id NUMBER;
VARIABLE task_name VARCHAR2(255);
EXECUTE :task_name := 'MYTASK';
EXECUTE DBMS_ADVISOR.CREATE_TASK ('SQL Access Advisor', :task_id, :task_name);
-- Section 17.2.5 Creating Templates
------------------------------------
-- 1. Create a template called MY_TEMPLATE
VARIABLE template_id NUMBER;
VARIABLE template_name VARCHAR2(255);
EXECUTE :template_name := 'MY_TEMPLATE';
EXECUTE DBMS_ADVISOR.CREATE_TASK('SQL Access Advisor',:template_id, -
:template_name, is_template => 'TRUE');
-- 2. Set template parameters.
-- set naming conventions for recommended indexes/mvs
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -
:template_name, 'INDEX_NAME_TEMPLATE', 'SH_IDX$$_<SEQ>');
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -
:template_name, 'MVIEW_NAME_TEMPLATE', 'SH_MV$$_<SEQ>');
-- First create the tablespaces used in SET_TASK_PARAMETER
-- doc example.
connect /as sysdba;
create tablespace sh_indexes datafile 'shidx.f' size 2m reuse
autoextend on default storage (initial 2k next 2k pctincrease 0
maxextents unlimited);
create tablespace sh_mviews datafile 'shmv.f' size 2m reuse
autoextend on default storage (initial 2k next 2k pctincrease 0
maxextents unlimited);
-- Now continue with the SET_TASK_PARAMETER example.
connect sh/sh;
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -
:template_name, 'DEF_INDEX_TABLESPACE', 'SH_INDEXES');
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -
:template_name, 'DEF_MVIEW_TABLESPACE', 'SH_MVIEWS');
-- 3. This template can now be used as a starting point
-- to create a task as follows:
EXECUTE DBMS_ADVISOR.DELETE_TASK('MYTASK');
VARIABLE task_id NUMBER;
VARIABLE task_name VARCHAR2(255);
EXECUTE :task_name := 'MYTASK';
EXECUTE DBMS_ADVISOR.CREATE_TASK('SQL Access Advisor', :task_id, -
:task_name, template=>'MY_TEMPLATE');
-- Use pre-defined template SQLACCESS_WAREHOUSE
EXECUTE DBMS_ADVISOR.DELETE_TASK('MYTASK');
EXECUTE DBMS_ADVISOR.CREATE_TASK('SQL Access Advisor', -
:task_id, :task_name, template=>'SQLACCESS_WAREHOUSE');
-- Example 17-1 Creating a Workload
-----------------------------------
VARIABLE workload_name VARCHAR2(255);
EXECUTE :workload_name := 'MYWORKLOAD';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:workload_name,'This is my first workload');
-- Example 17-2 Creating a Workload from a Template
---------------------------------------------------
-- 1. Create the variables.
VARIABLE template_id NUMBER;
VARIABLE template_name VARCHAR2(255);
-- 2. Create a template called MY_WK_TEMPLATE.
EXECUTE :template_name := 'MY_WK_TEMPLATE';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:template_name, is_template=>'TRUE');
-- 3. Set template parameters. For example, the following sets the filter so only tables in the sh schema are tuned:
-- set USERNAME_LIST filter to SH
EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER( -
:template_name, 'USERNAME_LIST', 'SH');
-- 4. Now create a workload using the template:
EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD('MYWORKLOAD');
VARIABLE workload_name VARCHAR2(255);
EXECUTE :workload_name := 'MYWORKLOAD';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD ( -
:workload_name, 'This is my first workload', 'MY_WK_TEMPLATE');
-- Section 17.2.8 Linking a Task and a Workload
-----------------------------------------------
EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_REF('MYTASK', 'MYWORKLOAD');
-- Section 17.2.9.1 SQL Tuning Set
----------------------------------
-- First create a SQL Tuning Set called MY_STS_WORKLOAD
declare
sql_stmt varchar2(1000);
begin
sql_stmt :=
'SELECT /* Query */
t.week_ending_day, p.prod_subcategory,
sum(s.amount_sold) AS dollars,
s.channel_id, s.promo_id
FROM sales s, times t, products p
WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id
AND s.prod_id > 10 AND s.prod_id < 50
GROUP BY t.week_ending_day, p.prod_subcategory,
s.channel_id, s.promo_id';
execute immediate sql_stmt;
end;
/
-- Grant privilege to sh for using sql tuning set
connect /as sysdba;
grant ADMINISTER SQL TUNING SET to sh;
connect sh/sh;
DECLARE
sqlsetname VARCHAR2(30);
sqlsetcur dbms_sqltune.sqlset_cursor;
refid NUMBER;
BEGIN
sqlsetname := 'MY_STS_WORKLOAD';
dbms_sqltune.create_sqlset(sqlsetname, 'Test loading from cursor cache');
OPEN sqlsetcur FOR
SELECT VALUE(P)
FROM TABLE(
dbms_sqltune.select_cursor_cache(
'sql_text like ''SELECT /* Query%''',
NULL,
NULL,
NULL,
NULL,
NULL,
null)
) P;
dbms_sqltune.load_sqlset(sqlsetname, sqlsetcur);
end;
/
-- Now create a workload from the SQL Tuning Set.
VARIABLE sqlsetname VARCHAR2(30);
VARIABLE workload_name VARCHAR2(30);
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
EXECUTE :sqlsetname := 'MY_STS_WORKLOAD';
EXECUTE :workload_name := 'MY_WORKLOAD';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD (:workload_name);
EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_STS (:workload_name , -
:sqlsetname, 'NEW', 1, :saved_stmts, :failed_stmts);
-- Section 17.2.9.2 Loading a User-Defined Workload
---------------------------------------------------
--
-- Create a user-workload table that will contain SQL statements.
--
set echo off
@utlxaa
CREATE INDEX aadv_uwk_idx_01
ON user_workload (module);
CREATE INDEX aadv_uwk_idx_02
ON user_workload (username);
--
-- Clean up any prior activity data
--
truncate table user_workload;
--
-- Insert sample SQL statements into the user-workload table
--
--
-- query 1: aggregation with selection
--
INSERT INTO user_workload
(username, module, action, priority, elapsed_time, cpu_time, buffer_gets,
disk_reads, rows_processed, executions, optimizer_cost, last_execution_date,
stat_period, sql_text)
VALUES ('SH', 'Example1', 'Action', 2, 1, 1, 1, 1, 1, 1000, 1, SYSDATE, 1,
'SELECT t.week_ending_day, p.prod_subcategory,
sum(s.amount_sold) AS dollars,
s.channel_id, s.promo_id
FROM sales s, times t, products p
WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id
AND s.prod_id > 10 AND s.prod_id < 50
GROUP BY t.week_ending_day, p.prod_subcategory,
s.channel_id, s.promo_id
');
--
-- query 2: aggregation with selection
--
INSERT INTO user_workload
(username, module, action, priority, elapsed_time, cpu_time, buffer_gets,
disk_reads, rows_processed, executions, optimizer_cost, last_execution_date,
stat_period, sql_text)
VALUES ('SH', 'Example1', 'Action', 2, 1, 1, 1, 1, 1, 1000, 1, SYSDATE, 1,
' SELECT t.calendar_month_desc, sum(s.amount_sold) AS dollars
FROM sales s , times t
WHERE s.time_id = t.time_id
AND s.time_id between TO_DATE(''01-JAN-2000'', ''DD-MON-YYYY'')
AND TO_DATE(''01-JUL-2000'', ''DD-MON-YYYY'')
GROUP BY t.calendar_month_desc
');
--
-- query 3: star query
--
INSERT INTO user_workload
(username, module, action, priority, elapsed_time, cpu_time, buffer_gets,
disk_reads, rows_processed, executions, optimizer_cost, last_execution_date,
stat_period, sql_text)
VALUES ('SH', 'Example1', 'Action', 2, 1, 1, 1, 1, 1, 1000, 1, SYSDATE, 1,
'SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id
AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id
AND c.cust_state_province = ''CA''
AND ch.channel_desc in (''Internet'',''Catalog'')
AND t.calendar_quarter_desc IN (''1999-Q1'',''1999-Q2'')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc
');
--
-- query 4: order by
--
INSERT INTO user_workload
(username, module, action, priority, elapsed_time, cpu_time, buffer_gets,
disk_reads, rows_processed, executions, optimizer_cost, last_execution_date,
stat_period, sql_text)
VALUES ('SH', 'Example1', 'Action', 2, 1, 1, 1, 1, 1, 1000, 1, SYSDATE, 1,
' SELECT c.country_id, c.cust_city, c.cust_last_name
FROM customers c
WHERE c.country_id in (''US'', ''UK'')
ORDER BY c.country_id, c.cust_city, c.cust_last_name
');
commit;
-- now load the user-defined workload
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_USER( -
'MYWORKLOAD', 'NEW', 'SH', 'USER_WORKLOAD', :saved_stmts, :failed_stmts);
-- Section 17.2.9.3 Loading a SQL Cache Workload
------------------------------------------------
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_SQLCACHE (-
'MYWORKLOAD', 'APPEND', 2, :saved_stmts, :failed_stmts);
-- Section 17.2.9.4 Using a Hypothetical Workload
-------------------------------------------------
VARIABLE workload_name VARCHAR2(255);
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
EXECUTE :workload_name := 'SCHEMA_WKLD';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:workload_name);
EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER (:workload_name, -
'USERNAME_LIST', 'SH');
EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_SCHEMA ( -
:workload_name, 'NEW', 2, :saved_stmts, :failed_stmts);
-- Section 17.2.9.5 Using a 9i Workload
---------------------------------------
-- Before the example in the documentation can be run, an Oracle9i
-- workload must first be created.
-- Run some SQL statements so we have something in the cache
set echo off
connect / as sysdba;
alter system flush shared_pool;
connect sh/sh;
set echo off
SELECT SUM(amount_sold), promo_id FROM sales where promo_id < 8 group by promo_id order by promo_id;
SELECT SUM(amount_sold), channel_id FROM sales group by channel_id order by channel_id;
set echo on
-- load the SQL CACHE workload and check that it is there
VARIABLE collection_id NUMBER;
VARIABLE no_recs NUMBER;
-- The setup is complete, now the documentation example can be shown.
-- 1. Create some variables.
VARIABLE workload_name VARCHAR2(255);
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
-- Section 17.2.9.6 SQLAccess Advisor Workload Parameters
---------------------------------------------------------
-- Order statements by OPTIMIZER_COST
EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER ( -
'MYWORKLOAD', 'ORDER_LIST', 'OPTIMIZER_COST');
-- Max number of statements 3
EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER('MYWORKLOAD', 'SQL_LIMIT', 3);
-- Section 17.2.10 SQL Workload Journal
---------------------------------------
-- You can turn journaling off before importing workload as follows:
EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER('MYWORKLOAD', 'JOURNALING', 0);
-- To view only fatal messages:
EXECUTE DBMS_ADVISOR.SET_SQLWKLD_PARAMETER('MYWORKLOAD', 'JOURNALING', 4);
-- Section 17.2.11 Adding SQL Statements to a Workload
------------------------------------------------------
VARIABLE sql_text VARCHAR2(400);
EXECUTE :sql_text := 'SELECT AVG(amount_sold) FROM sales';
EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_STATEMENT ( -
'MYWORKLOAD', 'MONTHLY', 'ROLLUP', priority=>1, executions=>10, -
username => 'SH', sql_text => :sql_text);
-- Section 17.2.12 Deleting SQL Statements from a Workload
----------------------------------------------------------
-- Delete using sql_id
EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD_STATEMENT('MYWORKLOAD', 10);
-- Section 17.2.13 Changing SQL Statements in a Workload
--------------------------------------------------------
EXECUTE DBMS_ADVISOR.UPDATE_SQLWKLD_STATEMENT( -
'MYWORKLOAD', 2, priority=>3);
-- Section 17.2.14.1 Setting Workload Attributes
------------------------------------------------
EXECUTE DBMS_ADVISOR.UPDATE_SQLWKLD_ATTRIBUTES ( -
'MYWORKLOAD', read_only=> 'TRUE');
-- Section 17.2.14.2 Resetting Workloads
----------------------------------------
-- need to first set it to be readable
EXECUTE DBMS_ADVISOR.UPDATE_SQLWKLD_ATTRIBUTES ( -
'MYWORKLOAD', read_only=> 'FALSE');
EXECUTE DBMS_ADVISOR.RESET_SQLWKLD('MYWORKLOAD');
-- Section 17.2.14.3 Removing a Link Between a Workload and a Task
------------------------------------------------------------------
EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD_REF('MYTASK', 'MYWORKLOAD');
-- Section 17.2.15 Removing Workloads
-------------------------------------
EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD('MYWORKLOAD');
-- Section 17.2.16 Recommendation Options
-----------------------------------------
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER('MYTASK','STORAGE_CHANGE', 100);
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -
'MYTASK', 'VALID_TABLE_LIST', 'SH.SALES, SH.CUSTOMERS');
-- clean-up
EXECUTE dbms_advisor.delete_task('MYTASK');
-- Section 17.2.17 Generating Recommendations
---------------------------------------------
-- Steps 1 through 6 are the required setup steps to be able to execute a
-- task and have some worthwhile data to view after its execution.
-- Step 1 Prepare the USER_WORKLOAD table
-- The USER_WORKLOAD table is loaded with SQL statements as follows:
connect sh/sh;
-- aggregation with selection
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT t.week_ending_day, p.prod_subcategory,
SUM(s.amount_sold) AS dollars, s.channel_id, s.promo_id
FROM sales s, times t, products p WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id AND s.prod_id > 10 AND s.prod_id < 50
GROUP BY t.week_ending_day, p.prod_subcategory,
s.channel_id, s.promo_id')
/
-- aggregation with selection
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT t.calendar_month_desc, SUM(s.amount_sold) AS dollars
FROM sales s , times t
WHERE s.time_id = t.time_id
AND s.time_id between TO_DATE(''01-JAN-2000'', ''DD-MON-YYYY'')
AND TO_DATE(''01-JUL-2000'', ''DD-MON-YYYY'')
GROUP BY t.calendar_month_desc')
/
--Load all SQL queries.
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id AND c.cust_state_province = ''CA''
AND ch.channel_desc IN (''Internet'',''Catalog'')
AND t.calendar_quarter_desc IN (''1999-Q1'',''1999-Q2'')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc')
/
-- order by
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT c.country_id, c.cust_city, c.cust_last_name
FROM customers c WHERE c.country_id in (''US'', ''UK'')
ORDER BY c.country_id, c.cust_city, c.cust_last_name')
/
COMMIT;
connect sh/sh;
set serveroutput on;
VARIABLE task_id NUMBER;
VARIABLE task_name VARCHAR2(255);
VARIABLE workload_name VARCHAR2(255);
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
-- Step 2 Create a workload named MYWORKLOAD
EXECUTE :workload_name := 'MYWORKLOAD';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:workload_name);
-- Step 3 Load the workload from user-defined table SH.USER_WORKLOAD
EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_USER (:workload_name, 'APPEND', 'SH', -
'USER_WORKLOAD', :saved_stmts, :failed_stmts);
PRINT :saved_stmts;
PRINT :failed_stmts;
-- Step 4 Create a task named MYTASK
EXECUTE :task_name := 'MYTASK';
EXECUTE DBMS_ADVISOR.CREATE_TASK('SQL Access Advisor', :task_id, :task_name);
-- Step 5 Set task parameters
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER(:task_name, 'STORAGE_CHANGE', 100);
EXECUTE DBMS_ADVISOR.SET_TASK_PARAMETER ( -
:task_name, 'EXECUTION_TYPE', 'INDEX_ONLY');
-- Step 6 Create a link between workload and task
EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_REF(:task_name, :workload_name);
-- Now execute the task for section 17.2.17.1
EXECUTE DBMS_ADVISOR.EXECUTE_TASK('MYTASK');
-- Section 17.2.18 Viewing the Recommendations
----------------------------------------------
VARIABLE workload_name VARCHAR2(255);
VARIABLE task_name VARCHAR2(255);
EXECUTE :task_name := 'MYTASK';
EXECUTE :workload_name := 'MYWORKLOAD';
SELECT REC_ID, RANK, BENEFIT
FROM USER_ADVISOR_RECOMMENDATIONS WHERE TASK_NAME = :task_name;
SELECT sql_id, rec_id, precost, postcost,
(precost-postcost)*100/precost AS percent_benefit
FROM USER_ADVISOR_SQLA_WK_STMTS
WHERE TASK_NAME = :task_name
order by sql_id;
SELECT 'Action Count', COUNT(DISTINCT action_id) cnt
FROM user_advisor_actions WHERE task_name = :task_name;
-- see the actions for each recommendations
SELECT rec_id, action_id, SUBSTR(command,1,30) AS command
FROM user_advisor_actions WHERE task_name = :task_name
ORDER BY rec_id, action_id;
-- The following PL/SQL procedure can be used to print out some of
-- the attributes of the recommendations.
connect sh/sh;
CREATE OR REPLACE PROCEDURE show_recm (in_task_name IN VARCHAR2) IS
CURSOR curs IS
SELECT DISTINCT action_id, command, attr1, attr2, attr3, attr4
FROM user_advisor_actions
WHERE task_name = in_task_name
ORDER BY action_id;
v_action number;
v_command VARCHAR2(32);
v_attr1 VARCHAR2(4000);
v_attr2 VARCHAR2(4000);
v_attr3 VARCHAR2(4000);
v_attr4 VARCHAR2(4000);
v_attr5 VARCHAR2(4000);
BEGIN
OPEN curs;
DBMS_OUTPUT.PUT_LINE('=========================================');
DBMS_OUTPUT.PUT_LINE('Task_name = ' || in_task_name);
LOOP
FETCH curs INTO
v_action, v_command, v_attr1, v_attr2, v_attr3, v_attr4 ;
EXIT when curs%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Action ID: ' || v_action);
DBMS_OUTPUT.PUT_LINE('Command : ' || v_command);
DBMS_OUTPUT.PUT_LINE('Attr1 (name) : ' || SUBSTR(v_attr1,1,30));
DBMS_OUTPUT.PUT_LINE('Attr2 (tablespace): ' || SUBSTR(v_attr2,1,30));
DBMS_OUTPUT.PUT_LINE('Attr3 : ' || SUBSTR(v_attr3,1,30));
DBMS_OUTPUT.PUT_LINE('Attr4 : ' || v_attr4);
DBMS_OUTPUT.PUT_LINE('Attr5 : ' || v_attr5);
DBMS_OUTPUT.PUT_LINE('----------------------------------------');
END LOOP;
CLOSE curs;
DBMS_OUTPUT.PUT_LINE('=========END RECOMMENDATIONS============');
END show_recm;
/
-- see what the actions are using sample procedure
set serveroutput on size 99999
EXECUTE show_recm(:task_name);
-- Section 17.2.19 Access Advisor Journal
-----------------------------------------
-- This section moved to the end of section 17.2.28 since
-- it requires the task to be reset.
-- Section 17.2.20 Stopping the recommendation Process
------------------------------------------------------
-- Note, a task must be executing for this to succeed. This
-- testcase just proves the syntax is correct. Expect an error
-- such as "task must be executing to be cancelled".
EXECUTE DBMS_ADVISOR.CANCEL_TASK('MYTASK');
-- Section 17.2.21 Marking Recommendations
------------------------------------------
EXECUTE DBMS_ADVISOR.MARK_RECOMMENDATION('MYTASK', 2, 'REJECT');
-- Section 17.2.22 Modifying Recommendations
--------------------------------------------
-- Before the TABLESPACE recommendation attribute can be updated,
-- recommendations of the CREATE type must exist. Steps 1 through 6
-- do the required setup for this.
-- 1. Create task
VARIABLE task_id2 NUMBER;
VARIABLE task_name2 VARCHAR2(255);
EXECUTE :task_name2 := 'MYTASK2';
EXECUTE DBMS_ADVISOR.CREATE_TASK ('SQL Access Advisor', :task_id2, -
:task_name2);
-- 2. Create workload
VARIABLE workload_name2 VARCHAR2(255);
EXECUTE :workload_name2 := 'MYWORKLOAD2';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:workload_name2,'This is my test workload');
-- 3. Link workload and task
EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_REF(:task_name2, :workload_name2);
-- 4. Add a sql statement to the workload
VARIABLE sql_text2 VARCHAR2(400);
EXECUTE :sql_text2 := 'SELECT AVG(amount_sold) FROM sales';
EXECUTE DBMS_ADVISOR.ADD_SQLWKLD_STATEMENT ( -
:workload_name2, 'MONTHLY', 'ROLLUP', priority=>1, executions=>10, -
username => 'SH', sql_text => :sql_text2);
-- 5. Execute the task
EXECUTE DBMS_ADVISOR.EXECUTE_TASK(:task_name2);
-- 6. See the actions recommended
SELECT rec_id, action_id, SUBSTR(command,1,30) AS command
FROM user_advisor_actions WHERE task_name = :task_name2
ORDER BY rec_id, action_id;
-- The following example modifies the attribute TABLESPACE for recommendation
-- ID 1, action ID 1 to SH_MVIEWS.
EXECUTE DBMS_ADVISOR.UPDATE_REC_ATTRIBUTES(:task_name2, 1, 2, 'TABLESPACE', -
'SH_MVIEWS');
-- Section 17.2.23 Generating SQL Scripts
-----------------------------------------
-- Define a directory "ADVISOR_RESULTS" and grant permissions to
-- read/write to it.
--
-- connect sh/sh;
-- CREATE DIRECTORY ADVISOR_RESULTS AS '/mydir';
-- GRANT READ ON DIRECTORY ADVISOR_RESULTS TO PUBLIC;
-- GRANT WRITE ON DIRECTORY ADVISOR_RESULTS TO PUBLIC;
-- save script CLOB to file
-- Note: When "get_task_script" passed in as a parameter you get the error
-- 'invalid file script'. This is a problem with how SQLPlus handles
-- CLOBs. Doc changed to call GET_TASK_SCRIPT directly in CREATE_FILE.
-- generate script as a CLOB
--variable scriptbuf CLOB;
--EXECUTE :scriptbuf := 'DBMS_ADVISOR.GET_TASK_SCRIPT(''MYTASK'')';
-- doesn't work
--EXECUTE DBMS_ADVISOR.CREATE_FILE(:scriptbuf, 'ADVISOR_RESULTS',-
-- 'advscript.sql');
-- works
EXECUTE DBMS_ADVISOR.CREATE_FILE(DBMS_ADVISOR.GET_TASK_SCRIPT('MYTASK'), 'ADVISOR_RESULTS','advscript.sql');
-- Section 17.2.24 When Recommendations are No Longer Required
--------------------------------------------------------------
EXECUTE DBMS_ADVISOR.RESET_TASK('MYTASK');
-- Section 17.2.25 Performing a Quick Tune
------------------------------------------
VARIABLE task_name VARCHAR2(255);
VARIABLE sql_stmt VARCHAR2(4000);
EXECUTE :sql_stmt := 'SELECT COUNT(*) FROM customers -
WHERE cust_state_province=''CA''';
EXECUTE :task_name := 'MY_QUICKTUNE_TASK';
EXECUTE DBMS_ADVISOR.QUICK_TUNE(DBMS_ADVISOR.SQLACCESS_ADVISOR, -
:task_name, :sql_stmt);
-- Section 17.2.26.1 Updating Task Attributes
---------------------------------------------
EXECUTE DBMS_ADVISOR.UPDATE_TASK_ATTRIBUTES('MYTASK', 'TUNING1');
-- The following example marks the task TUNING1 to read only
EXECUTE DBMS_ADVISOR.UPDATE_TASK_ATTRIBUTES('TUNING1', read_only => 'TRUE');
-- The following example marks the task MYTASK as a template.
EXECUTE DBMS_ADVISOR.UPDATE_TASK_ATTRIBUTES('TUNING1', is_template=>'TRUE');
-- Section 17.2.26.2 Deleting Tasks
-----------------------------------
-- need an existing task to delete, use TUNING1 instead of MYTASK
-- but first make it readable.
EXECUTE DBMS_ADVISOR.UPDATE_TASK_ATTRIBUTES('TUNING1', read_only => 'FALSE');
EXECUTE DBMS_ADVISOR.DELETE_TASK('TUNING1');
-------------------------------------------------------------------------
--Section 17.2.28 Examples of Using the SQLAccess Advisor
-------------------------------------------------------------------------
-- cleanup from previous example
EXECUTE DBMS_ADVISOR.DELETE_SQLWKLD('MYWORKLOAD');
-- Section 17.2.28.1 Recommendations From a User-Defined Workload
-----------------------------------------------------------------
-- The following example imports workload from a user-defined table,
-- SH.USER_WORKLOAD. It then creates a task called MYTASK, sets the
-- storage budget to 100 MB and runs the task. The recommendations
-- are printed out using a PL/SQL procedure. Finally, it generates a
-- script, which can be used to implement the recommendations.
-- Step 1 Prepare the USER_WORKLOAD table
-- The USER_WORKLOAD table is loaded with SQL statements as follows:
connect sh/sh;
-- aggregation with selection
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT t.week_ending_day, p.prod_subcategory,
SUM(s.amount_sold) AS dollars, s.channel_id, s.promo_id
FROM sales s, times t, products p WHERE s.time_id = t.time_id
AND s.prod_id = p.prod_id AND s.prod_id > 10 AND s.prod_id < 50
GROUP BY t.week_ending_day, p.prod_subcategory,
s.channel_id, s.promo_id')
/
-- aggregation with selection
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT t.calendar_month_desc, SUM(s.amount_sold) AS dollars
FROM sales s , times t
WHERE s.time_id = t.time_id
AND s.time_id between TO_DATE(''01-JAN-2000'', ''DD-MON-YYYY'')
AND TO_DATE(''01-JUL-2000'', ''DD-MON-YYYY'')
GROUP BY t.calendar_month_desc')
/
--Load all SQL queries.
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT ch.channel_class, c.cust_city, t.calendar_quarter_desc,
SUM(s.amount_sold) sales_amount
FROM sales s, times t, customers c, channels ch
WHERE s.time_id = t.time_id AND s.cust_id = c.cust_id
AND s.channel_id = ch.channel_id AND c.cust_state_province = ''CA''
AND ch.channel_desc IN (''Internet'',''Catalog'')
AND t.calendar_quarter_desc IN (''1999-Q1'',''1999-Q2'')
GROUP BY ch.channel_class, c.cust_city, t.calendar_quarter_desc')
/
-- order by
INSERT INTO user_workload (username, module, action, priority, sql_text)
VALUES ('SH', 'Example1', 'Action', 2,
'SELECT c.country_id, c.cust_city, c.cust_last_name
FROM customers c WHERE c.country_id in (''US'', ''UK'')
ORDER BY c.country_id, c.cust_city, c.cust_last_name')
/
COMMIT;
connect sh/sh;
set serveroutput on;
VARIABLE task_id NUMBER;
VARIABLE task_name VARCHAR2(255);
VARIABLE workload_name VARCHAR2(255);
VARIABLE saved_stmts NUMBER;
VARIABLE failed_stmts NUMBER;
EXECUTE :workload_name := 'MYWORKLOAD';
EXECUTE DBMS_ADVISOR.CREATE_SQLWKLD(:workload_name);
-- Step 3 Load the workload from user-defined table SH.USER_WORKLOAD
EXECUTE DBMS_ADVISOR.IMPORT_SQLWKLD_USER (:workload_name, 'APPEND', 'SH', -
'USER_WORKLOAD', :saved_stmts, :failed_stmts);
PRINT :saved_stmts;