In the dynamic realm of database management, monitoring and optimizing query performance is paramount. SQL Server offers the Query Store feature, a powerful tool that captures and stores detailed information about query execution plans and runtime statistics. In this blog post, we’ll delve into leveraging T-SQL to extract valuable insights from the Query Store, focusing on queries executed within the last 7 days.
Query Store Essentials
Before we dive into T-SQL scripts, let’s briefly revisit the Query Store. It acts as a repository for query-related data, allowing database administrators to analyze historical performance metrics and troubleshoot issues efficiently.
Comprehensive Query Analysis in the Last 7 Days
To get a comprehensive view of queries executed in the last 7 days, we can use the following T-SQL script:
-- T-SQL script to analyze queries executed in the last 7 days
SELECT p.query_id query_id, q.object_id object_id, ISNULL(OBJECT_NAME(q.object_id),'') object_name, qt.query_sql_text query_sql_text, ROUND(CONVERT(float, SUM(rs.avg_duration*rs.count_executions))*0.001,2) total_duration, ROUND(CONVERT(float, SUM(rs.avg_cpu_time*rs.count_executions))*0.001,2) total_cpu_time, ROUND(CONVERT(float, SUM(rs.avg_logical_io_reads*rs.count_executions))*8,2) total_logical_io_reads, ROUND(CONVERT(float, SUM(rs.avg_physical_io_reads*rs.count_executions))*8,2) total_physical_io_reads, ROUND(CONVERT(float, SUM(rs.avg_logical_io_writes*rs.count_executions))*8,2) total_logical_io_writes, SUM(rs.count_executions) count_executions, COUNT(distinct p.plan_id) num_plans FROM sys.query_store_runtime_stats rs JOIN sys.query_store_plan p ON p.plan_id = rs.plan_id JOIN sys.query_store_query q ON q.query_id = p.query_id JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id WHERE rs.first_execution_time BETWEEN (getdate()-7) AND (getdate()) GROUP BY p.query_id, qt.query_sql_text, q.object_id HAVING COUNT(distinct p.plan_id) >= 1 ORDER BY count_executions DESC
This script provides a detailed breakdown of query performance, including execution counts, total duration, CPU time, and more, for queries executed in the last 7 days.
Specific Query Search Example
Suppose you want to focus on queries containing a specific keyword within the last 7 days. The following T-SQL script demonstrates how to achieve this:
-- T-SQL script to search for a specific query in the last 7 days
SELECT p.query_id query_id, q.object_id object_id, ISNULL(OBJECT_NAME(q.object_id),'') object_name, qt.query_sql_text query_sql_text, ROUND(CONVERT(float, SUM(rs.avg_duration*rs.count_executions))*0.001,2) total_duration, ROUND(CONVERT(float, SUM(rs.avg_cpu_time*rs.count_executions))*0.001,2) total_cpu_time, ROUND(CONVERT(float, SUM(rs.avg_logical_io_reads*rs.count_executions))*8,2) total_logical_io_reads, ROUND(CONVERT(float, SUM(rs.avg_physical_io_reads*rs.count_executions))*8,2) total_physical_io_reads, ROUND(CONVERT(float, SUM(rs.avg_logical_io_writes*rs.count_executions))*8,2) total_logical_io_writes, SUM(rs.count_executions) count_executions, COUNT(distinct p.plan_id) num_plans FROM sys.query_store_runtime_stats rs JOIN sys.query_store_plan p ON p.plan_id = rs.plan_id JOIN sys.query_store_query q ON q.query_id = p.query_id JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id WHERE rs.first_execution_time BETWEEN (getdate()-7) AND (getdate()) and qt.query_sql_text LIKE 'DELETE FROM%' GROUP BY p.query_id, qt.query_sql_text, q.object_id HAVING COUNT(distinct p.plan_id) >= 1 ORDER BY count_executions DESC
This example narrows down the search to queries with the keyword “DELETE FROM” executed in the last 7 days.
Conclusion
In conclusion, harnessing the power of T-SQL with the SQL Server Query Store provides a robust way to analyze and optimize query performance. The provided scripts offer a starting point for gaining valuable insights into the queries executed in the last 7 days, enabling proactive database management and performance tuning.
Stay tuned for more in-depth explorations of SQL Server features and best practices!
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.
SQL Server 2022, is packed with a plethora of new features and improvements that promise to take the performance, security, and productivity of your database to new heights. In this article, we will explore the various enhancements and upgrades that SQL Server 2022 has to offer.
Explanation of SQL Server 2022 and its Significance for Database Performance
SQL Server 2022 is the latest version of Microsoft’s popular relational database management system. It has been designed to improve the speed, security, and scalability of your database. This version offers significant enhancements that make it an excellent choice for organizations that require a high-performance database platform. With the use of persistent memory, batch mode enhancements, improved indexing, and query execution, in-memory OLTP improvements, and many more, SQL Server 2022 offers a significant performance boost.
Brief History of SQL Server and Its Evolution
SQL Server has come a long way since its inception in 1989. It has been continually updated to keep up with the changing needs of the industry. Over the years, it has become one of the most popular database management systems in the world, offering reliable and robust performance to millions of users. The latest version of SQL Server 2022 is a testament to the continuous evolution and innovation of this powerful platform.
Performance Enhancements in SQL Server 2022
SQL Server 2022 offers a host of performance enhancements that make it faster, more efficient, and more scalable than ever before. Some of the most significant improvements include the use of persistent memory, batch mode over row mode enhancements, improved indexing and query execution, and in-memory OLTP improvements.
Persistent memory is a new feature that allows you to use non-volatile memory devices as part of your database storage. This feature provides faster access to your data and reduces I/O overhead, resulting in a significant performance boost. Batch mode over row mode enhancements provide better query performance by processing large data sets in batches instead of one row at a time. Improved indexing and query execution improve the overall speed of your queries, resulting in faster response times. In-memory OLTP improvements increase the performance of your database by providing better concurrency control, reduced locking, and higher transaction throughput.
Security Features in SQL Server 2022
Security is a top concern for any database management system, and SQL Server 2022 offers several new features to keep your data safe. These features include trusted enclave technology for secure enclaves, always encrypted feature for SQL Server, and row-level security enhancements.
Trusted enclave technology provides a secure environment for running code and executing sensitive operations. It is designed to protect your data against malware, rootkits, and other types of attacks. Always encrypted feature provides enhanced security by encrypting data at rest and in transit, ensuring that your sensitive data remains safe from prying eyes. Row-level security enhancements provide fine-grained control over access to your data, allowing you to restrict access to specific rows or columns based on user roles and permissions.
High Availability and Disaster Recovery
High availability and disaster recovery are critical considerations for any database management system. SQL Server 2022 offers several new features that improve these areas, including improved failover clusters and availability groups, resumable online index rebuild, and enhancements to backup and restore.
Improved failover clusters and availability groups provide better resiliency and faster failover times, ensuring that your database remains available in the event of a failure. Resumable online index rebuild allows you to pause and resume index rebuild operations, reducing downtime and minimizing disruption to your users. Enhancements to backup and restore provide faster and more reliable backup and restore times, reducing the risk of data loss in the event of a disaster.
Query Processing and Analytical Features
SQL Server 2022 introduces new analytical features that provide advanced capabilities for processing complex data and performing sophisticated analyses. With enhanced features for graph processing, users can now easily analyze large amounts of data and gain deeper insights into their business operations.
In addition to the improved graph processing capabilities, SQL Server 2022 also offers new machine learning capabilities that allow users to train and deploy machine learning models directly in the database. This provides users with a seamless experience for running machine learning workloads alongside traditional database workloads.
SQL Server 2022 also includes enhancements to query processing and performance. With improvements in the core engine, queries can be executed faster and with greater efficiency. This is achieved through the use of intelligent query optimization techniques that analyze query patterns and data statistics to optimize query performance.
Data Virtualization and Integration:
Data virtualization is a critical aspect of modern data management, and SQL Server 2022 offers several improvements in this area. With improvements to PolyBase, users can easily integrate data from a wide range of external sources into their SQL Server databases. This includes data stored in Hadoop clusters, Azure Blob Storage, and other cloud-based data sources.
SQL Server 2022 also includes enhancements to data integration with Azure Synapse Analytics. With improved integration capabilities, users can easily move data between their SQL Server databases and their Azure Synapse Analytics workspaces. This makes it easier to perform advanced analytics on large amounts of data.
Developer Features:
SQL Server 2022 introduces several new developer features that provide enhanced support for developing and deploying applications on the SQL Server platform. With enhancements to JSON support, users can now store and manipulate JSON data directly in the database. This allows for greater flexibility when working with semi-structured data.
SQL Server 2022 also includes enhancements to temporal tables and query optimization. Temporal tables allow users to store historical data in a database table, making it easy to perform analysis on historical trends. Query optimization enhancements provide greater performance for complex queries, making it easier for developers to work with large datasets.
Tools and Management:
SQL Server 2022 includes several new tools and management features that make it easier to manage SQL Server environments. With an enhanced SQL Server Management Studio (SSMS), users can perform tasks such as database backup and restore, query execution, and other management tasks with greater ease.
In addition to SSMS, SQL Server 2022 also includes new Azure Data Studio features. This provides users with a modern, cross-platform tool for managing SQL Server environments. Enhanced SQL Server Integration Services (SSIS) provides users with greater flexibility and control over their data integration workflows.
Advantages of Upgrading to SQL Server 2022:
Upgrading to SQL Server 2022 provides numerous benefits to users. With increased speed, efficiency, and productivity, users can achieve greater performance and scalability with their databases. Improved security and disaster recovery features provide greater protection for critical data, and enhanced analytics and reporting capabilities make it easier to extract insights from data.
The improved developer productivity provided by SQL Server 2022 allows for faster application development and deployment, and the new tools and management features make it easier to manage SQL Server environments.
How to Upgrade to SQL Server 2022:
Before upgrading to SQL Server 2022, it is important to plan for the upgrade process. Best practices for upgrading include conducting a thorough evaluation of your existing database infrastructure, creating a detailed upgrade plan, and performing thorough testing and troubleshooting after the upgrade is complete.
Migrating your data to SQL Server 2022 can be accomplished through a variety of methods, including backup and restore, detach and attach, or using the Data Migration Assistant (DMA) tool. Once the upgrade is complete, it is important to perform post-upgrade testing and troubleshooting to ensure that the upgraded database is functioning correctly.
Post-upgrade testing and troubleshooting involves performing a series of tests to validate the functionality of the upgraded database. The tests should cover the full range of database functionality, including querying, indexing, security, and disaster recovery. The goal of these tests is to ensure that the upgraded database is functioning correctly and that there are no issues that could impact performance or stability.
If issues are discovered during testing, troubleshooting should begin immediately. Troubleshooting can involve a variety of activities, including reviewing logs, checking system configurations, and working with Microsoft support to resolve any issues. It is important to resolve any issues quickly to minimize the impact on business operations.
In conclusion, upgrading to SQL Server 2022 offers many benefits, including increased speed, improved security, and enhanced analytics and reporting capabilities. With careful planning and execution, upgrading to SQL Server 2022 can be a seamless process that delivers immediate and long-term benefits to your organization. By following best practices for upgrading, migrating your data, and performing post-upgrade testing and troubleshooting, you can ensure that your upgraded database is functioning correctly and that you are taking full advantage of the new features and capabilities available in SQL Server 2022.
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.
In this blog post, we will discuss an issue encountered with an Always On Availability Group where a database remained stuck in the Initializing / In Recovery state on the secondary replica after a failover. We will explore the steps taken to resolve the problem and provide insights into the underlying causes.
Issue
During a failover from the primary to the secondary replica in an Always On Availability Group, one particular database entered the Initializing / In Recovery state on the secondary replica. Despite restarting SQL services and attempting to suspend data movement, the database remained stuck in this state.
Resolution
Logging off users and restarting SQL services: Initially, all users were logged off from the secondary replica, and the SQL services on the replica were restarted. However, these steps did not resolve the issue.
Suspending data movement: The next approach involved suspending the data movement of the problematic database from the SQL Server Management Studio on the secondary replica. However, this operation resulted in a “lock request time out period exceeded” error.
Identifying the blocking process: To investigate further, a query was executed to identify any blocking processes using the following query: SELECT * FROM sysprocesses WHERE blocked > 0 It was discovered that the DB Startup thread for the specific database was blocking the SPID attempting to suspend data movement.
Checking recovery status: The SQL Server error log was examined to determine if the database’s recovery had completed. The log indicated that recovery for the database had finished in 43 seconds, with no user action required. Recovery completed for database JB_DB (database ID 7) in 43 second(s) (analysis 64 ms, redo 324 ms, undo 0 ms.) This is an informational message only. No user action is required.
Analyzing DB Startup thread wait type: Monitoring the DB Startup thread revealed that it was in a wait state with the HADR_RECOVERY_WAIT_FOR_UNDO type. This wait type occurs when the secondary database is waiting for the reverting and initializing phases to synchronize with the primary database after failover.
Monitoring undo progress: The “Log remaining for undo” performance monitor counter was used to track the amount of log in kilobytes remaining for the undo phase. Surprisingly, the counter showed 0, indicating that there was nothing to undo, and the recovery completed message in the SQL Server error log confirmed that the undo took 0 milliseconds.
Failing over to another synchronous secondary replica: To further troubleshoot the issue, a failover was performed from the primary replica to another synchronous secondary replica. This time, all databases returned to a synchronized state without any issues.
Restarting SQL Server Instance on Primary Replica if there are no additional synchronous secondary replica: If there are no additional synchronous secondary replica to failover the primary replica, you need to restart the SQL Server Instance on Primary Replica a a workaround for this issue.
Summary
The issue of a database stuck in the Initializing / In Recovery state after an Always On Availability Group failover was resolved by failing over to another synchronous secondary replica. Although the root cause and exact timing of the issue remain unknown, this blog post aims to provide guidance and assistance to others who may encounter similar challenges within the community. This problem has been identified as a bug and is documented here.
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.