You are on page 1of 11

Index

 A database index is a data structure that improves the


speed of operations in a table.
 Indexes are created on a per column basis. If you have a
table with the columns: name, age, birthday and
employeeID and want to create an index to speed up how
long it takes to find employeeID values in your queries,
then you would need to create an index for employeeID.
 When you create this index, MySQL will build a lookup
index where employeeID specific queries can be run
quickly. However, the name, age and birthday queries
would not be any faster.
 Indexes are something extra that you can enable on your
MySQL tables to increase performance,cbut they do have
some downsides.
 When you create a new index MySQL builds a separate
block of information that needs to be updated every time
there are changes made to the table.
 This means that if you are constantly updating, inserting
and removing entries in your table this could have a
negative impact on performance.
creating a index 

 If you are creating a new MySQL table you can specify a


column to index by using the INDEX term as we have
below.
 We have created two fields: name and employeeID
(index).
 CREATE TABLE employee_records (
name VARCHAR(50),
employeeID INT, INDEX (employeeID));
creating a index - existing table

 CREATE INDEX name_index ON Employee


(Employee_Name)
How to create a multi-column index in SQL:
 CREATE table Employee (Employee_Name varchar(25),
Employee_Age int (25));

 CREATE INDEX name_index ON Employee


(Employee_Name, Employee_Age);

 drop index name_index on employee;


why you would need a database index
 why you would need a database index by going through a
very simple example.
 Suppose that we have a database table called Employee
with three columns – Employee_Name, Employee_Age,
and Employee_Address. Assume that the Employee table
has thousands of rows.
 Now, let’s say that we want to run a query to find all the
details of any employees who are named ‘Harry’?
 So, we decide to run a simple query like this:
 SELECT * FROM Employee WHERE Employee_Name
= ‘Harry'
What would happen without an index on the table?
 Once we run that query, what exactly goes on behind the
scenes to find employees who are named Harry?
 Well, the database software would literally have to look at
every single row in the Employee table to see if the
Employee_Name for that row is ‘Harry’. And, because we
want every row with the name ‘Harry’ inside it, we can
not just stop looking once we find just one row with the
name Harry’, because there could be other rows with the
name Harry.
 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 a database index can help performance
 The whole point of having an index is to speed up search
queries by essentially cutting down the number of
records/rows in a table that need to be examined.
What kind of data structure is an index?

 B- trees are the most commonly used data structures for


indexes. The reason B- trees are the most popular data
structure for indexes is due to the fact that they are time
efficient – because look-ups, deletions, and insertions can
all be done in logarithmic time. And, another major reason
B- trees are more commonly used is because the data that
is stored inside the B- tree can be sorted

You might also like