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

Day 10 SQL Notes Basic To Advanced (SQL Indexes)

Uploaded by

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

Day 10 SQL Notes Basic To Advanced (SQL Indexes)

Uploaded by

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

Day 10: SQL Notes Basic to Advanced

Todays Topic: SQL Indexes


Types of SQL Indexes
Creating Indexes
Dropping Indexes
Clustered vs. Non-Clustered Indexes

SQL indexes are database objects that improve the


speed of data retrieval operations on a table at the
cost of additional storage and maintenance overhead.

They work similarly to indexes in books, where they


help you quickly locate specific information without
scanning the entire content.

Improves query performance by speeding up data


retrieval.
Creates a data structure to quickly locate rows in
a table.
Can be applied to one or more columns in a table.
Helps in optimizing search operations, but may
impact write performance.
10.1 Types of Indexes:
Clustered Index: Determines the physical order of
data in the table. A table can have only one clustered
index. The primary key is usually a clustered index by
default.

Non-Clustered Index: Contains a separate structure


from the table's data, with pointers to the actual data
rows. A table can have multiple non-clustered indexes.

Unique Index: Ensures that all values in the index key


are unique. It is often used to enforce uniqueness
constraints on columns.

Composite Index: An index on multiple columns, which


helps in queries that filter or sort based on those
columns.

Full-Text Index: Used for efficient text searches


within large text columns, such as finding words or
phrases in a TEXT field.

Spatial Index: Used for spatial data types in


databases that support geographic data, enabling fast
querying of spatial information.
10.2 Creating Indexes:
Creating indexes in SQL involves defining an index on
one or more columns of a table to improve query
performance. Here’s a step-by-step guide to creating
various types of indexes:

1. Basic Index Creation


To create a basic index on a single column, use the following
syntax:

Example:

2. Unique Index
A unique index ensures that all values in the indexed
column(s) are unique. It is often used to enforce
uniqueness constraints.
Syntax:
10.2 Creating Indexes:
Creating indexes in SQL involves defining an index on
one or more columns of a table to improve query
performance. Here’s a step-by-step guide to creating
various types of indexes:

1. Basic Index Creation


To create a basic index on a single column, use the following
syntax:

Example:

2. Unique Index
A unique index ensures that all values in the indexed
column(s) are unique. It is often used to enforce
uniqueness constraints.
Syntax:
Example:

3. Composite Index
A composite index (or multi-column index) is created on
multiple columns. It is useful for queries that filter or
sort on multiple columns.

Syntax:

Example:

4. Clustered Index
A clustered index determines the physical order of data
in the table. A table can have only one clustered index.

Syntax:
Example:

Note: In some databases like MySQL, the primary key


is automatically used as the clustered index if you don’t
explicitly define one.

5. Full-Text Index
A full-text index is used for efficient searching of
text within large text columns.
SQL Syntax (SQL Server):

Example:

6. Spatial Index
A spatial index is used for spatial data types to enable
fast querying of geographic data.
SQL Syntax (SQL Server):

Example:

10.3 Dropping an Index


Dropping indexes is a straightforward process, but it's
important to understand the implications on your database
performance and structure. Here’s how you can drop
indexes in various SQL databases:
Syntax:

Example:

Considerations Before Dropping an Index

Impact on Performance: Dropping an index can slow down


queries that used it.

Dependencies: Verify the index is not part of a primary


key or unique constraint.

Testing: Test performance changes in a staging


environment first.
10.4 Clustered vs. Non-Clustered Indexes

Aspect Clustered Index Non-Clustered Index

Determines the physical order of Creates a separate structure


Definition
data in the table. from the table's data.

Only one clustered index per table Multiple non-clustered indexes


Number per Table
is allowed. can be created on a table.

Data rows are stored in the order Indexes are stored separately
Data Storage
of the clustered index key. from the data rows.

The index is the actual table with The index contains pointers to
Index Structure
sorted data. the actual data rows.

Typically used on primary keys or Can be used on any column(s) to


Key Usage
unique columns. improve query performance.

Efficient for range queries and Efficient for exact matches


Performance
sorting operations. and queries involving joins.

Alters the physical arrangement Does not alter the physical


Impact on Data
of the data on disk. arrangement of the data.

Often faster for queries involving Speeds up searches, especially


Query Speed
sorting and range queries. for columns frequently queried.

Storage Requires additional storage for


Can lead to fragmentation of data.
Overhead the index structure.

Maintenance involves updating


Updates to the index can be
Maintenance the index separately from the
costly in terms of performance.
data.

Best for columns frequently


Best for columns frequently used
Typical Use Case used in search conditions or
in sorting or range queries.
joins.
SQL Has Been Completed
Total 95 Pages
From Tomorrow I’ll Post Python Notes Series

You might also like