Posts

Showing posts with the label Performance Tuning

Read-Ahead Reads in SQL Server

Image
SQL Server is known for its efficient I/O management system. One of the performance-boosting features that often works silently behind the scenes is Read-Ahead Reads. If you've ever wondered how SQL Server optimizes data retrieval before it's even requested by the query engine, read-ahead reads are a key part of the magic. What are Read-Ahead Reads? Read-Ahead Reads refer to the mechanism by which SQL Server proactively fetches data pages from disk into the buffer cache before the query processor actually needs them. This helps minimize latency and improves query performance. In simple terms : SQL Server tries to anticipate what data your query will need and loads it into memory in advance. Why Does SQL Server Use Read-Ahead Reads? Disk I/O is one of the slowest operations in any database system. To reduce query wait times caused by disk reads, SQL Server:      * Pre-fetches data pages that are likely to be needed.      * Loads them into the buffer pool. ...

How data is replicated from the Primary Replica to the Secondary Replica in a SQL Server Always On Availability Group

Image
When it comes to high availability in SQL Server, Always On Availability Groups offer a robust solution for ensuring data resilience and fault tolerance. At the heart of this technology is a sophisticated log-based replication mechanism distinct from traditional database mirroring or snapshot replication. Let’s walk through exactly how data moves from the Primary Replica to the Secondary Replica, step by step. High-Level Process Flow  Primary Replica → Log Generation → Log Transport → Log Reception → Redo → Acknowledgement. Step-by-Step Explanation 1. Client Writes Data to Primary Replica * A client application sends a transaction (e.g., INSERT, UPDATE) to the Primary Replica. * SQL Server writes this transaction to the transaction log of the primary database. * This log write is made to the log buffer and flushed to disk before the transaction is considered committed. 2. Log Block is Captured by the Always On Transport Manager * The HADR_LOG_SEND_MANAGER process monitors the trans...

Degree of Parallelism Feedback in SQL Server 2025

Image
Degree of Parallelism (DOP) feedback is a feature in SQL Server 2025 that automatically tunes the number of threads used for query execution, optimizing performance and resource usage. This feature is now generally available and enabled by default in SQL Server 2025 as well as in Azure SQL Database. How  Degree of Parallelism (DOP) Feedback  Works - Dynamic Adjustment : DOP feedback dynamically adjusts the degree of parallelism for queries based on actual runtime performance metrics such as CPU time and elapsed time. Self-Tuning : If a query consistently underperforms due to excessive parallelism (e.g., too many threads causing overhead), DOP feedback will reduce the DOP for future executions of that specific query. No Manual Tuning Required : This process is automatic, eliminating the need for DBAs to manually adjust parallelism settings for recurring queries. Persistent Feedback : The feedback and adjustments are stored in the Query Store, ensuring that optimizations persist...

Zstandard Backup Compression in SQL Server

Image
Zstandard Backup Compression in SQL Server 2025  In SQL Server 2025, Microsoft has introduced a highly anticipated enhancement - support for the Zstandard (ZSTD) compression algorithm in native database backups. This marks a major leap forward in backup efficiency, delivering faster processing speeds and smaller file sizes without significantly increasing CPU usage. Whether you're managing on-premises SQL Servers or hybrid cloud environments, understanding and implementing Zstandard compression can help you optimize both performance and storage costs. What is Zstandard? Zstandard (ZSTD) is a modern, open-source lossless data compression algorithm developed by Facebook. It’s designed to offer: High compression ratios (comparable to zlib/gzip) Lightning-fast compression and decompression Low memory and CPU usage Unlike legacy algorithms like MS_XPRESS , Zstandard excels at balancing speed and size—making it ideal for high-throughput, enterprise-grade workloads. Why SQL...

Optimized Halloween Protection in SQL Server

Optimized Halloween Protection in SQL Server 2025 The Halloween Problem  is a classic and subtle issue in relational database systems that arises during certain data modification operations—specifically UPDATE, INSERT, DELETE, or MERGE statements. The core of the problem is that, under specific circumstances, a row can be modified more than once during a single statement execution, even though the intention is for each row to be updated only once. How Does the Halloween Problem Occur? The issue typically surfaces when: * An update operation modifies a column that is also used in the index being scanned to identify the rows to update. * As the update progresses, changing the indexed column can cause the physical location of the row in the index to change. * This movement can cause the row to reappear ahead of the scan cursor, meaning the same row is encountered and updated again—potentially leading to multiple updates or even infinite loops. Halloween Protection: The Traditional So...

Windows cluster upgrade - ROLLING UPGRADE method

The "Rolling Upgrade" method is a commonly used approach for upgrading a cluster while minimizing downtime and maintaining service availability. It involves sequentially upgrading individual nodes or components of a cluster, one at a time, while the rest of the cluster continues to operate. Here's an overview of the steps involved in a Rolling Upgrade: Plan the upgrade : Determine the scope and requirements of the upgrade, including the target version or configuration changes. Consider factors like compatibility, dependencies, and any potential impact on applications or services running on the cluster. Prepare the upgrade : Ensure you have a backup or snapshot of the cluster's current state in case you need to roll back. Review documentation and release notes for the new version or configuration changes. Prepare any necessary upgrade scripts or tools. Start with a node or component: Select an initial node or component for the upgrade. This could be a single machine, a...

Wait Type - SQL Server

WAIT TYPES refer to the specific events or conditions that cause a query or transaction to wait for a resource to become available. Understanding wait types is essential for troubleshooting and optimizing SQL Server performance. SQL Server wait types can be broadly categorized into two group s: External waits : These waits occur when SQL Server is waiting for an external resource, such as I/O operations or network requests. Examples of external wait types include PAGEIOLATCH_EX, NETWORK_IO, and ASYNC_NETWORK_IO. DMVs related to External Waits : sys.dm_io_virtual_file_stats : This DMV provides information on I/O statistics at the file level. It can be used to identify which database files are experiencing high I/O activity. sys.dm_exec_requests : This DMV provides information on currently executing queries, including the wait type, wait time, and resource utilization. Internal waits : These waits occur when SQL Server is waiting for an internal resource, such as a latch or a lock. Exam...

Split Brain - SQL Server

Split Brain is a term used in SQL Server clustering to describe a scenario where the nodes in the cluster lose communication with each other, and each node believes that it is the only active node in the cluster. This can cause multiple issues, including data corruption, duplicate transactions, and other unexpected behaviors. Here's an in-depth detail of split brain in SQL Server clustering with examples: In a SQL Server clustering environment, there are multiple nodes that are connected to each other via a network. Each node has access to the same shared storage, which contains the SQL Server database files. In a normal situation, the nodes communicate with each other to ensure that only one node is active at any given time. This is achieved through a mechanism called a cluster heartbeat, where the nodes send signals to each other to indicate that they are still alive and functioning. However, in certain scenarios, the cluster heartbeat may fail, and the nodes may lose communicat...

Usage of Physical and Virtual Memory in SQL Server.

Physical memory , also known as RAM (Random Access Memory), is a type of computer memory that is used to temporarily store data and instructions that the CPU (Central Processing Unit) needs to access quickly. Virtual memory, on the other hand, is a technique that allows the operating system to use a portion of the hard disk as if it were RAM. In SQL Server, physical memory is used to cache data and instructions that are frequently accessed by the server. This is known as the buffer pool, and it is used to improve the performance of the database by reducing the number of disk reads that need to be performed. When SQL Server needs to access data that is not in the buffer pool, it must retrieve it from disk, which can be slower than accessing it from memory. Virtual memory is used by SQL Server as a backup when the physical memory is not sufficient to store all the data and instructions that are needed by the server. In this case, the operating system will move some of the data and instr...

How Memory Impact the SQL Server Performance?

Memory is a critical component that can significantly impact the performance of SQL Server. When SQL Server processes data, it stores and retrieves information from memory, and the amount of memory available to SQL Server can directly affect its performance.  Buffer Cache Hit Ratio : The Buffer Cache Hit Ratio is a critical performance metric for SQL Server, which represents the percentage of data pages that are found in memory. A high buffer cache hit ratio indicates that the SQL Server is finding the data it needs in memory, which leads to faster query execution times. A low buffer cache hit ratio means that the SQL Server is having to fetch data from disk, which is slower and can lead to poor performance. For example , let's say that a SQL Server has 32 GB of memory, and the buffer cache hit ratio is 99%. This means that 99% of the data that SQL Server needs is in memory, and only 1% of the data needs to be fetched from disk. This results in faster query execution times and impr...

How Input/Output Impact the SQL Server Performance?

I/O (Input/Output) can have a significant impact on SQL Server performance as it involves the read and write operations to and from disk. Here are some factors that can impact SQL Server I/O performance: Storage Type : The type of storage you use for SQL Server can impact performance. Solid State Drives (SSDs) offer faster I/O compared to Hard Disk Drives (HDDs), but they can be more expensive. RAID Configuration : A Redundant Array of Inexpensive Disks (RAID) configuration can improve performance and provide redundancy for data protection. RAID configurations can improve read and write performance by striping data across multiple disks. Disk Layout : The way that data is laid out on disks can impact I/O performance. Using multiple disks for data, logs, and backups can help to avoid contention and improve performance. Indexing Strategy : Indexing can impact I/O performance by affecting the amount of data that needs to be read from disk. A well-designed indexing strategy can reduce the ...

How CPU Cores Impact the SQL Server Performance?

The number of CPU cores can have a significant impact on the performance of SQL Server. Here is a explanation of how the number of CPU cores affects SQL Server performance, along with examples: Parallel query processing : SQL Server can take advantage of multiple CPU cores to process queries in parallel, which can result in faster query processing times.  For example , suppose you have a query that takes 10 seconds to run on a system with 4 CPU cores. If you double the number of CPU cores to 8, the same query may only take 5 seconds to run, assuming the system has enough memory and disk I/O capacity to handle the increased workload. Concurrent user activity : SQL Server can handle more concurrent user activity with more CPU cores. Each user connection can be assigned to a separate CPU core, which can result in more efficient processing of queries and requests.  For example , suppose you have a system with 4 CPU cores and 50 users accessing SQL Server simultaneously. If each us...

Prefetching - SQL Server

Prefetching is a technique used by SQL Server to improve query performance by anticipating the data that will be needed for a query and reading it into memory in advance. This reduces the number of I/O operations required to fetch data from disk, which can significantly improve query performance. There are several mechanisms that SQL Server uses for prefetching, including: Read-Ahead : This mechanism anticipates the data pages that will be needed for a query and reads them from disk into memory in advance. This is based on a predictive algorithm that uses information from the query execution plan to determine which pages are likely to be needed. Pre-Fetching : This mechanism anticipates the related data that will be needed for a query and reads it from related tables into memory in advance. This is based on the relationships defined in the query and can significantly reduce the number of I/O operations required during query execution. Parallelism : SQL Server can use multiple threads ...

TLS 1.3 on SQL Server

Transport Layer Security (TLS) is a cryptographic protocol that provides secure communication over a network. TLS 1.3 is the latest version of the protocol, released in 2018, and it provides several improvements over the previous versions, including stronger encryption algorithms and a streamlined handshake process. In the context of SQL Server, TLS 1.3 can be used to secure the communication between the SQL Server instance and client applications, such as database management tools or web applications. When TLS 1.3 is enabled, all data exchanged between the client and server is encrypted and authenticated, protecting it from eavesdropping and tampering. To enable TLS 1.3 for SQL Server, you need to follow these steps : Install a certificate on the SQL Server instance: To use TLS 1.3, you need to have a certificate installed on the SQL Server instance. This certificate is used to authenticate the server to the client and to establish the encryption keys. Enable TLS 1.3 on the SQL Serv...

Lock Manager - SQL Server

The Lock Manager in SQL Server is responsible for managing the locks that are acquired by transactions to ensure data consistency and integrity. When a transaction wants to access a data resource, it must first acquire a lock on that resource. The Lock Manager controls the acquisition and release of locks and ensures that concurrent transactions do not conflict with each other. The Lock Manager works by using different types of locks to manage the transactions. The two main types of locks used by the Lock Manager are Shared locks and Exclusive locks. Shared locks: A shared lock allows multiple transactions to read the same data resource simultaneously, but it prevents any transaction from modifying the data resource while the shared lock is held. Exclusive locks: An exclusive lock allows a single transaction to modify a data resource, preventing any other transactions from accessing or modifying the same resource until the exclusive lock is released. The Lock Manager uses a variety of...

Log Manager - SQL Server

The Log Manager in SQL Server is responsible for managing the transaction log, which is a critical component of the database. The transaction log records all the transactions that are performed on the database, including insertions, updates, and deletions, and it also records the time at which the change was made. In case of a system failure or error, the transaction log is used to recover the database to a consistent state. The Log Manager performs several tasks to ensure the integrity and consistency of the transaction log. These tasks include: Writing log records to the transaction log file: Whenever a transaction is executed, the Log Manager writes a log record to the transaction log file. The log record contains information about the transaction, including the type of transaction, the time at which it was executed, and the data that was changed. Flushing the log buffer: The Log Manager maintains a log buffer in memory, which stores the log records before they are written to the t...

Lock and it types - SQL Server

In SQL Server, a " LOCK " is a mechanism used to ensure data consistency and prevent data corruption when multiple transactions access the same data concurrently. When a transaction needs to access a piece of data, it requests a lock on that data. If the data is already locked by another transaction, the requesting transaction may have to wait until the lock is released. There are several types of locks in SQL Server, including: Shared Lock : A shared lock allows multiple transactions to read the same data simultaneously, but it prevents any transaction from modifying the data until the lock is released. For example, when multiple transactions are reading from the same table, they acquire shared locks on the table to ensure that they don't interfere with each other. Exclusive Lock : An exclusive lock is acquired when a transaction needs to modify data, and it prevents any other transaction from reading or modifying the same data until the lock is released. For example, wh...

Rebuilding Index with Compression - SQL Server

Rebuilding Index with Compression is a process in SQL Server that involves rebuilding an existing index on a table with the addition of data compression. This process can help reduce the size of the index, which in turn can help improve query performance and reduce storage costs. Here is an example of how to rebuild an index with compression in SQL Server: Assuming we have a table called "SalesData" with an index called "IX_SalesData_SalesDate", which we want to compress. 1) First, we need to identify the index we want to compress:     SELECT name, type_desc, is_disabled, compression_desc      FROM sys.indexes      WHERE object_id = OBJECT_ID('SalesData')      AND name = 'IX_SalesData_SalesDate'; This query retrieves information about the index, including its name, type, whether it is currently disabled, and its compression settings. 2) Next, we need to rebuild the index with compression:     ALTER INDEX IX_SalesData_Sa...

XACT_ABORT - SQL Server

XACT_ABORT is a setting in SQL Server that specifies whether a transaction should be automatically rolled back if a run-time error occurs within the transaction. When XACT_ABORT is set to ON, if a run-time error occurs within a transaction, the entire transaction is immediately rolled back and an error message is returned to the client. This can be useful to ensure data consistency and to prevent partially completed transactions from leaving the database in an inconsistent state. When XACT_ABORT is set to OFF (which is the default setting), a run-time error within a transaction will cause the transaction to be rolled back only if the error is severe enough to cause the batch to terminate. This means that it is possible for the transaction to partially complete before an error is encountered, which could leave the database in an inconsistent state. To enable XACT_ABORT in SQL Server, you can use the following syntax at the beginning of your query or stored procedure:     ...

Buffer Pool Parallel Scan - SQL Server 2022

Buffer pool parallel scan in SQL Server 2022 When a table or index scan is performed in SQL Server, the pages are read into the buffer pool. With buffer pool parallel scan, the pages are scanned in parallel, which allows the scan to complete more quickly and efficiently. This is achieved by dividing the work of scanning the pages into multiple tasks, each of which is performed by a separate processor. The number of processors used for the scan is controlled by the MAXDOP option. MAXDOP stands for "maximum degree of parallelism" and specifies the maximum number of processors to use for the scan. For example, if you set MAXDOP to 4, SQL Server will use up to 4 processors to scan the pages in parallel.                SELECT * FROM SalesData OPTION (MAXDOP 4) The decision to use buffer pool parallel scan is made by the query optimizer, based on the size of the table, the available resources, and other factors. The query optimizer will use buffer ...