This series of articles will discuss SQL server performance. In the title, I use SQL Performance because the articles could include SQL statement (Query) optimization and also include SQL Server performance issues, and probably include stored procedure performance issues.
- SQL Performance (0), Summary
- SQL Performance (1), Optimized SELECT Query (A) - this article
- SQL Performance (2), Optimized SELECT Query (B)
- SQL Performance (3), Optimized Data Modifying Query
- SQL Performance (4), Tune the SQL Server Database
Introduction
Performance tuning SELECT statements can be a time-consuming task that follows the Pareto principle’s 80/20 rule: 20% effort is likely to give you an 80% performance improvement. To get another 20% performance improvement you probably need to spend 80% of the time.
In this article, we will emphasize the 20% effort with 80% benefits to improve performance before we move to query plans and some complicated or detailed issues that I leave to the second article, SQL Performance (2), Optimized SELECT Query (B). In this way, it will be easier for both me and the readers to follow.
So, this article will be the easy part, but the important part for tuning the SQL query to improve the performance.
The Causes affecting query run time
Obviously, the factors affecting query run time are the size of the query return and the complexity of the query,
- Size
The larger the table (more columns), the longer the records (more rows), and the worse the query performance will be. - Complexity
The more complex the query, the worse the query performance will be. Some major factors for query complexity as,- Joins
If your joins substantially increase the row count of the result set, your query is likely to be slow. - Aggregations
Combining multiple rows to produce a result requires more computation than simply retrieving those rows.
- Joins
The Major Improvement for Tuning Query
Obviously reducing the size and avoiding the complexities of the query will be the solutions,
- Reduce Size. Limit the Size of the query Set
- Limit Columns: Only Select Fields You Need
- Limit Rows: Select Reasonable Record length
- Remove Unnecessary Tables in the query
- Remove OUTER JOINS
- Enforce Indexes to Improve Performance
- Make indexes on all fields used in the WHERE and JOIN portions of the SQL statement
- Remove Calculated Fields in JOIN and WHERE Clauses
- Reduce Complexity. will be discussed in detail in SQL Performance (2), Optimized SELECT Query (B)
- Run your query during off-peak hours
- Selecting from large tables (>1,000,000 records)
- Cartesian Joins or CROSS JOINs
- Looping statements
- SELECT DISTINCT statements
- Nested subqueries
- Wildcard searches in long text or memo fields
- Multiple schema queries
- Run your query during off-peak hours

Join the conversation! Your thoughts help the community grow.