Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort in PL/SQL with a Package 2

Status
Not open for further replies.

Sim

Programmer
Jul 25, 2000
5
FR
Thx but this was not the solution,<br>
<br>
my task is to create a package with the bubble sort and quick sort algorithm. The &quot;select...order by...&quot; clause, Ok thats easy :eek:)<br>
<br>
The Problem is, that I have to translate this two alogrithm from C++ into Oracle PL/SQL to stop the elapsed time for sort the random numbers. I must compare the time from C++ and PL/SQL.<br>
<br>
It's a little bit heavy!<br>
<br>
SiM
 
SiM -<br>
Like Mike, I see nothing wrong with your english. <br>
<br>
Catching the times is fairly easy in PL/SQL. <br>
In your procedure invocation, include an argument (p_elapsed OUT NUMBER).<br>
Declare two local variables, <br>
v_start DATE;<br>
v_stop DATE;<br>
<br>
At the beginning of your procedure, issue the following command:<br>
SELECT sysdate INTO v_start;<br>
<br>
Similarly, at the end of your routine issue:<br>
SELECT sysdate INTO v_start;<br>
<br>
Finally, set the value for your argument:<br>
<br>
p_elapsed := (v_stop - v_start)*86400;<br>
<br>
This will give you how many seconds the procedure took from start to finish.<br>
Unfortunately, Oracle won't let you track time to a higher granularity than a second.
 
Just realized my above entry is erroneous.<br>
It should read:<br>
<br>
At the beginning of your procedure, issue the following command:<br>
SELECT sysdate INTO v_start;<br>
<br>
Similarly, at the end of your routine issue:<br>
SELECT sysdate INTO v_stop;<br>
<br>
The original post would have you overwriting your start time and then trying to perform math with an unitialized variable.<br>
Sorry for the foul-up!<br>

 
SiM, you may need a from clause on your select, i.e.<br>
SELECT sysdate INTO v_stop FROM sys.dual;<br>
If you're getting problems with the algorithm consistently running too fast, e.g. 0 or 1 seconds, or similarly useless measures, try using a loop to invoke the algorithm 100 or 1000 times between your start and stop timings.<br>
You could also try putting your algorithm in a PL/SQL procedure and invoking it from SQL*Plus with timings on:<br>
<br>
SQL&gt; set timing on<br>
<br>
SQL&gt; execute my_proc;<br>
<br>
real: 5398<br>
<br>
SQL&gt;<br>
<br>
I think the timings this gives go to the millisecond level, though it's probably implementation specific, so check the SQL*Plus manual for your installation for what it says about set timing.
 
Yaffle -<br>
Thanks for catching the obvious oversight in the SELECT statements. I can't believe I missed that!<br>
<br>
According to Oracle, the number returned by setting timing on (in your example, real: 5398) does not correspond to an actual unit of time. It is a derived value that takes into account many factors based on resource usage. It is useful for benchmarking, but will not give you an actual time value. Also, Oracle does not track time beyond hundredths of a second.<br>
<br>
For timing purposes, probably your best bet is to use sql_trace.<br>
You might want to use a script file that turns trace and timed_statistics on and off. This will minimize the impact on your system:<br>
<br>
ALTER SESSION SET TIMED_STATISTICS=TRUE;<br>
ALTER SESSION SET SQL_TRACE=TRUE;<br>
EXECUTE sort_procedure;<br>
ALTER SESSION SET SQL_TRACE=FALSE;<br>
ALTER SESSION SET TIMED_STATISTICS=FALSE;<br>
<br>
After running this script, you will use tkprof to reformat your trace file.<br>
<br>
Here is a copy of such a session (in this case, the procedure name is 'quack'):<br>
<br>
<br>
SQL&gt; set timing off<br>
SQL&gt; alter session set timed_statistics=true;<br>
SQL&gt; alter session set sql_trace=true;<br>
SQL&gt; execute quack;<br>
SQL&gt; alter session set sql_trace=false;<br>
SQL&gt; alter session set timed_statistics=false;<br>
<br>
This generated a trace file called ora00283.trc.<br>
<br>
Running tkprof:<br>
<br>
C:\&gt;tkprof ora00283.trc c:\output.txt sys=NO<br>
<br>
This will read the trace file and put formatted information into an output file called output.txt.<br>
<br>
Looking at this file:<br>
<br>
Trace file: ora00283.trc<br>
Sort options: default<br>
<br>
********************************************************************************<br>
count = number of times OCI procedure was executed<br>
cpu = cpu time in seconds executing<br>
elapsed = elapsed time in seconds executing<br>
disk = number of physical reads of buffers from disk<br>
query = number of buffers gotten for consistent read<br>
current = number of buffers gotten in current mode (usually for update)<br>
rows = number of rows processed by the fetch or execute call<br>
********************************************************************************<br>
.<br>
.<br>
.<br>
BEGIN quack; END;<br>
<br>
<br>
call count cpu elapsed disk query current rows<br>
------- ------ ------- - ---------- ---------- ---------- ---------- ----------<br>
Parse 2 0.04 0.04 0 0 0 0<br>
Execute 2 0.05 0.05 0 0 0 2<br>
Fetch 0 0.00 0.00 0 0 0 0<br>
------- ------ -------- ---------- ---------- ---------- ---------- ----------<br>
total 4 0.09 0.09 0 0 0 2<br>
<br>
Misses in library cache during parse: 0<br>
Optimizer goal: CHOOSE<br>
Parsing user id: 25<br>
********************************************************************************<br>
<br>
We see this procedure took .09 seconds to complete. However, notice that almost half of this time (.04 seconds) was spent parsing. A second run would probably show a lower overall time, since the code would now be in the library cache and parsing would not be necessary. It's not a bad idea to run several executions and then take the average time. This helps minimize the effects of fluctuations in resource usage, data location, etc.<br>
<br>
I think this is about as good as it gets for timing purposes.<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top