JBs Wiki

Menu

Skip to content
  • Home
  • SQL Server Blogs
  • YouTube Videos

Tag Archives: SQL Server Tutorials

Standard

Posted by

Vivek Janakiraman

Posted on

July 28, 2024

Posted under

SQL Server 2022

Comments

Leave a comment

SQL Server 2022: Intelligent Query Processing Enhancements

SQL Server 2022 has ushered in a new era of database optimization, thanks to the remarkable advancements in Intelligent Query Processing (IQP). These enhancements are designed to automatically improve the performance of queries, making it easier to manage and optimize databases without extensive manual intervention. In this blog, we will delve into the latest IQP features, providing detailed explanations, examples, and code snippets to demonstrate their impact on query performance. Let’s explore these game-changing features! 🌟

1. Parameter Sensitive Plan Optimization (PSPO) πŸ”„

One of the common challenges in SQL Server query performance is parameter sniffing, where the query optimizer generates an execution plan based on the first parameter value it encounters. This can lead to suboptimal plans for subsequent executions with different parameter values. SQL Server 2022 introduces Parameter Sensitive Plan Optimization (PSPO) to address this issue.

How PSPO Works: PSPO allows SQL Server to create multiple execution plans for a single query, each tailored to different parameter values. This ensures that the most efficient plan is used for each specific scenario.

Example:

Consider the following query that retrieves orders based on CustomerID:

SELECT OrderID, OrderDate, TotalAmount
FROM Orders
WHERE CustomerID = @CustomerID;

Without PSPO, SQL Server might create a plan optimized for a specific CustomerID, potentially causing poor performance for other customers with different data distributions. With PSPO, SQL Server can store multiple plans, ensuring optimal performance across different parameter values.

2. Degree of Parallelism Feedback (DOP Feedback) πŸ”’

The Degree of Parallelism (DOP) determines how many threads SQL Server uses to execute a query. Incorrect DOP settings can lead to inefficient CPU usage and poor query performance. SQL Server 2022 introduces DOP Feedback, which dynamically adjusts the DOP based on actual runtime conditions.

How DOP Feedback Works: DOP Feedback monitors the performance of parallel queries and adjusts the DOP for subsequent executions, aiming to find the most efficient level of parallelism.

Example:

SELECT ProductID, SUM(Quantity) AS TotalQuantity
FROM Sales
GROUP BY ProductID;

For a query like this, SQL Server may initially choose a DOP based on its estimates. If the initial DOP is too high or too low, resulting in suboptimal performance, DOP Feedback will adjust it in future executions, optimizing both CPU utilization and query execution time.

3. Memory Grant Feedback (Persisted) 🧠

Memory Grant Feedback was first introduced in SQL Server 2017 to adjust memory grants dynamically based on actual execution requirements. SQL Server 2022 enhances this feature with persistence, meaning the adjustments are retained across query executions, even if the server restarts.

How Memory Grant Feedback (Persisted) Works: If a query is granted too much or too little memory, it can lead to resource contention or wasted resources. The persisted Memory Grant Feedback feature adjusts the memory grant size based on the query’s actual memory usage.

Example:

SELECT CustomerID, COUNT(*) AS OrderCount
FROM Orders
GROUP BY CustomerID;

If this query initially receives a larger memory grant than necessary, it may lead to wasted resources. Memory Grant Feedback will reduce the grant for future executions, improving resource utilization.

4. Query Store Hints 🎯

Query Store Hints provide a mechanism to apply hints to specific queries without modifying the source code. This feature is particularly useful for optimizing third-party applications or legacy code where you cannot directly alter the SQL statements.

How Query Store Hints Work: You can use Query Store Hints to influence the optimizer’s choices, such as forcing the use of a specific index or join type, without changing the application code.

Example:

Imagine you have a query that benefits from using a specific index:

SELECT * FROM Products
WHERE ProductName = 'Gadget';

You can apply a hint to force the use of a particular index:

EXEC sp_query_store_set_hints @query_id = 1, @hints = 'OPTION (FORCESEEK)';

This ensures that the query optimizer selects the most efficient execution plan, improving query performance.

5. Intelligent Query Processing (IQP) Mode πŸš€

The IQP mode in SQL Server 2022 includes several key features that collectively enhance query performance. By enabling IQP mode, you automatically gain access to a suite of optimizations, including Adaptive Joins, Batch Mode on Rowstore, and more.

Key Features in IQP Mode:

  • Adaptive Joins: Dynamically choose between different join strategies based on runtime conditions.
  • Batch Mode on Rowstore: Use batch processing for rowstore data, significantly improving performance for certain workloads.
  • Approximate Query Processing: Speed up queries with approximate results where absolute precision is not required.

Enabling IQP Mode:

To enable IQP mode, use the following T-SQL commands:

ALTER DATABASE [YourDatabaseName] SET QUERY_STORE = ON;
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
ALTER DATABASE SCOPED CONFIGURATION SET INTERLEAVED_EXECUTION_TVF = ON;
ALTER DATABASE SCOPED CONFIGURATION SET BATCH_MODE_ADAPTIVE_JOINS = ON;
ALTER DATABASE SCOPED CONFIGURATION SET BATCH_MODE_ON_ROWSTORE = ON;

This configuration ensures that your database leverages the latest IQP features, providing significant performance improvements for complex queries.

Conclusion πŸŽ‰

SQL Server 2022’s Intelligent Query Processing enhancements represent a significant leap forward in database performance optimization. Whether dealing with parameter-sensitive queries, complex joins, or memory-intensive operations, these features provide automated, data-driven solutions that enhance efficiency and performance.

By adopting these advancements, you can ensure that your SQL Server environment operates at peak performance, with minimal manual intervention. Explore these features in your SQL Server 2022 deployment and experience the transformative impact of Intelligent Query Processing! πŸš€βœ¨

For more tutorials and tips on SQL Server, including performance tuning and database management, be sure to check out our JBSWiki YouTube channel.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided β€œAS IS” with no warranties, and confers no rights.

Standard

Posted by

Vivek Janakiraman

Posted on

July 23, 2024

Posted under

Core

Comments

Leave a comment

How to List All Covering Indexes and Columns in a SQL Server Database

Covering indexes in SQL Server are powerful tools that can significantly improve query performance by covering all the columns required by a query. This means the index includes all the columns referenced in the query, either as key columns or as included columns, so the query can be resolved without accessing the table or clustered index. In this blog, we will explore how to list all covering indexes and the columns they cover in a SQL Server database.

Understanding Covering Indexes

A covering index is an index that includes all the columns needed to satisfy a particular query. By having a covering index, SQL Server can retrieve the necessary data directly from the index without having to access the base table, which can greatly enhance query performance.

Listing Covering Indexes and Columns

To list all covering indexes and the columns they cover, including the included columns, you can use SQL Server’s system catalog views. The relevant views include sys.indexes, sys.index_columns, sys.columns, and sys.tables.

Here is a detailed query to achieve this:

WITH KeyColumns AS (
    SELECT 
        i.object_id,
        i.index_id,
        t.name AS TableName,
        i.name AS IndexName,
        i.type_desc AS IndexType,
        i.is_unique AS IsUnique,
        i.is_primary_key AS IsPrimaryKey,
        STRING_AGG(c.name, ', ') WITHIN GROUP (ORDER BY ic.key_ordinal) AS KeyColumns
    FROM 
        sys.indexes AS i
        INNER JOIN sys.tables AS t
            ON i.object_id = t.object_id
        LEFT JOIN sys.index_columns AS ic
            ON i.object_id = ic.object_id
            AND i.index_id = ic.index_id
            AND ic.is_included_column = 0
        LEFT JOIN sys.columns AS c
            ON ic.object_id = c.object_id
            AND ic.column_id = c.column_id
    WHERE 
        i.type_desc IN ('CLUSTERED', 'NONCLUSTERED')
    GROUP BY 
        i.object_id, i.index_id, t.name, i.name, i.type_desc, i.is_unique, i.is_primary_key
),
IncludedColumns AS (
    SELECT 
        i.object_id,
        i.index_id,
        STRING_AGG(c.name, ', ') WITHIN GROUP (ORDER BY ic.index_column_id) AS IncludedColumns
    FROM 
        sys.indexes AS i
        LEFT JOIN sys.index_columns AS ic
            ON i.object_id = ic.object_id
            AND i.index_id = ic.index_id
            AND ic.is_included_column = 1
        LEFT JOIN sys.columns AS c
            ON ic.object_id = c.object_id
            AND ic.column_id = c.column_id
    WHERE 
        i.type_desc IN ('CLUSTERED', 'NONCLUSTERED')
    GROUP BY 
        i.object_id, i.index_id
)
SELECT 
    kc.TableName,
    kc.IndexName,
    kc.IndexType,
    kc.IsUnique,
    kc.IsPrimaryKey,
    kc.KeyColumns,
    ic.IncludedColumns
FROM 
    KeyColumns AS kc
    LEFT JOIN IncludedColumns AS ic
        ON kc.object_id = ic.object_id
        AND kc.index_id = ic.index_id
ORDER BY 
    kc.TableName, kc.IndexName;

Using SQL Server Management Studio (SSMS)

If you prefer a graphical interface, SQL Server Management Studio (SSMS) provides a way to view indexes and their columns:

  1. Open SSMS and connect to your database.
  2. Expand the database in the Object Explorer.
  3. Navigate to the “Tables” folder and expand it.
  4. Expand the specific table you are interested in.
  5. Expand the “Indexes” folder under the table to see all indexes.
  6. Right-click on an index and select “Properties”.
  7. In the Index Properties dialog, go to the “Included Columns” page to see the included columns.

Conclusion

Listing all covering indexes and the columns they cover in a SQL Server database can be accomplished through querying system catalog views or using SQL Server Management Studio. Understanding and managing covering indexes are crucial for optimizing query performance and ensuring efficient data retrieval.

For more tutorials and tips on SQL Server, including performance tuning and database management, be sure to check out our JBSWiki YouTube channel.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided β€œAS IS” with no warranties, and confers no rights.

Standard

Posted by

Vivek Janakiraman

Posted on

July 23, 2024

Posted under

Core

Comments

Leave a comment

How to List All Non-Clustered Indexes in a SQL Server Database

Non-clustered indexes are a critical component in optimizing the performance of queries in SQL Server. Unlike clustered indexes, which determine the physical order of data in a table, non-clustered indexes create a separate structure that points to the data. In this blog, we’ll explore how to list all non-clustered indexes in a SQL Server database using different methods.

Understanding Non-Clustered Indexes

A non-clustered index does not alter the physical order of the data within the table. Instead, it creates a structure that holds the non-clustered index key values and pointers to the data rows that contain the key value. Non-clustered indexes are useful for improving the performance of queries that do not modify the data or require sorting and filtering based on non-primary key columns.

Listing Non-Clustered Indexes

To list all non-clustered indexes in a SQL Server database, you can use several methods. Here are the most common approaches:

  1. Using INFORMATION_SCHEMA Views
  2. Using System Catalog Views
  3. Using SQL Server Management Studio (SSMS)

Using INFORMATION_SCHEMA Views

While INFORMATION_SCHEMA views provide a standardized way to access metadata about database objects, system catalog views are generally more detailed for indexing information. However, here is a basic query using INFORMATION_SCHEMA views:

SELECT 
    tc.TABLE_NAME,
    tc.CONSTRAINT_NAME
FROM 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
WHERE 
    tc.CONSTRAINT_TYPE = 'UNIQUE'
ORDER BY 
    tc.TABLE_NAME, tc.CONSTRAINT_NAME;

Using System Catalog Views

System catalog views are more comprehensive for retrieving detailed information about non-clustered indexes. The relevant views include sys.indexes, sys.tables, and sys.columns.

SELECT 
    t.name AS TableName,
    i.name AS IndexName,
    c.name AS ColumnName
FROM 
    sys.indexes AS i
    INNER JOIN sys.index_columns AS ic
        ON i.object_id = ic.object_id
        AND i.index_id = ic.index_id
    INNER JOIN sys.columns AS c
        ON ic.object_id = c.object_id
        AND ic.column_id = c.column_id
    INNER JOIN sys.tables AS t
        ON i.object_id = t.object_id
WHERE 
    i.type = 2 -- 2 indicates a non-clustered index
ORDER BY 
    t.name, i.name, c.name;

Using SQL Server Management Studio (SSMS)

If you prefer a graphical interface, SQL Server Management Studio (SSMS) provides an easy way to view non-clustered indexes:

  1. Open SSMS and connect to your database.
  2. Expand the database in the Object Explorer.
  3. Navigate to the “Tables” folder and expand it.
  4. Expand the specific table you are interested in.
  5. Expand the “Indexes” folder under the table to see all indexes, including non-clustered indexes. Non-clustered indexes are typically indicated without the key icon used for clustered indexes.

Conclusion

Listing all non-clustered indexes in a SQL Server database can be accomplished through various methods, including querying system catalog views or using SQL Server Management Studio. Understanding and managing non-clustered indexes are vital for optimizing query performance and ensuring efficient data retrieval.

For more tutorials and tips on SQL Server, including performance tuning and database management, be sure to check out our JBSWiki YouTube channel.

Thank You,
Vivek Janakiraman

Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided β€œAS IS” with no warranties, and confers no rights.

Post navigation

← Older posts
Newer posts →
  • LinkedIn
  • Instagram
  • Twitter
  • Facebook
  • Mail

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 41 other subscribers
Advertisements
Advertisements
Advertisements
Advertisements
Advertisements
Powered by WordPress.com.
JBs Wiki
Vivek Janakiraman / Proudly powered by WordPress Theme: Zoren.
Loading Comments...