SQL Trace Data (Paper)
SQL Trace Data (Paper)
Cary
Millsap
Method
R
Corporation,
Southlake,
Texas,
USA
http://twitter.com/CaryMillsap
[email protected]
Revised
2011/01/13
The
most
recent
update
of
this
paper
is
available
free
of
obligation
at
http://method-‐r.com/downloads.
Oracle's
extended
SQL
trace
data
stream
contains
a
linear
sequential
record
of
every
database
call
and
every
operating
system
call
that
the
Oracle
kernel
executes
in
response
to
the
code
that
you
write.
The
trace
Iile
contains
timings
that
enable
you
to
precisely
measure
how
long
your
code
takes
to
run
and
why.
The
detailed
information
about
individual
database
calls
and
operating
system
calls
is
vital
to
building
scalable
applications
(as
well
as
troubleshooting
them
in
production).
The
information
in
there
is
a
gold
mine,
and
some
of
it
is
available
nowhere
else.
1 Oracle extended SQL trace is useful for the same kinds of reasons that the Unix strace tool is useful. My description here is
2 An Oracle session is an entity with a unique sid-‐serial# combination within an Oracle instance, which you can see in v$session.
3 An Oracle kernel process is an operating system process (which you can inspect with, for example, a Linux command like ps, lsof,
or strace) that executes the instructions required by an Oracle session.
enable
procedure. This
code
will
enable
tracing
for
the
session
that’s
logging
on.
There’s
no
need
for
a
corresponding
When
you
use
dbms_monitor,
I
recommend
that
you
before
logoff
trace
disablement,
by
the
way,
use
the
default
value
of
waits=>true.
If
you
use
the
because
logging
off
automatically
disables
the
value
waits=>false,
you’ll
be
using
SQL
trace,
but
not
trace.
extended
SQL
trace.
You’ll
get
a
trace
of
the
Oracle
database
calls
your
application
makes,
but
not
the
OS
sys_context
calls
that
Oracle
makes.
Without
a
record
of
those
OS
The
sys_context
function
gives
you
dozens
of
hooks
calls
in
your
trace
aile,
you’ll
not
be
able
to
account
for
you
can
use
inside
of
a
trigger
body
to
identify
the
potentially
signiaicant
amounts
of
response
time. session
that’s
logging
in.
For
example,
sys_context('USERENV','IP_ADDRESS')
I
also
recommend
that
you
use
the
non-‐default
value
binds=>true.
Setting
binds=>true
causes
the
Oracle
…returns
the
IP
address
of
the
machine
from
Database
to
emit
the
values
that
the
application
bound
which
the
client
is
connected.
With
sys_context,
to
its
placeholder
variables
into
the
trace
data
stream.
you
can
write
PL/SQL
code
in
your
after
logon
For
example,
if
someone
has
executed
this
SQL
trigger
body
that
can
decide
to
trace
only
logins
statement: originating
from
a
speciaic
IP
address,
only
logins
requested
by
certain
client
OS
users,
or
logins
update salary set amount = :v1 where id = :v2
matching
any
of
the
other
criteria
that
sys_context
…then
your
trace
data
will
show
what
values
were
makes
available
to
you.
assigned
to
v1
and
v2.
This
is
invaluable
information
Running
suspicious
SQL
in
your
own
session
for
creating
reproducible
test
cases
for
solving
There
are
lots
of
ways
you
can
identify
suspicious
problems.
However,
be
aware
of
the
data
security
SQL
statements:
you’ll
aind
them
in
OEM,
in
your
implication
of
emitting
bind
data
into
a
trace
aile;
AWR
or
ADDM
reports,
or
by
querying
v$sqlstat.
when
you
do
it,
you
are
copying
potentially
sensitive
Copy
them
and
run
them
in
SQL*Plus
or
Oracle
data
from
your
database
to
your
aile
system. SQL
Developer.
You
can
plug
in
whatever
bind
variable
values
you
need
and
trace
to
your
heart’s
5 SESSION
TRACING content.
One
of
the
nice
things
about
Oracle
tracing
is
that
dbms_monitor.database_trace_enable
you’re
not
required
to
change
an
application
in
order
If
all
else
fails,
there’s
always
the
nuclear
bomb
to
use
it.
Even
if
the
application
you’re
using
is
option
of
tracing
everything
that
happens
over
completely
un-‐modiaiable
and
was
in
no
way
designed
some
time
interval
you
choose.
Beware,
however:
to
cooperate
with
tracing,
you
still
have
the
following
enabling
trace
for
a
whole
instance
or
database
very
powerful
tools
at
your
disposal: will
only
activate
tracing
for
new
sessions
that
log
in
after
you
enable
the
trace.
It
won’t
enable
dbms_monitor.session_trace_enable
tracing
for
sessions
that
are
already
logged
in.
With
session_trace_enable,
you
can
trace
any
session
on
your
system
if
you
know
its
session
id
and
serial
number.
You
just
have
to
be
meticulous
6 END-‐TO-‐END
TRACING
to
enable
the
trace
immediately
before
the
user
There
are
three
major
milestones
in
the
history
of
issues
the
slow
request,
and
then
disable
the
trace
tracing
in
the
Oracle
Database:
immediately
after
the
slow
request
is
fulailled.
4 That’s right, plus or minus. Sometimes a program will run faster when you trace it. It sounds nice, but it’s usually not what you
want.
5 Barry Boehm, 1981, Software Engineering Economics, Englewood Cliffs NJ: P T R Prentice Hall, p40.
6 Call durations in trace ailes are expressed in microseconds, abbreviated µs. 1 µs = .000001 seconds, or 1/1,000,000th of a second.
7 You can see another trace aile guided tour in “For Developers: Making Friends with the Oracle Database for Fast, Scalable
Applications.”
Yes,
Oracle
measured
the
call
as
having
consumed
Here’s
what
the
aields
mean:
[far]
more
CPU
time
than
the
call’s
actual
elapsed
WAIT
duration.8
This
call
is
an
OS
call,
not
a
database
call.10
e=1734
#12397272
This
call
consumed
1,734
±1
µs
of
elapsed
time.
The
call’s
cursor
handle
id
is
12397272.
8 For a detailed description of why the CPU consumption statistic is accurate to only ±10,000 µs (except on Solaris machines on
which
microstate
accounting
is
enabled),
see
Optimizing
Oracle
Performance,
by
Cary
Millsap
and
Jeff
Holt
(O’Reilly
2003),
pp161–
165.
9 They’re most likely not to be consistent within a given aile on Microsoft Windows systems, where tim values wrap (that is, get as
large as they can go and then start over at 0) more frequently than on other systems.
10 I’ve had good fun explaining why the word wait is the wrong word to use in this context. See for example my essay, “Dang it,
11 See also patches 9415425 for Oracle 11.1.0.7.1 on Solaris and 9941786 for Oracle 11.1.0.7.4 on Solaris.