Improving SQL Query Performance : Indexes

 


An index is analogous to the Table of Contents in a book. 


Prior to exploring what indexes entail, I'd like to begin by discussing 'what SQL is.'

SQL, often pronounced as 'sequel,' stands for Structured Query Language

It is known for being declarative, enabling you to specify what you want without needing to focus on how to achieve it.

However, beyond your initial query (SELECT * FROM menu WHERE food = 'burger'), the computer still needs to know how to go about finding those burgers, and that's where an Execution Plan comes into play.

An SQL Execution Plan is a detailed outline or strategy devised by the database management system (DBMS) to execute a query efficiently, specifying the steps required such as scanning, filtering, and joining data to retrieve the requested information.

Now, imagine scanning a table like flipping through pages in a book – the more pages, the longer it takes. So, keep in mind that scanning time is directly proportional to the table's row count.

As someone that finds themselves writing SQL queries to generate reports frequently, there are often times when a "simple query" may take a long time to be executed. 

Enter indexing – the superhero that rescues you from full table scans! 

It's like having a GPS for your data, speeding up searches by pointing directly to what you are looking for. 

With that being said here are some other features you need to know about indexes before using them:


Types of Indexes 

Various types of indexes exist (e.g., B-tree, Hash, Bitmap), each suitable for different scenarios. Understanding the data and query patterns helps in choosing the appropriate index type for optimization.


Trade-Offs with Write Operations

While indexes speed up read operations, they might slightly slow down write operations (inserts, updates, deletes). Every time data is modified, indexes might need to be updated accordingly, impacting write performance.


Index Maintenance

Regular maintenance of indexes is necessary for optimal performance. Over time, indexes might become fragmented, impacting query speed. Rebuilding or reorganizing indexes can help maintain efficiency.


Impact on Execution Plans

Indexes influence the query execution plan. Sometimes, the query optimizer might not use an index even if available, leading to unexpected performance outcomes. Understanding how the optimizer works is crucial.



Resources

Types of Indexes

LinkedIn Course: Advanced SQL for Query Tuning and Performance Optimization

Video: Query Execution Order


Comments

Popular posts from this blog

Missing Data : What to Do?

Prompt Engineering : An Introduction

Upskilling: Certificates vs. Certifications

Women In STEM : Challenges and Advantages

SQL Server Reporting Services vs. Power BI

5 Authentication Methods

There Has Been a Data Breach: Now What?

Inductive and Deductive Reasoning

Don't Be Bland : Spice Up Your Personal Brand