Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,400 --> 00:00:05,870
In this lesson, we're going to
take a look at execution plans,
2
00:00:05,870 --> 00:00:09,890
SQL execution plans, and
the explain plan tool
3
00:00:09,890 --> 00:00:13,610
or command that we
can use to determine
4
00:00:13,610 --> 00:00:17,160
how Oracle is going to
execute a given SQL statement.
5
00:00:22,310 --> 00:00:24,880
And let's put in a
simple SQL statement.
6
00:00:29,970 --> 00:00:32,960
So we're selecting all the
columns from the M table
7
00:00:32,960 --> 00:00:35,710
where E name equals Smith.
8
00:00:35,710 --> 00:00:37,990
But let's think about
all of the things that
9
00:00:37,990 --> 00:00:40,660
go into that SQL statement.
10
00:00:40,660 --> 00:00:43,300
It's a simple one
line SQL statement.
11
00:00:43,300 --> 00:00:47,030
It has a select clause,
a from and a where,
12
00:00:47,030 --> 00:00:51,020
but there's actually a lot more
that goes on behind the scenes
13
00:00:51,020 --> 00:00:53,560
that it's important
to understand.
14
00:00:53,560 --> 00:00:57,130
So what happens when we
execute this statement?
15
00:00:57,130 --> 00:01:00,370
Oracle has to perform
what's called a parse.
16
00:01:00,370 --> 00:01:03,370
It has to pass this
statement, and that basically
17
00:01:03,370 --> 00:01:07,360
means that it's going to break
down the statement into machine
18
00:01:07,360 --> 00:01:09,910
language that it can understand.
19
00:01:09,910 --> 00:01:14,530
So consider all that it has
to verify in this statement
20
00:01:14,530 --> 00:01:16,150
before it can execute it.
21
00:01:16,150 --> 00:01:20,380
So first and foremost, it has
to be syntactically correct,
22
00:01:20,380 --> 00:01:23,440
meaning all of the clauses
are spelled correctly.
23
00:01:23,440 --> 00:01:28,100
If we misspell a
clause and execute it,
24
00:01:28,100 --> 00:01:31,190
we get a error message
that says from keyword not
25
00:01:31,190 --> 00:01:34,230
found where expected.
26
00:01:34,230 --> 00:01:35,770
So that's the first
step it has to go
27
00:01:35,770 --> 00:01:39,580
through, syntactical
verification of the statement.
28
00:01:39,580 --> 00:01:42,430
The next thing it
has to do is verify
29
00:01:42,430 --> 00:01:44,750
that the objects actually exist.
30
00:01:44,750 --> 00:01:48,520
So we say select all the
columns from the M table,
31
00:01:48,520 --> 00:01:51,310
but in the parse
phase, Oracle has
32
00:01:51,310 --> 00:01:54,670
to decide whether the
M table actually exists
33
00:01:54,670 --> 00:01:57,730
and whether the
Scott user owns it.
34
00:01:57,730 --> 00:02:02,880
So for instance, if we
misspell that and execute,
35
00:02:02,880 --> 00:02:04,090
table or view does not exist.
36
00:02:07,510 --> 00:02:09,760
The next thing it has to
do is column verification,
37
00:02:09,760 --> 00:02:13,610
it has to verify that this
column exists in this table.
38
00:02:13,610 --> 00:02:19,140
So again, misspelled
and executed,
39
00:02:19,140 --> 00:02:23,500
we get an invalid
identifier error.
40
00:02:23,500 --> 00:02:26,390
So all of these
things come into play.
41
00:02:26,390 --> 00:02:30,440
And then the security
phase begins, if you will.
42
00:02:30,440 --> 00:02:33,130
So if we are
syntactically correct
43
00:02:33,130 --> 00:02:36,400
and if these objects
and columns exist,
44
00:02:36,400 --> 00:02:39,220
then Oracle has to
determine whether or not
45
00:02:39,220 --> 00:02:43,930
the user executing the command
has the proper permissions
46
00:02:43,930 --> 00:02:47,560
to read from this table and, in
some cases, from this column.
47
00:02:47,560 --> 00:02:50,990
So that has to occur, as well.
48
00:02:50,990 --> 00:02:53,400
Now all of that is
part of the parse
49
00:02:53,400 --> 00:02:55,730
and we're starting
to see that that
50
00:02:55,730 --> 00:02:59,180
could add up to a lot of
work on the part of Oracle.
51
00:02:59,180 --> 00:03:01,040
And indeed, parsing
a statement is
52
00:03:01,040 --> 00:03:04,040
considered a relatively
expensive operation.
53
00:03:04,040 --> 00:03:06,200
But the most important
thing that Oracle
54
00:03:06,200 --> 00:03:10,670
does during the parse phase is
it breaks down the statement
55
00:03:10,670 --> 00:03:13,610
and comes up with
an execution plan.
56
00:03:13,610 --> 00:03:17,960
The execution plan is going
to be Oracle's determination
57
00:03:17,960 --> 00:03:20,810
for the best way,
the fastest way
58
00:03:20,810 --> 00:03:23,630
to retrieve the data
that's requested.
59
00:03:23,630 --> 00:03:25,940
So it looks at all
the possible ways
60
00:03:25,940 --> 00:03:29,090
that data could be
retrieved in this statement.
61
00:03:29,090 --> 00:03:31,850
Does it look through
every row in the table?
62
00:03:31,850 --> 00:03:33,530
Does it use an index?
63
00:03:33,530 --> 00:03:35,060
Does it use a partition?
64
00:03:35,060 --> 00:03:37,400
So any of those
things can affect
65
00:03:37,400 --> 00:03:41,900
how a statement is executed,
and to DBAs, the execution plan
66
00:03:41,900 --> 00:03:45,140
is incredibly important,
and really developers, too.
67
00:03:45,140 --> 00:03:48,080
The execution plan
determines really how well
68
00:03:48,080 --> 00:03:49,730
the statement will perform.
69
00:03:49,730 --> 00:03:51,650
So if we write a
statement that's
70
00:03:51,650 --> 00:03:55,370
really written improperly
or could be written better,
71
00:03:55,370 --> 00:03:58,260
then it's going
to perform poorly.
72
00:03:58,260 --> 00:04:01,760
And so we've got a fantastic
tool, a very useful tool
73
00:04:01,760 --> 00:04:05,240
in order to determine what the
execution plan of a statement
74
00:04:05,240 --> 00:04:06,680
is going to be.
75
00:04:06,680 --> 00:04:08,000
Else how would we know--
76
00:04:08,000 --> 00:04:09,650
when we write this
statement, how
77
00:04:09,650 --> 00:04:11,990
would we know if
it's basically well
78
00:04:11,990 --> 00:04:14,310
written or not well written?
79
00:04:14,310 --> 00:04:18,080
And that's something
called an explain plan.
80
00:04:18,080 --> 00:04:20,930
Now there's different
ways to do explain plan,
81
00:04:20,930 --> 00:04:24,410
but SQL Developer has a very
useful facility here right up
82
00:04:24,410 --> 00:04:28,040
in our toolbar called
the explain plan button.
83
00:04:28,040 --> 00:04:33,030
And we click the button
and it gives us the output
84
00:04:33,030 --> 00:04:35,310
that you see here.
85
00:04:35,310 --> 00:04:38,220
It's going to
access the Table M,
86
00:04:38,220 --> 00:04:41,700
it's going to do a full
scan, a full table scan,
87
00:04:41,700 --> 00:04:45,660
meaning it's going to read every
row in the search for E name
88
00:04:45,660 --> 00:04:48,370
equals Smith, and
it has a cost--
89
00:04:48,370 --> 00:04:50,550
a resource cost of 3.
90
00:04:50,550 --> 00:04:52,350
That's kind of a
relative number,
91
00:04:52,350 --> 00:04:54,390
but it can be used
at times to determine
92
00:04:54,390 --> 00:04:57,000
whether you've made an
improvement in a statement
93
00:04:57,000 --> 00:04:58,420
or not.
94
00:04:58,420 --> 00:05:01,960
This gives us a
basic execution plan
95
00:05:01,960 --> 00:05:05,470
for how our statement is
going to run in the database.
96
00:05:05,470 --> 00:05:08,020
So what kinds of
things can we do
97
00:05:08,020 --> 00:05:12,280
to influence the outcome
of an execution plan?
98
00:05:12,280 --> 00:05:15,760
Well, in this case, we
might be able to improve
99
00:05:15,760 --> 00:05:18,710
on this idea of a
full table scan.
100
00:05:18,710 --> 00:05:20,950
So basically what
will occur here
101
00:05:20,950 --> 00:05:24,010
is that, as Oracle
searches this table,
102
00:05:24,010 --> 00:05:26,140
it will take the
first row and look
103
00:05:26,140 --> 00:05:28,660
to see whether the
e name equals Smith.
104
00:05:28,660 --> 00:05:30,700
If it does, it will
return the row.
105
00:05:30,700 --> 00:05:32,140
If not, it goes
to the next where
106
00:05:32,140 --> 00:05:36,460
it checks it, goes to the next,
next, next, on down the line.
107
00:05:36,460 --> 00:05:39,650
Well, the M table in
our case has 14 rows.
108
00:05:39,650 --> 00:05:42,960
That's not a terribly
high cost, if we would.
109
00:05:42,960 --> 00:05:45,700
But what if our table
had millions of rows?
110
00:05:45,700 --> 00:05:49,660
Is doing a full table
scan really the best way
111
00:05:49,660 --> 00:05:53,080
to approach querying a table
for that particular piece
112
00:05:53,080 --> 00:05:54,470
of information?
113
00:05:54,470 --> 00:05:58,620
So we might use something called
an index, which will basically
114
00:05:58,620 --> 00:06:02,640
create a structure that allows
Oracle to more quickly access
115
00:06:02,640 --> 00:06:03,630
that piece of data.
116
00:06:09,310 --> 00:06:12,190
So we've created an
index for the M table
117
00:06:12,190 --> 00:06:14,060
on the E name column.
118
00:06:14,060 --> 00:06:19,260
So now anytime that
we run this statement,
119
00:06:19,260 --> 00:06:23,310
it will not do a full
table scan of every row.
120
00:06:23,310 --> 00:06:26,650
Instead, it will search the
index that we've created.
121
00:06:26,650 --> 00:06:31,660
And we can see this using
the explain plan button.
122
00:06:31,660 --> 00:06:34,170
Notice how our explain
plan is different now.
123
00:06:34,170 --> 00:06:37,530
We've added the index and our
explained plan is different,
124
00:06:37,530 --> 00:06:39,390
our execution plan is different.
125
00:06:39,390 --> 00:06:40,650
We've affected it.
126
00:06:40,650 --> 00:06:44,100
So it's going to use the
objects M, like before,
127
00:06:44,100 --> 00:06:46,750
but it's also going
to use M index.
128
00:06:46,750 --> 00:06:51,090
And instead of full here,
it says by index row ID
129
00:06:51,090 --> 00:06:53,460
patched and range scan.
130
00:06:53,460 --> 00:06:55,890
So it's going to do a scan
of the index to come up
131
00:06:55,890 --> 00:06:57,330
with the information.
132
00:06:57,330 --> 00:06:59,460
And notice that the
cost, the total cost
133
00:06:59,460 --> 00:07:00,990
here has gone from three to two.
134
00:07:00,990 --> 00:07:03,450
Even in a very small
table, we can see
135
00:07:03,450 --> 00:07:07,220
the benefit of adding an index.
136
00:07:07,220 --> 00:07:15,360
And if we drop the index, again,
look at our explained plan,
137
00:07:15,360 --> 00:07:18,240
we see that it has gone
back to the original cost
138
00:07:18,240 --> 00:07:21,130
and doing a full table scan.
139
00:07:21,130 --> 00:07:24,040
Now all of these examples
are relatively simple
140
00:07:24,040 --> 00:07:26,830
and these explain plans
aren't terribly complex,
141
00:07:26,830 --> 00:07:29,050
but that's not always the case.
142
00:07:29,050 --> 00:07:30,550
These tables are small.
143
00:07:30,550 --> 00:07:33,440
In the real world, tables
can be much larger.
144
00:07:33,440 --> 00:07:36,490
There can be joins involved
in the select statement
145
00:07:36,490 --> 00:07:38,350
and it can get much
more complicated.
146
00:07:38,350 --> 00:07:40,900
So just to show you
what that looks like,
147
00:07:40,900 --> 00:07:44,770
let's use kind of
an extreme example.
148
00:07:44,770 --> 00:07:49,660
I'm going to make a connection
to the database as system
149
00:07:49,660 --> 00:07:52,270
because I want to query
from a data dictionary view.
150
00:07:57,310 --> 00:08:02,880
So I'm looking for the
M table owned by Scott
151
00:08:02,880 --> 00:08:05,190
and this gives us
information about it.
152
00:08:05,190 --> 00:08:07,560
But again, this statement
has to be parsed
153
00:08:07,560 --> 00:08:10,320
and an execution plan has
to be developed for it,
154
00:08:10,320 --> 00:08:14,340
so let's look at what
this plan might look like.
155
00:08:14,340 --> 00:08:18,420
Considerably more complicated.
156
00:08:18,420 --> 00:08:21,520
In fact, it's nested
so far that it's
157
00:08:21,520 --> 00:08:24,630
difficult to see in
the explain plan.
158
00:08:24,630 --> 00:08:29,180
The reason that this is such
a complex execution plan
159
00:08:29,180 --> 00:08:32,240
is that DBA tables is
actually a view that
160
00:08:32,240 --> 00:08:36,110
looks at many tables in
the underlying system.
161
00:08:36,110 --> 00:08:38,540
Now a lot of these
tables are things
162
00:08:38,540 --> 00:08:48,110
like, OBJ, pound sign, file
pound sign, user dollar,
163
00:08:48,110 --> 00:08:49,560
OBJ dollar.
164
00:08:49,560 --> 00:08:53,480
These are underlying tables
in the Oracle Data dictionary
165
00:08:53,480 --> 00:08:55,430
that Oracle uses itself.
166
00:08:55,430 --> 00:08:57,600
They're not meant to
be queried directly.
167
00:08:57,600 --> 00:08:59,270
They're very difficult
to read, they
168
00:08:59,270 --> 00:09:02,810
don't have column names that
are very indicative of what
169
00:09:02,810 --> 00:09:04,190
the data contains.
170
00:09:04,190 --> 00:09:08,450
But Oracle puts simpler
views on top of those tables
171
00:09:08,450 --> 00:09:11,090
in order to make things
simpler for us to understand.
172
00:09:11,090 --> 00:09:13,490
But notice a few things here.
173
00:09:13,490 --> 00:09:16,310
It's doing things like
a join in this execution
174
00:09:16,310 --> 00:09:22,190
plan with a Cartesian
product even, an outer join.
175
00:09:22,190 --> 00:09:27,630
It's doing an index row ID
scan of the user dollar table.
176
00:09:27,630 --> 00:09:31,170
So there's many different
types of options in this column
177
00:09:31,170 --> 00:09:34,350
that Oracle can
tell us that it's
178
00:09:34,350 --> 00:09:39,650
using in order to communicate
the execution plan to us.
14502
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.