SELECT * FROM
Employee
WHERE
Employee_Name = 'Jesus'
For more details on Indexes and it's Types- Click here.
What would happen without an Index on the table?
The database would
literally have to look at every single row in the Employee table to see if the
Employee_Name for that row is ‘Jesus’. And, because we want every row with the
name ‘Jesus’ inside it, we cannot just stop looking once we find just one row
with the name ‘Jesus’, because there could be other rows with the name Jesus.
So, every row up
until the last row must be searched – which means thousands of rows in this
scenario will have to be examined by the database to find the rows with the
name ‘Jesus’. This is what is called a full table scan.
How does an index improve performance?
Because an index
is basically a data structure that is used to store column values, looking up
those values becomes much faster. And, if an index is using the most commonly
used data structure type – a B- tree – then the data structure is also sorted.
Having the column values be sorted can be a major performance enhancement.How does an index improve performance?
IF we create a B-
tree index on the Employee_Name column This means that when we search for
employees named “Jesus” using the above SQL, then the entire Employee table
does not have to be searched to find employees named “Jesus”. Instead, the
database will use the index to find employees named Jesus, because the index
will presumably be sorted alphabetically by the Employee’s name. And, because
it is sorted, it means searching for a name is a lot faster because all names
starting with a “J” will be right next to each other in the index! It’s also
important to note that the index also stores pointers to the table row so that
other column values can be retrieved.
What exactly is inside a database index?
So, now you know
that a database index is created on a column in a table, and that the index
stores the values in that specific column. But, it is important to understand
that a database index does not store the values in the other columns of the
same table. For example, if we create an index on the Employee_Name column,
this means that the Employee_Age and Employee_Address column values are not
also stored in the index. If we did just store all the other columns in the
index, then it would be just like creating another copy of the entire table –
which would take up way too much space and would be very inefficient.What exactly is inside a database index?
An index also
stores a pointer to the table row So, the question is if the value that we are
looking for is found in an index (like ‘Jesus’) , how does it find the other
values that are in the same row (like the address of Jesus and his age)? Well,
it’s quite simple – database indexes also store pointers to the corresponding
rows in the table.
A pointer is just a reference to a place in memory where the
row data is stored on disk. So, in addition to the column value that is stored
in the index, a pointer to the row in the table where that value lives is also
stored in the index. This means that one of the values (or nodes) in the index
for an Employee_Name could be something like (“Jesus”, 0x82829), where 0x82829
is the address on disk (the pointer) where the row data for “Jesus” is stored.
Without that pointer all you would have is a single value, which would be
meaningless because you would not be able to retrieve the other values in the
same row – like the address and the age of an employee.
Get involved
and leave your Comments in the Box Below. The more people get involved, the
more we all benefit.
So, leave your thoughts before you leave
the page.
can we create index on foreign key?
ReplyDeleteYes, we can.
Delete