Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,430 --> 00:00:05,290
In this lesson, we're looking
at function based indexes.
2
00:00:05,290 --> 00:00:08,810
A function based
index is another type
3
00:00:08,810 --> 00:00:12,180
of index for very
specific situations.
4
00:00:12,180 --> 00:00:14,700
So let's take an example here.
5
00:00:14,700 --> 00:00:18,110
Let's say that we have a
library or a application that
6
00:00:18,110 --> 00:00:21,740
has library books
in it, and we have
7
00:00:21,740 --> 00:00:25,330
books that have their author,
maybe the number of pages.
8
00:00:25,330 --> 00:00:27,140
But they have their
published date.
9
00:00:27,140 --> 00:00:30,200
And so, when we
query, we may want
10
00:00:30,200 --> 00:00:34,130
to see the book that was
published on a certain date.
11
00:00:34,130 --> 00:00:36,890
And, if it's a large
list, a large number
12
00:00:36,890 --> 00:00:40,500
of rows in the table, then we
may want to put an index on it.
13
00:00:40,500 --> 00:00:43,520
And so, if we index
that particular
14
00:00:43,520 --> 00:00:45,290
column with an index--
15
00:00:45,290 --> 00:00:47,600
with a regular B-tree index--
16
00:00:47,600 --> 00:00:50,360
and we look up based
on the exact date,
17
00:00:50,360 --> 00:00:52,190
then it will use that index.
18
00:00:52,190 --> 00:00:53,900
So if it's a long
list, we need an index,
19
00:00:53,900 --> 00:00:56,910
we're trying to speed
our query performance.
20
00:00:56,910 --> 00:01:01,100
But what if we were to query
for all of the books that
21
00:01:01,100 --> 00:01:03,600
were published on a given year?
22
00:01:03,600 --> 00:01:08,270
And so we'd actually use a two
car function to find that list.
23
00:01:08,270 --> 00:01:11,030
Well, in this case,
the Oracle optimizer
24
00:01:11,030 --> 00:01:15,350
is going to ignore the index
because the values are indexed
25
00:01:15,350 --> 00:01:18,260
based on their full
date, that full date
26
00:01:18,260 --> 00:01:21,560
value, not on just the year.
27
00:01:21,560 --> 00:01:23,810
And so we would
see ourselves doing
28
00:01:23,810 --> 00:01:28,170
this query and the performance
being significantly slower.
29
00:01:28,170 --> 00:01:31,610
So the function based index
is an interesting solution
30
00:01:31,610 --> 00:01:33,120
to this problem.
31
00:01:33,120 --> 00:01:36,950
So we use a function based index
to query based on a function.
32
00:01:36,950 --> 00:01:41,090
So we need the function
we create in the index
33
00:01:41,090 --> 00:01:43,310
to match the function
that we're going
34
00:01:43,310 --> 00:01:46,020
to use when we make the query.
35
00:01:46,020 --> 00:01:48,050
So let's just kind
of break this down
36
00:01:48,050 --> 00:01:50,390
with an example in
the Scott schema.
37
00:01:50,390 --> 00:01:53,190
So I'm connected
to my Scott user.
38
00:01:53,190 --> 00:01:57,390
I'm going to do
select star from EMP.
39
00:01:57,390 --> 00:02:01,090
One of the values that
we see here is hire date.
40
00:02:01,090 --> 00:02:04,960
So sometimes we want to
query based on the hire date.
41
00:02:04,960 --> 00:02:11,460
So if I do select star from
EMP where a hire equals--
42
00:02:11,460 --> 00:02:20,700
and we'll pick one of them, 01
May 81, the value is returned.
43
00:02:20,700 --> 00:02:23,070
But let's say there were
a lot of values here
44
00:02:23,070 --> 00:02:25,530
and this query was
taking a lot of time.
45
00:02:25,530 --> 00:02:30,250
And so I may want to create
an index on hire date.
46
00:02:30,250 --> 00:02:33,960
So, to show this, we're going
to look at the explain plan
47
00:02:33,960 --> 00:02:36,510
button, which is right here.
48
00:02:36,510 --> 00:02:38,700
And then explain
plan button is going
49
00:02:38,700 --> 00:02:42,300
to execute the explain plan,
which is going to break down
50
00:02:42,300 --> 00:02:46,080
the structure of how
Oracle will perform
51
00:02:46,080 --> 00:02:49,690
the solution at the
values based on our query.
52
00:02:49,690 --> 00:02:53,220
All right, so we see that
it's doing a full table scan.
53
00:02:53,220 --> 00:02:55,370
So it's doing a full
scan of every value.
54
00:02:55,370 --> 00:02:56,330
And we don't want that.
55
00:02:56,330 --> 00:02:59,060
We want it to do
something with an index.
56
00:02:59,060 --> 00:03:08,380
So we'll say create index
emp_idx on emo hire date.
57
00:03:08,380 --> 00:03:10,110
It's created.
58
00:03:10,110 --> 00:03:13,070
And now, when we run
the explained plan,
59
00:03:13,070 --> 00:03:15,020
we see that it's not
doing a full scan,
60
00:03:15,020 --> 00:03:18,500
it's doing an index scan,
an index range scan.
61
00:03:18,500 --> 00:03:20,940
And so that's generally
going to be much faster.
62
00:03:20,940 --> 00:03:23,000
So that's exactly what we want.
63
00:03:23,000 --> 00:03:25,790
However, let's say we
have a different query.
64
00:03:25,790 --> 00:03:28,370
We may run a report
against our employees
65
00:03:28,370 --> 00:03:32,750
to find all the employees
that were hired in 1981.
66
00:03:32,750 --> 00:03:36,590
We'll need to break
this out in a function.
67
00:03:36,590 --> 00:03:39,070
Two part hire date.
68
00:03:39,070 --> 00:03:40,960
So a four digit year.
69
00:03:40,960 --> 00:03:48,430
And we say equal to 1981.
70
00:03:48,430 --> 00:03:51,380
So we get that value
back, this value
71
00:03:51,380 --> 00:03:54,030
is for those hired in 1981.
72
00:03:54,030 --> 00:03:55,860
But let's look at
the explain plan.
73
00:03:55,860 --> 00:03:58,710
Now, we have an
index on hire date.
74
00:03:58,710 --> 00:04:01,670
And notice that it's
doing a full table scan.
75
00:04:01,670 --> 00:04:04,760
It's not using the
index that we created.
76
00:04:04,760 --> 00:04:05,540
Why?
77
00:04:05,540 --> 00:04:09,080
Because we indexed based
on the entire date and not
78
00:04:09,080 --> 00:04:10,280
on the year.
79
00:04:10,280 --> 00:04:13,340
So, if we do queries
based on the year,
80
00:04:13,340 --> 00:04:17,690
just the year alone, we may
use a function based index.
81
00:04:17,690 --> 00:04:22,400
We're going to
drop emp_idx index,
82
00:04:22,400 --> 00:04:26,760
and we're going to create
a function based index.
83
00:04:26,760 --> 00:04:31,350
fidx for function
based index on emp.
84
00:04:31,350 --> 00:04:34,980
And then we specify the function
that we're going to use.
85
00:04:39,870 --> 00:04:42,100
And it was created.
86
00:04:42,100 --> 00:04:44,050
Again, just running
the statement
87
00:04:44,050 --> 00:04:46,030
shows that it returns.
88
00:04:46,030 --> 00:04:50,090
But now, if we look
at the explained plan,
89
00:04:50,090 --> 00:04:54,020
notice that it's
using the higher FIDX,
90
00:04:54,020 --> 00:04:56,360
spelled it that way,
and it's saying it's
91
00:04:56,360 --> 00:04:59,750
by index row ID range scan.
92
00:04:59,750 --> 00:05:03,320
So it's actually using an
index to do the query now
93
00:05:03,320 --> 00:05:05,720
after we've created a
function based index,
94
00:05:05,720 --> 00:05:08,000
whereas before it was not.
7482
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.