INDICES
Databases
1
1. What are Index?
Nobody likes slow systems.
High system performance is of primary importance in
almost all database systems.
Most companies invest heavily in hardware so that data
retrievals and manipulations can be faster.
But there is a limit to the hardware investments a company
can make.
Optimizing your database is a cheaper and better solution.
For this purpose, we can use INDEXES.
2
2. Why is indexing important?
Slow response time is usually due to records being stored
randomly in database tables.
Search queries have to traverse the stored records
randomly one after another to locate the desired data.
This results in poorly performing databases when trying to
recover data from large tables
3
2. Why is indexing important?
INDEXES are created on the column(s) that will be used to
filter the data.
Using indexes on tables that are updated frequently can
result in poor performance. This is because MySQL creates
a new index block every time data is added or updated in
the table.
In general, indexes should be used on tables whose data
does not change frequently but are used extensively in
certain search queries.
4
3. Create basic index syntax
Indices can be defined in 2 ways:
• At the time of creation of the DB (CREATE)
• After the table has been created (ALTER TABLE)
Why is indexing
important?
Non-
indexed Indexed
database database
5
4. What type of indexes are there?
• Unique
• Primary
• Ordinary
• Full text (not common)
• Part of fields or columns (not common)
6
5. Unique index (UNIQUE, ADD UNIQUE)
A unique index does not allow the insertion of duplicate
values in the column that we mark as unique. Can be
applied to multiple columns
To modify a table:
ALTER TABLE "table_name“
ADD UNIQUE (column1_name, column2_name,...);
When a table is
created:
CREATE TABLE "table_name" (column1_name [type of value], column2_name
[type of value],...
UNIQUE [column1_name, column2_name,...] );
7
6. Primary Index (PRIMARY KEY)
A primary index is also known as Primary KEY, it allows you
to create an index whose value is unique and cannot be
NULL. There can only be one primary index per table.
To modify a table:
ALTER TABLE "table_name“
ADD PRIMARY KEY (column1_name, column2_name,...);
When a table is
created:
CREATE TABLE "table_name" (column1_name [type of value], column2_name
[type of value],...
PRIMARY KEY [column1_name, column2_name,...] );
8
7. Ordinary Index (ADD INDEX)
This type of index in MySQL allows the existence or
insertion of duplicate values in a table. With this type we
only improve the execution and performance of the queries
(Which istable:
To modify a no small thing).
ALTER TABLE "table_name“
ADD INDEX (column1_name, column2_name,...);
When a table is
created:
CREATE TABLE "table_name" (column1_name [type of value], column2_name
[type of value],...
INDEX [column1_name, column2_name,...] );
9
8. Full text indexes (FULLTEXT)
These FULLTEXT or full text indexes are used in tables of
type MyISAM, they must be related to columns of type
TEXT, CHAR or VARCHAR.
They optimize query performance by searching tables with
rich texta table:
To modify field information.
ALTER TABLE "table_name“
ADD FULLTEX (column1_name, column2_name,...);
When a table is
created:
CREATE TABLE "table_name" (column1_name [type of value], column2_name
[type of value],...
FULTEXT [column1_name, column2_name,...] );
10
9. Indexes with part of fields or columns
Similar to the previous FULLTEXT index but only taking part
of the column content. These types of indexes simplify
queries on text fields. For example, if we search on tables
of names or surnames, selecting the first characters would
be useful for making the comparison.
These indexes are defined by selecting the first X
characters of the column
To modify a table:
ALTER TABLE "table_name“
ADD FULLTEX (column1_name (X), column2_name(X),...);
When a table is
created:
CREATE TABLE "table_name" (column1_name [type of value], column2_name
[type of value],...
FULTEXT [column1_name (X), column2_name (X),...] );
11
12