0% found this document useful (0 votes)
13 views

Indexes When to Use and When to Avoid In SQL. _ by BaseCS101 _ Level Up Coding

Uploaded by

kevinkao809
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Indexes When to Use and When to Avoid In SQL. _ by BaseCS101 _ Level Up Coding

Uploaded by

kevinkao809
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Open in app

Search Write

This member-only story is on us. Upgrade to access all of Medium.

Member-only story

Indexes When to Use and When to


Avoid In SQL.
Improve the performance of your database by knowing the concept of
indexes.

BaseCS101 · Follow
Published in Level Up Coding · 4 min read · Jan 27, 2023

70

Indexing

Through this article, we’ll learn what indexes are, why to use them, how to
create them, and when to avoid them. The concept of indexes is mostly
asked in the interviews which are focused on SQL.

What Is an Index?
Indexes are a special kind of lookup table that is used by the database search
engine to speed up data retrieval from the tables. Basically, an index is a
pointer that points to the tuples of a table.

An index in a database is very similar to an index in the back of a book. It


means when we want to jump to a specific topic from a book then we can
first see the index of the book and then find the page number for that topic
and then open that topic by going to that page number.

Another example can be, imagine we are looking to find a piece of


information that is within a large table in the database. To get this
information out of the table from the database, the search engine will look
through every record until it finds it assuming the records of the table are
unsorted. If the data we are looking for is towards the very end, this query
would take a long time to run.

For such scenarios, indexes are very useful to query the tables and get the
results very quickly. If the records of the table would have been sorted then
it would have been easier to search the record.
sorted records on name

In the above example, records of the table are sorted on the basis of id’s, and
to search for a name in the table we have to iterate through all the records of
the table for finding a match. This would take a linear time that is O(n) .

Hence we can have an index on the name which is sorted alphabetically and
points to actual records of the table. Using this index searching for a name
would take log(n) time as the records are alphabetically sorted and a binary
search can be performed.

Why Indexes Are Used?


Indexes help faster data retrieval from the databases. Basically, it speeds up
the select queries and where clause. But at the same time, it degraded the
performance of INSERT and UPDATE queries. Hence, we should be careful
while using the indexes.

How To Create Indexes?


CREATE INDEX Syntax:

The below syntax creates an index (index_name) on a table. This index will
allow duplicate values.

CREATE INDEX index_name


ON table_name (column1, column2, …);

CREATE UNIQUE INDEX Syntax:

The below syntax creates a unique index (index_name) on a table. This


index will not allow duplicate values.

CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, …);

Note: The syntax for creating indexes varies among different databases. So check
the syntax for creating indexes in your database.

DROP INDEX Syntax:

The DROP INDEX statement is used to delete an index in a table.

MySQL:
ALTER TABLE table_name
DROP INDEX index_name;

MS Access:

DROP INDEX index_name ON table_name;

SQL Server:

DROP INDEX table_name.index_name;

DB2/Oracle:

DROP INDEX index_name;

When to Create Single-column and Composite Indexes?


Whether to create a single-column index or a composite index, depends on
the columns being used in SELECT statements for filtering rows of a table.

We should look at the column(s) that are used very frequently in the query’s WHERE

clause as filter conditions.

If the WHERE clause uses a single column for filtering the rows then a single-
column index is good otherwise if multiple columns are used for filtering
then a composite index is suitable.

What Is the Implicit Index?


Implicit indexes are indexes automatically created by the database server
when a table is created. Indexes are automatically created for the primary key
and unique constraints.

When Should We Avoid Using Indexes?


The purpose of indexes is to enhance a database’s performance, there are
multiple scenarios where indexes should not be used.

1. Indexes should not be used on tables containing few records.

2. Tables that have frequent, large batch updates or insert operations.

3. Indexes should not be used on columns that contain a high number of


NULL values.

4. Indexes should not be used on the columns that are frequently


manipulated.
Remember creating too many indexes on a table can
slow down your index-related operations to multi-
fold.

That’s all for this article. Hope you enjoyed it !!!

Follow Vikram Gupta for Similar Content.


You may like to read similar SQL Interview Questions.

1. Know the Difference between TRUNCATE, DELETE, and DROP

Know the Difference between TRUNCATE, DELETE, and DROP


Most commonly asked interview questions in SQL.
levelup.gitconnected.com

2. Group by and Having Clauses in SQL

Group by and Having Clauses in SQL


Often used clauses with aggregate functions in SQL statements.
medium.com

3. Views in SQL — Create, Update, and Drop

Views in SQL — Create, Update, and Drop


A virtual table based on the result set of an SQL statement.
medium.com

4. Indexes When to Use and When to Avoid In SQL.

Indexes When to Use and When to Avoid In SQL.


Improve the performance of your database by knowing the concept
of indexes.
levelup.gitconnected.com

Index Sql Indexing Databse Sql Programming


Written by BaseCS101 Follow

1.6K Followers · Writer for Level Up Coding

-: Empowering Developers to Ace Their Technical Interviews :-

More from BaseCS101 and Level Up Coding

BaseCS101 Alexander Nguyen in Level Up Coding

Spring Boot @Async Annotation — The resume that got a software


Make API Calls Asynchronous engineer a $300,000 job at Google.
Build Non-blocking Asynchronous 1-page. Well-formatted.
applications that are highly performant

Dec 12, 2023 165 2 Jun 1 23K 456

Jayden Levitt in Level Up Coding BaseCS101 in Javarevisited

Cathie Woods Tesla Prediction Will A Complete Guide on


Transform Your View of Elon Musk. ExecutorService In Java
Some people are about to eat their words if Executor, ExecutorService, and
her research holds up. ThreadPoolExecutor, factory methods of…

Sep 16 1.95K 105 Feb 25, 2023 62 1

See all from BaseCS101 See all from Level Up Coding

Recommended from Medium


Alexander Nguyen in Level Up Coding Mehmet Ali TOR

The resume that got a software Best Missing Data Analysis


engineer a $300,000 job at Google. Methods
1-page. Well-formatted. Missing data is frequently encountered in
data sets obtained from real life. Since…

Jun 1 23K 456 Aug 20 25

Lists

General Coding Knowledge Coding & Development


20 stories · 1604 saves 11 stories · 825 saves

Stories to Help You Grow as a ChatGPT


Software Developer 21 stories · 816 saves
19 stories · 1385 saves

Choco in JavaScript in Plain English Ashish Pratap Singh in AlgoMaster.io

Mastering Promises: 8 Advanced How I Mastered Data Structures


Tips and Techniques and Algorithms
Unlock the Full Potential of JavaScript Getting good at Data Structures and
Promises with Advanced Strategies and Pro… Algorithms (DSA) helped me clear interview…

Jan 5 2.2K 35 Jul 23 1.8K 17

Tessa Schlesinger Global Atheist A… in Tessa’s W… Anwesha Bhattacharya

How Do You Recognize Someone 17 ways to manage Anxiety:


Truly Intelligent? What is anxiety?
There is only one characteristic that
separates them from others.

Apr 21 11.5K 251 Jul 15 91 1

See more recommendations

You might also like