Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:08,192 --> 00:00:08,960
Hi everyone
1
2
00:00:09,216 --> 00:00:11,520
In this video we will learn about indexes
2
3
00:00:12,032 --> 00:00:14,080
Indexes are a performance tuning method
3
4
00:00:14,592 --> 00:00:16,640
Which allow faster retrieval of records
4
5
00:00:17,408 --> 00:00:20,224
An index in a database is similar to an index in a book
5
6
00:00:20,992 --> 00:00:23,808
Just like in a book if you want to find a particular chapter
6
7
00:00:24,064 --> 00:00:25,088
or a particular topic
7
8
00:00:25,600 --> 00:00:26,880
You first refer to the index
8
9
00:00:27,648 --> 00:00:31,232
Find out the page number go to that page and look at that topic
9
10
00:00:31,744 --> 00:00:33,024
similar is in the database
10
11
00:00:33,280 --> 00:00:34,304
If there is an index
11
12
00:00:34,560 --> 00:00:36,864
The database will first go to the index
12
13
00:00:37,120 --> 00:00:41,984
Find out the relevant position of that record and then go to the table and retrieve the data
13
14
00:00:42,496 --> 00:00:45,824
There are benefits of having an index and there are cons also
14
15
00:00:46,080 --> 00:00:48,896
so if it helps in faster retrieval of records
15
16
00:00:49,408 --> 00:00:50,944
If you have an index
16
17
00:00:51,200 --> 00:00:56,832
It will lead to a delay in the up-gradation of records when you update records in a table
17
18
00:00:57,600 --> 00:00:59,648
So if there is an index built on a table
18
19
00:00:59,904 --> 00:01:03,232
And you try to update the table using the update command
19
20
00:01:03,744 --> 00:01:06,816
It will take more time than if there was no index
20
21
00:01:07,072 --> 00:01:09,120
So it is important that we only
21
22
00:01:09,376 --> 00:01:11,936
Apply indexes if it is really necessary
22
23
00:01:12,960 --> 00:01:15,008
Let us learn how to apply an index
23
24
00:01:15,520 --> 00:01:16,800
The syntax for that is
24
25
00:01:17,312 --> 00:01:18,336
We write create
25
26
00:01:18,848 --> 00:01:19,360
Index
26
27
00:01:20,128 --> 00:01:21,408
Give the index a name
27
28
00:01:22,176 --> 00:01:23,712
On the table name
28
29
00:01:24,224 --> 00:01:27,040
and then we specify the column names which are to be used
29
30
00:01:27,552 --> 00:01:28,576
For creating this index
30
31
00:01:29,344 --> 00:01:33,952
Usually we have only one column name, this single column index is called as simple index
31
32
00:01:34,464 --> 00:01:36,768
Sometimes we use two or more columns
32
33
00:01:37,024 --> 00:01:39,840
as primary key to retrieve data, in those cases
33
34
00:01:40,096 --> 00:01:41,888
we use more than two columns
34
35
00:01:42,144 --> 00:01:44,192
And that index is called a composite index
35
36
00:01:44,448 --> 00:01:46,752
Let us look at one example
36
37
00:01:47,264 --> 00:01:49,056
So if you want to create an index
37
38
00:01:49,824 --> 00:01:50,848
On month values
38
39
00:01:51,104 --> 00:01:55,712
If you remember we had in the cross join we had a month table
39
40
00:01:56,224 --> 00:01:58,784
On that month table if you wanted to create an index
40
41
00:01:59,552 --> 00:02:01,344
You can create an index like create index
41
42
00:02:01,856 --> 00:02:03,648
Mon underscore IDX
42
43
00:02:03,904 --> 00:02:10,048
Usually when you are naming an index developers name an Index like this. It is not mandatory you can name it anything
43
44
00:02:10,304 --> 00:02:13,120
But usually it is done like this, mon_idx
44
45
00:02:13,376 --> 00:02:15,936
This idx give you an idea that it is an index
45
46
00:02:17,216 --> 00:02:17,728
On
46
47
00:02:17,984 --> 00:02:22,080
Month values this is the table name and in the bracket the column name is written
47
48
00:02:22,592 --> 00:02:24,896
Let us go and write this in the PG admin
48
49
00:02:40,000 --> 00:02:45,376
Create index mon index on month values this is the column name select this
49
50
00:02:48,960 --> 00:02:49,984
We have
50
51
00:02:50,752 --> 00:02:53,312
Successfully created an index. To check the index
51
52
00:02:53,824 --> 00:02:55,872
We go to this particular table
52
53
00:02:56,640 --> 00:03:00,224
so let us refresh and go to the particular table
53
54
00:03:00,992 --> 00:03:04,064
open it. There is one
54
55
00:03:04,576 --> 00:03:06,624
Index option there is your
55
56
00:03:06,880 --> 00:03:08,160
Index mon index
56
57
00:03:08,416 --> 00:03:09,184
So
57
58
00:03:09,696 --> 00:03:11,744
This index is created whenever you
58
59
00:03:12,000 --> 00:03:13,792
Do a query on this table
59
60
00:03:14,048 --> 00:03:20,192
It will automatically go and use this index that is why whenever you will be updating this table also it will go and
60
61
00:03:20,448 --> 00:03:21,984
Change this index also
61
62
00:03:22,240 --> 00:03:23,776
That is why it'll be taking more time
62
63
00:03:24,032 --> 00:03:29,920
Apart from creating you can also rename an index or drop an index. Let us look at the syntax for that also
63
64
00:03:31,200 --> 00:03:34,016
So to drop an index you just write drop index
64
65
00:03:35,040 --> 00:03:36,320
And give the index name
65
66
00:03:37,344 --> 00:03:38,880
This if exists
66
67
00:03:39,136 --> 00:03:45,280
Is an optional keyword if you write it it will check if the index is there and then delete it
67
68
00:03:45,536 --> 00:03:47,328
If it is not there it will not give you an error
68
69
00:03:48,352 --> 00:03:49,888
So when you are running
69
70
00:03:50,144 --> 00:03:51,168
A set of queries
70
71
00:03:51,424 --> 00:03:52,960
And this is in between them
71
72
00:03:53,216 --> 00:03:57,312
If you have not return if exist and you are dropping an index and it does not exist
72
73
00:03:58,080 --> 00:04:02,176
It will fail the query and your all the queries in your set of queries will not run
73
74
00:04:02,432 --> 00:04:05,760
so it is advisable that you write if exists also
74
75
00:04:06,272 --> 00:04:07,040
In between
75
76
00:04:07,552 --> 00:04:09,088
And this if exists
76
77
00:04:09,344 --> 00:04:11,904
Keyword can be used while dropping
77
78
00:04:12,416 --> 00:04:13,952
Tables also. So it is not
78
79
00:04:14,208 --> 00:04:16,768
Restricted to indexes, you can use it to
79
80
00:04:17,280 --> 00:04:18,559
Drop table also
80
81
00:04:20,351 --> 00:04:23,423
The Other optional keyword is the cascade and restrict
81
82
00:04:23,679 --> 00:04:24,959
So if there is an
82
83
00:04:25,215 --> 00:04:29,823
Database object which is dependent on this index and you're deleting this index
83
84
00:04:30,591 --> 00:04:32,639
What will happen to that dependent object
84
85
00:04:32,895 --> 00:04:34,431
So if you write cascade
85
86
00:04:34,687 --> 00:04:36,991
That dependent object will also be deleted
86
87
00:04:38,015 --> 00:04:39,295
If you write restrict
87
88
00:04:40,063 --> 00:04:46,207
This index will not be deleted it will give you an error saying that there is an dependent object you cannot delete this
88
89
00:04:46,463 --> 00:04:46,975
Index
89
90
00:04:48,511 --> 00:04:51,327
So that is use of cascade and restrict keyboard
90
91
00:04:51,839 --> 00:04:54,911
If you want to rename an index you will use alter index
91
92
00:04:55,679 --> 00:04:58,495
Give the index name rename to give the new index name
92
93
00:04:59,263 --> 00:05:01,567
So this is how we drop and rename an index
93
94
00:05:02,335 --> 00:05:05,151
let us go and drop the index that we just created
94
95
00:05:06,943 --> 00:05:10,271
so we'll just write drop index
95
96
00:05:10,527 --> 00:05:13,599
and write the name of the index
96
97
00:05:17,439 --> 00:05:21,535
The query ran successfully just refresh this table
97
98
00:05:22,815 --> 00:05:24,607
And within indexes
98
99
00:05:28,447 --> 00:05:29,983
There is no index
99
100
00:05:30,239 --> 00:05:31,007
as of now
100
101
00:05:31,775 --> 00:05:34,079
So we successfully deleted the index
101
102
00:05:34,335 --> 00:05:37,663
You can rename using the syntax that we provided
102
103
00:05:42,015 --> 00:05:45,343
Apart from this there are some good practices while assigning an index
103
104
00:05:45,855 --> 00:05:47,135
So it is advisable that
104
105
00:05:47,391 --> 00:05:53,535
The column that you are using for creating index is of integer type this will be taking less space and will be
105
106
00:05:53,791 --> 00:05:54,303
More efficient
106
107
00:05:54,559 --> 00:05:55,583
So even if
107
108
00:05:56,095 --> 00:05:56,607
You have
108
109
00:05:57,631 --> 00:05:59,935
A text type of column which you want to use
109
110
00:06:00,703 --> 00:06:01,471
You can
110
111
00:06:01,983 --> 00:06:08,127
Do a one to one mapping of that textual column to an integer column and then use that integer column as an index
111
112
00:06:08,639 --> 00:06:09,919
So that will be more efficient
112
113
00:06:11,455 --> 00:06:14,527
another important point is make sure that the column you are building
113
114
00:06:15,039 --> 00:06:17,599
Is declared not null. so if there are null values
114
115
00:06:17,855 --> 00:06:20,159
The index does not work as efficiently
115
116
00:06:21,951 --> 00:06:26,815
And it is very important that you build an index when it is really necessary otherwise
116
117
00:06:27,071 --> 00:06:28,095
Some of the queries
117
118
00:06:28,351 --> 00:06:31,167
On your database will perform less efficiently
118
119
00:06:32,447 --> 00:06:33,727
That's all for the index
119
120
00:06:34,751 --> 00:06:38,079
In the next video we will learn about some important string functions
10123
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.