Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
967 lines (800 loc) · 26.1 KB

File metadata and controls

967 lines (800 loc) · 26.1 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
CREATE OR REPLACE PACKAGE TEST_SELECT AS
/*
collect_select_statements(max_number_selects,
include_pattern1,...,include_pattern10,
exclude_pattern1,...,exclude_pattern10) - This proc is run on the
the source database to collect select statements including statements that have
the include patterns and excluding those whohave the exclude patterns.
Patterns use LIKE conditions %x%.
*/
procedure collect_select_statements(
max_number_selects in number,
include_pattern1 in varchar2 := '%%',
include_pattern2 in varchar2 := '%%',
include_pattern3 in varchar2 := '%%',
include_pattern4 in varchar2 := '%%',
include_pattern5 in varchar2 := '%%',
include_pattern6 in varchar2 := '%%',
include_pattern7 in varchar2 := '%%',
include_pattern8 in varchar2 := '%%',
include_pattern9 in varchar2 := '%%',
include_pattern10 in varchar2 := '%%',
exclude_pattern1 in varchar2 := ' ',
exclude_pattern2 in varchar2 := ' ',
exclude_pattern3 in varchar2 := ' ',
exclude_pattern4 in varchar2 := ' ',
exclude_pattern5 in varchar2 := ' ',
exclude_pattern6 in varchar2 := ' ',
exclude_pattern7 in varchar2 := ' ',
exclude_pattern8 in varchar2 := ' ',
exclude_pattern9 in varchar2 := ' ',
exclude_pattern10 in varchar2 := ' ');
/*
copy_select_statements(link_name)
copies select statements from remote source database
pointed to by link_name's db link.
*/
procedure copy_select_statements(
link_name in varchar2);
/*
update_select_statements(from_text,to_text) - updates select statement text.
Used to change table names and schema names. I.e. from prodschema.prodtablename
to testschema.testtablename.
*/
procedure update_select_statements(
from_text in varchar2,
to_text in varchar2);
/*
get_explain_plans(test_name) - runs explain plan against every select recording the
current test name. I.e. get_explain_plans('production stats'). Plans are stored in
plan_table by sqlnumber.
*/
procedure get_explain_plans(
test_name in varchar2);
/*
execute_all(test_name) - execute every query for the current test scenario.
*/
procedure execute_all(
test_name in varchar2);
/*
execute_diff_plans(test_name,compared_to_test_name) - execute only the queries
whose plan under the current test_name differ from previous test name
compared_to_test_name. So, if test_name is "Production stats" and
compared_to_test_name is "Empty stats" then we assume you have run get_explain_plans
for both "Production stats" and "Empty stats" and only run the sqls whose plans
are different in the two scenarios.
*/
procedure execute_diff_plans(
v_test_name in varchar2,
compared_to_test_name in varchar2);
/*
display_results(test_name,compared_to_test_name) - output results of testings in the
two scenarios. List results from all queries that ran more than 3 times as long
with one test or the other. Also summarize results with total elapsed time,
number of queries executed, average elapsed time, and percent improvement.
*/
procedure display_results(
v_test_name in varchar2,
compared_to_test_name in varchar2);
/*
show_explained_plan(test_name,sqlnumber) - extract plan from plan_table for given test name
and sql statement.
*/
function show_explained_plan(
v_test_name in varchar2,
sqlnumber in number) return CLOB;
/*
reexecute_errored(v_test_name) - re-execute every query for the current test scenario
that had an error.
*/
procedure reexecute_errored(
v_test_name in varchar2);
/*
show_executed_plan(test_name,sqlnumber) - extract plan from plan_table for given test name
and sql statement.
*/
procedure show_executed_plan(
v_test_name in varchar2,
v_sqlnumber in number);
/*
execute_one(test_name,sqlnumber) - rerun one sql statement
*/
procedure execute_one(
v_test_name in varchar2,
v_sqlnumber in number);
END;
/
show errors
CREATE OR REPLACE PACKAGE BODY TEST_SELECT
AS
procedure collect_select_statements(
max_number_selects in number,
include_pattern1 in varchar2 := '%%',
include_pattern2 in varchar2 := '%%',
include_pattern3 in varchar2 := '%%',
include_pattern4 in varchar2 := '%%',
include_pattern5 in varchar2 := '%%',
include_pattern6 in varchar2 := '%%',
include_pattern7 in varchar2 := '%%',
include_pattern8 in varchar2 := '%%',
include_pattern9 in varchar2 := '%%',
include_pattern10 in varchar2 := '%%',
exclude_pattern1 in varchar2 := ' ',
exclude_pattern2 in varchar2 := ' ',
exclude_pattern3 in varchar2 := ' ',
exclude_pattern4 in varchar2 := ' ',
exclude_pattern5 in varchar2 := ' ',
exclude_pattern6 in varchar2 := ' ',
exclude_pattern7 in varchar2 := ' ',
exclude_pattern8 in varchar2 := ' ',
exclude_pattern9 in varchar2 := ' ',
exclude_pattern10 in varchar2 := ' ')
is
CURSOR SQL_CURSOR IS
SELECT DISTINCT
SQL_ID,
DBID
FROM DBA_HIST_SQLSTAT;
SQL_REC SQL_CURSOR%ROWTYPE;
CURSOR TEXT_CURSOR(SQL_ID_ARGUMENT VARCHAR2,DBID_ARGUMENT NUMBER) IS
SELECT
SQL_TEXT
FROM DBA_HIST_SQLTEXT
WHERE
SQL_TEXT like include_pattern1 and
SQL_TEXT like include_pattern2 and
SQL_TEXT like include_pattern3 and
SQL_TEXT like include_pattern4 and
SQL_TEXT like include_pattern5 and
SQL_TEXT like include_pattern6 and
SQL_TEXT like include_pattern7 and
SQL_TEXT like include_pattern8 and
SQL_TEXT like include_pattern9 and
SQL_TEXT like include_pattern10 and
SQL_TEXT not like exclude_pattern1 and
SQL_TEXT not like exclude_pattern2 and
SQL_TEXT not like exclude_pattern3 and
SQL_TEXT not like exclude_pattern4 and
SQL_TEXT not like exclude_pattern5 and
SQL_TEXT not like exclude_pattern6 and
SQL_TEXT not like exclude_pattern7 and
SQL_TEXT not like exclude_pattern8 and
SQL_TEXT not like exclude_pattern9 and
SQL_TEXT not like exclude_pattern10 and
(upper(SQL_TEXT) like 'SELECT%' or
upper(SQL_TEXT) like 'WITH%') and
SQL_ID = SQL_ID_ARGUMENT and
DBID = DBID_ARGUMENT;
TEXT_REC TEXT_CURSOR%ROWTYPE;
sqlnumber number;
tempclob clob;
next_sqlnumber number;
begin
-- get next sql number based on number of queries already collected
select count(*)+1 into next_sqlnumber from select_statements;
sqlnumber := next_sqlnumber ;
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
OPEN TEXT_CURSOR(SQL_REC.SQL_ID,SQL_REC.DBID);
LOOP
FETCH TEXT_CURSOR INTO TEXT_REC;
EXIT WHEN TEXT_CURSOR%NOTFOUND;
insert into select_statements values (sqlnumber,TEXT_REC.SQL_TEXT);
commit;
sqlnumber := sqlnumber + 1;
END LOOP;
CLOSE TEXT_CURSOR;
EXIT WHEN sqlnumber >= (max_number_selects+next_sqlnumber);
END LOOP;
CLOSE SQL_CURSOR;
end collect_select_statements;
procedure copy_select_statements(
link_name in varchar2)
is
begin
execute immediate 'truncate table select_statements';
execute immediate 'insert into select_statements select * from '||
'select_statements@'||link_name;
commit;
end copy_select_statements;
procedure update_error(
v_test_name in varchar2,
v_sqlnumber in number,
v_sqlerrm in varchar2)
is
row_cnt number;
trimmed_errm varchar2(64);
begin
trimmed_errm := substr(v_sqlerrm,1,64);
select count(*) into row_cnt
from test_results t
where
t.test_name=v_test_name and
t.sqlnumber=v_sqlnumber;
if row_cnt > 0 then
update test_results t
set
error_message=trimmed_errm
where
t.test_name=v_test_name and
t.sqlnumber=v_sqlnumber;
else
insert into test_results
(test_name,sqlnumber,error_message)
values (
v_test_name,
v_sqlnumber,
trimmed_errm);
end if;
commit;
end update_error;
procedure update_select_statements(
from_text in varchar2,
to_text in varchar2)
is
begin
update select_statements
set SQL_TEXT = replace(SQL_TEXT,from_text,to_text);
commit;
end update_select_statements;
procedure update_explain_plan_hash(
v_test_name in varchar2,
v_sqlnumber in number)
is
planoutput clob;
plan_hash_value number;
row_cnt number;
begin
select t.PLAN_TABLE_OUTPUT
into planoutput
from table(dbms_xplan.display('PLAN_TABLE',v_test_name||to_char(v_sqlnumber),'BASIC')) t
where t.plan_table_output like 'Plan%';
plan_hash_value:=to_number(substr(planoutput,18));
select count(*) into row_cnt
from test_results t
where
t.test_name=v_test_name and
t.sqlnumber=v_sqlnumber;
if row_cnt > 0 then
update test_results t
set explain_plan_hash=plan_hash_value
where
t.test_name=v_test_name and
t.sqlnumber=v_sqlnumber;
else
insert into test_results
(test_name,sqlnumber,explain_plan_hash)
values (
v_test_name,
v_sqlnumber,
plan_hash_value);
end if;
commit;
end update_explain_plan_hash;
procedure get_explain_plans(
test_name in varchar2)
is
CURSOR SQL_CURSOR IS
SELECT
sqlnumber,
sql_text
FROM select_statements
ORDER by sqlnumber;
SQL_REC SQL_CURSOR%ROWTYPE;
clob_cursor INTEGER;
sqlclob clob;
ignored_value INTEGER;
begin
execute immediate 'delete from plan_table where statement_id like '''||test_name||'%''';
commit;
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
sqlclob := 'explain plan set statement_id = '''||test_name||SQL_REC.sqlnumber||''' into plan_table for '||
SQL_REC.sql_text;
begin
clob_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE (clob_cursor,sqlclob,DBMS_SQL.NATIVE);
ignored_value := DBMS_SQL.EXECUTE(clob_cursor);
DBMS_SQL.CLOSE_CURSOR (clob_cursor);
update_explain_plan_hash(test_name,SQL_REC.sqlnumber);
DBMS_OUTPUT.put_line('Plan explained for SQL number '||SQL_REC.sqlnumber);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.put_line('Error on SQL number '||SQL_REC.sqlnumber);
DBMS_OUTPUT.put_line(SQLERRM);
update_error(test_name,SQL_REC.sqlnumber,SQLERRM);
end;
commit;
END LOOP;
CLOSE SQL_CURSOR;
end get_explain_plans;
procedure runselect(
v_test_name in varchar2,
v_sqlnumber in number,
sqlclob in clob) is
clob_cursor INTEGER;
rows_fetched INTEGER;
before_date date;
after_date date;
total_rows_fetched NUMBER;
elapsed_time_seconds NUMBER;
row_cnt number;
query_sql_id varchar2(13);
planoutput clob;
plan_hash_value number;
cursor_child_no number;
b_CPU_used_by_this_session number;
b_consistent_gets number;
b_db_block_gets number;
b_parse_time_elapsed number;
b_physical_reads number;
a_CPU_used_by_this_session number;
a_consistent_gets number;
a_db_block_gets number;
a_parse_time_elapsed number;
a_physical_reads number;
BEGIN
select sysdate into before_date from dual;
-- record current values of session statistics
select
s1.value CPU_used_by_this_session,
s2.value consistent_gets,
s3.value db_block_gets,
s4.value parse_time_elapsed,
s5.value physical_reads
into
b_CPU_used_by_this_session,
b_consistent_gets,
b_db_block_gets,
b_parse_time_elapsed,
b_physical_reads
from
v$mystat s1,
v$mystat s2,
v$mystat s3,
v$mystat s4,
v$mystat s5,
V$STATNAME n1,
V$STATNAME n2,
V$STATNAME n3,
V$STATNAME n4,
V$STATNAME n5
where
s1.STATISTIC#=n1.STATISTIC# and
n1.name = 'CPU used by this session' and
s2.STATISTIC#=n2.STATISTIC# and
n2.name = 'consistent gets' and
s3.STATISTIC#=n3.STATISTIC# and
n3.name = 'db block gets' and
s4.STATISTIC#=n4.STATISTIC# and
n4.name = 'parse time elapsed' and
s5.STATISTIC#=n5.STATISTIC# and
n5.name = 'physical reads';
clob_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE (clob_cursor,sqlclob,DBMS_SQL.NATIVE);
rows_fetched := DBMS_SQL.EXECUTE_AND_FETCH (clob_cursor);
total_rows_fetched := rows_fetched;
LOOP
EXIT WHEN rows_fetched < 1;
rows_fetched := DBMS_SQL.FETCH_ROWS (clob_cursor);
total_rows_fetched := total_rows_fetched + rows_fetched;
END LOOP;
DBMS_SQL.CLOSE_CURSOR (clob_cursor);
SELECT prev_sql_id,PREV_CHILD_NUMBER
into query_sql_id,cursor_child_no
from v$session
where audsid=USERENV('SESSIONID');
select t.PLAN_TABLE_OUTPUT
into planoutput
from table(dbms_xplan.display_cursor(query_sql_id,cursor_child_no,'BASIC')) t
where t.plan_table_output like 'Plan%';
plan_hash_value:=to_number(substr(planoutput,18));
-- record current values of session statistics
select
s1.value CPU_used_by_this_session,
s2.value consistent_gets,
s3.value db_block_gets,
s4.value parse_time_elapsed,
s5.value physical_reads
into
a_CPU_used_by_this_session,
a_consistent_gets,
a_db_block_gets,
a_parse_time_elapsed,
a_physical_reads
from
v$mystat s1,
v$mystat s2,
v$mystat s3,
v$mystat s4,
v$mystat s5,
V$STATNAME n1,
V$STATNAME n2,
V$STATNAME n3,
V$STATNAME n4,
V$STATNAME n5
where
s1.STATISTIC#=n1.STATISTIC# and
n1.name = 'CPU used by this session' and
s2.STATISTIC#=n2.STATISTIC# and
n2.name = 'consistent gets' and
s3.STATISTIC#=n3.STATISTIC# and
n3.name = 'db block gets' and
s4.STATISTIC#=n4.STATISTIC# and
n4.name = 'parse time elapsed' and
s5.STATISTIC#=n5.STATISTIC# and
n5.name = 'physical reads';
select sysdate into after_date from dual;
elapsed_time_seconds := (after_date-before_date)*24*3600;
select count(*) into row_cnt
from test_results t
where
t.test_name=v_test_name and
t.sqlnumber=v_sqlnumber;
if row_cnt > 0 then
update test_results t
set
rows_fetched=total_rows_fetched,
elapsed_in_seconds=elapsed_time_seconds,
sql_id=query_sql_id,
execute_plan_hash=plan_hash_value,
CPU_used_by_this_session=a_CPU_used_by_this_session-b_CPU_used_by_this_session,
consistent_gets=a_consistent_gets-b_consistent_gets,
db_block_gets=a_db_block_gets-b_db_block_gets,
parse_time_elapsed=a_parse_time_elapsed-b_parse_time_elapsed,
physical_reads=a_physical_reads-b_physical_reads
where
t.test_name=v_test_name and
t.sqlnumber=v_sqlnumber;
else
insert into test_results
(test_name,sqlnumber,rows_fetched,elapsed_in_seconds,sql_id,execute_plan_hash,
CPU_used_by_this_session,consistent_gets,db_block_gets,parse_time_elapsed,
physical_reads)
values (
v_test_name,
v_sqlnumber,
total_rows_fetched,
elapsed_time_seconds,
query_sql_id,
plan_hash_value,
a_CPU_used_by_this_session-b_CPU_used_by_this_session,
a_consistent_gets-b_consistent_gets,
a_db_block_gets-b_db_block_gets,
a_parse_time_elapsed-b_parse_time_elapsed,
a_physical_reads-b_physical_reads);
end if;
commit;
END runselect;
procedure execute_all(
test_name in varchar2)
is
CURSOR SQL_CURSOR IS
SELECT
sqlnumber,
sql_text
FROM select_statements
ORDER by sqlnumber;
SQL_REC SQL_CURSOR%ROWTYPE;
begin
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
begin
runselect(test_name,
SQL_REC.sqlnumber,
SQL_REC.sql_text);
DBMS_OUTPUT.put_line('Executed SQL number '||SQL_REC.sqlnumber);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.put_line('Error on SQL number '||SQL_REC.sqlnumber);
DBMS_OUTPUT.put_line(SQLERRM);
update_error(test_name,SQL_REC.sqlnumber,SQLERRM);
end;
commit;
END LOOP;
CLOSE SQL_CURSOR;
end execute_all;
procedure execute_diff_plans(
v_test_name in varchar2,
compared_to_test_name in varchar2)
is
CURSOR SQL_CURSOR IS
SELECT
sqlnumber,
sql_text
FROM select_statements
ORDER by sqlnumber;
SQL_REC SQL_CURSOR%ROWTYPE;
row_cnt number;
begin
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
select count(*) into row_cnt
from test_results t1,test_results t2
where
t1.explain_plan_hash <> t2.explain_plan_hash and
t1.test_name=v_test_name and
t2.test_name=compared_to_test_name and
t1.sqlnumber=SQL_REC.sqlnumber and
t1.sqlnumber=t2.sqlnumber;
if row_cnt > 0 then
begin
runselect(v_test_name,
SQL_REC.sqlnumber,
SQL_REC.sql_text);
DBMS_OUTPUT.put_line('Executed SQL number '||SQL_REC.sqlnumber);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.put_line('Error on SQL number '||SQL_REC.sqlnumber);
DBMS_OUTPUT.put_line(SQLERRM);
update_error(v_test_name,SQL_REC.sqlnumber,SQLERRM);
end;
commit;
end if;
END LOOP;
CLOSE SQL_CURSOR;
end execute_diff_plans;
procedure display_results(
v_test_name in varchar2,
compared_to_test_name in varchar2)
is
CURSOR RSLT_CURSOR(TEST1 varchar2,TEST2 varchar2) IS
select
t1.SQLNUMBER,
t1.SQL_ID,
t1.EXPLAIN_PLAN_HASH T1_EXPLAIN_PLAN_HASH,
t1.EXECUTE_PLAN_HASH T1_EXECUTE_PLAN_HASH,
t2.EXPLAIN_PLAN_HASH T2_EXPLAIN_PLAN_HASH,
t2.EXECUTE_PLAN_HASH T2_EXECUTE_PLAN_HASH,
t1.ELAPSED_IN_SECONDS T1_ELAPSED_IN_SECONDS,
t2.ELAPSED_IN_SECONDS T2_ELAPSED_IN_SECONDS
from
test_results t1,
test_results t2
where
t1.SQLNUMBER=t2.SQLNUMBER and
t1.TEST_NAME=TEST1 and
t2.TEST_NAME=TEST2 and
(t1.ELAPSED_IN_SECONDS*3) < t2.ELAPSED_IN_SECONDS and
t1.ELAPSED_IN_SECONDS is not null and
t2.ELAPSED_IN_SECONDS is not null
order by sqlnumber;
RSLT_REC RSLT_CURSOR%ROWTYPE;
row_cnt number;
t1_elapsed number;
t1_count number;
t1_average number;
t2_elapsed number;
t2_count number;
t2_average number;
begin
-- output selects where test1 3 times better than test2
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('Select statements that ran 3 times faster with '||
v_test_name||' than with '||compared_to_test_name||'.');
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('T1='||v_test_name);
DBMS_OUTPUT.PUT_LINE('T2='||compared_to_test_name);
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE(CHR(9)||'SQLNUMBER T1_EXECUTE_PLAN_HASH T2_EXECUTE_PLAN_HASH T1_ELAPSED_IN_SECONDS T2_ELAPSED_IN_SECONDS');
DBMS_OUTPUT.PUT_LINE(CHR(9)||'--------- -------------------- -------------------- --------------------- ---------------------');
row_cnt := 0;
OPEN RSLT_CURSOR(v_test_name,compared_to_test_name);
LOOP
FETCH RSLT_CURSOR INTO RSLT_REC;
EXIT WHEN RSLT_CURSOR%NOTFOUND;
row_cnt := row_cnt + 1;
DBMS_OUTPUT.PUT_LINE(CHR(9)||
lpad(to_char(RSLT_REC.SQLNUMBER),9)||' '||
lpad(to_char(RSLT_REC.T1_EXECUTE_PLAN_HASH),20)||' '||
lpad(to_char(RSLT_REC.T2_EXECUTE_PLAN_HASH),20)||' '||
lpad(TO_CHAR(trunc(RSLT_REC.T1_ELAPSED_IN_SECONDS)),21)||' '||
lpad(TO_CHAR(trunc(RSLT_REC.T2_ELAPSED_IN_SECONDS)),21));
END LOOP;
CLOSE RSLT_CURSOR;
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('Number of selects='||to_char(row_cnt));
-- output selects where test2 3 times better than test1
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('Select statements that ran 3 times faster with '||
compared_to_test_name||' than with '||v_test_name||'.');
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('T1='||compared_to_test_name);
DBMS_OUTPUT.PUT_LINE('T2='||v_test_name);
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE(CHR(9)||'SQLNUMBER T1_EXECUTE_PLAN_HASH T2_EXECUTE_PLAN_HASH T1_ELAPSED_IN_SECONDS T2_ELAPSED_IN_SECONDS');
DBMS_OUTPUT.PUT_LINE(CHR(9)||'--------- -------------------- -------------------- --------------------- ---------------------');
row_cnt := 0;
OPEN RSLT_CURSOR(compared_to_test_name,v_test_name);
LOOP
FETCH RSLT_CURSOR INTO RSLT_REC;
EXIT WHEN RSLT_CURSOR%NOTFOUND;
row_cnt := row_cnt + 1;
DBMS_OUTPUT.PUT_LINE(CHR(9)||
lpad(to_char(RSLT_REC.SQLNUMBER),9)||' '||
lpad(to_char(RSLT_REC.T1_EXECUTE_PLAN_HASH),20)||' '||
lpad(to_char(RSLT_REC.T2_EXECUTE_PLAN_HASH),20)||' '||
lpad(TO_CHAR(trunc(RSLT_REC.T1_ELAPSED_IN_SECONDS)),21)||' '||
lpad(TO_CHAR(trunc(RSLT_REC.T2_ELAPSED_IN_SECONDS)),21));
END LOOP;
CLOSE RSLT_CURSOR;
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('Number of selects='||to_char(row_cnt));
-- summary results
select
sum(t1.ELAPSED_IN_SECONDS),
count(*),
sum(t1.ELAPSED_IN_SECONDS)/count(*),
sum(t2.ELAPSED_IN_SECONDS),
sum(t2.ELAPSED_IN_SECONDS)/count(*)
into
t1_elapsed,
t1_count,
t1_average,
t2_elapsed,
t2_average
from
test_results t1,
test_results t2
where
t1.SQLNUMBER=t2.SQLNUMBER and
t1.TEST_NAME=v_test_name and
t2.TEST_NAME=compared_to_test_name and
t1.ELAPSED_IN_SECONDS is not null and
t2.ELAPSED_IN_SECONDS is not null;
t2_count := t1_count;
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE('Summary of test results');
DBMS_OUTPUT.PUT_LINE(CHR(9));
DBMS_OUTPUT.PUT_LINE(CHR(9)||' TEST_NAME TOTAL_ELAPSED_IN_SECONDS SELECTS_EXECUTED AVERAGE_ELAPSED_IN_SECONDS');
DBMS_OUTPUT.PUT_LINE(CHR(9)||'-------------------- ------------------------ ---------------- --------------------------');
DBMS_OUTPUT.PUT_LINE(CHR(9)||
lpad(v_test_name,20)||' '||
lpad(to_char(t1_elapsed),24)||' '||
lpad(to_char(t1_count),16)||' '||
lpad(TO_CHAR(trunc(t1_average)),26));
DBMS_OUTPUT.PUT_LINE(CHR(9)||
lpad(compared_to_test_name,20)||' '||
lpad(to_char(t2_elapsed),24)||' '||
lpad(to_char(t2_count),16)||' '||
lpad(TO_CHAR(trunc(t2_average)),26));
end display_results;
function show_explained_plan(
v_test_name in varchar2,
sqlnumber in number) return CLOB
is
begin
return dbms_xplan.display_plan('PLAN_TABLE',v_test_name||to_char(sqlnumber),'ALL');
end show_explained_plan;
procedure reexecute_errored(
v_test_name in varchar2)
is
CURSOR SQL_CURSOR IS
SELECT
s.sqlnumber,
s.sql_text
FROM
select_statements s,
test_results t
WHERE
s.sqlnumber=t.sqlnumber and
t.error_message is not null and
t.test_name = v_test_name
ORDER by sqlnumber;
SQL_REC SQL_CURSOR%ROWTYPE;
begin
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
update test_results t
set t.error_message=NULL
where
t.sqlnumber=SQL_REC.sqlnumber and
t.test_name=v_test_name;
commit;
begin
runselect(v_test_name,
SQL_REC.sqlnumber,
SQL_REC.sql_text);
DBMS_OUTPUT.put_line('Executed SQL number '||SQL_REC.sqlnumber);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.put_line('Error on SQL number '||SQL_REC.sqlnumber);
DBMS_OUTPUT.put_line(SQLERRM);
update_error(v_test_name,SQL_REC.sqlnumber,SQLERRM);
end;
commit;
END LOOP;
CLOSE SQL_CURSOR;
end reexecute_errored;
procedure show_executed_plan(
v_test_name in varchar2,
v_sqlnumber in number)
is
CURSOR SQL_CURSOR IS
select
SQL_ID,
EXECUTE_PLAN_HASH
from
test_results
where
sqlnumber=v_sqlnumber and
TEST_NAME=v_test_name;
SQL_REC SQL_CURSOR%ROWTYPE;
CURSOR SQL_CURSOR2(p_sql_id varchar2,p_plan_hash number)
IS
select PLAN_TABLE_OUTPUT from
table(DBMS_XPLAN.DISPLAY_AWR(p_sql_id,
p_plan_hash,
NULL,
'ALL'));
SQL_REC2 SQL_CURSOR2%ROWTYPE;
begin
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
OPEN SQL_CURSOR2(SQL_REC.SQL_ID,SQL_REC.EXECUTE_PLAN_HASH);
LOOP
FETCH SQL_CURSOR2 INTO SQL_REC2;
EXIT WHEN SQL_CURSOR2%NOTFOUND;
DBMS_OUTPUT.put_line(SQL_REC2.PLAN_TABLE_OUTPUT);
END LOOP;
END LOOP;
CLOSE SQL_CURSOR;
end show_executed_plan;
procedure execute_one(
v_test_name in varchar2,
v_sqlnumber in number)
is
CURSOR SQL_CURSOR IS
SELECT
s.sqlnumber,
s.sql_text
FROM
select_statements s,
test_results t
WHERE
s.sqlnumber=t.sqlnumber and
s.sqlnumber = v_sqlnumber and
t.test_name = v_test_name
ORDER by sqlnumber;
SQL_REC SQL_CURSOR%ROWTYPE;
begin
OPEN SQL_CURSOR;
LOOP
FETCH SQL_CURSOR INTO SQL_REC;
EXIT WHEN SQL_CURSOR%NOTFOUND;
update test_results t
set t.error_message=NULL
where
t.sqlnumber=SQL_REC.sqlnumber and
t.test_name=v_test_name;
commit;
begin
runselect(v_test_name,
SQL_REC.sqlnumber,
SQL_REC.sql_text);
DBMS_OUTPUT.put_line('Executed SQL number '||SQL_REC.sqlnumber);
EXCEPTION
WHEN others THEN
DBMS_OUTPUT.put_line('Error on SQL number '||SQL_REC.sqlnumber);
DBMS_OUTPUT.put_line(SQLERRM);
update_error(v_test_name,SQL_REC.sqlnumber,SQLERRM);
end;
commit;
END LOOP;
CLOSE SQL_CURSOR;
end execute_one;
end TEST_SELECT;
/
show errors
Morty Proxy This is a proxified and sanitized view of the page, visit original site.