Saturday, August 3, 2024

Memory-Optimized Tables in SQL Server

 Memory-Optimized Tables are a feature in SQL Server designed to improve the performance of transaction processing. These tables reside in memory and use optimized data structures to reduce latency and increase throughput. Here's a guide on how to work with memory-optimized tables in SQL Server.

Enabling Memory-Optimized Tables

First, you need to enable the memory-optimized filegroup for your database:

sql
ALTER DATABASE [YourDatabaseName]
ADD FILEGROUP [MemoryOptimizedData] CONTAINS MEMORY_OPTIMIZED_DATA;

Add a container to the memory-optimized filegroup:

ALTER DATABASE [YourDatabaseName]
ADD FILE (NAME='MemoryOptimizedData', FILENAME='C:\Data\MemoryOptimizedData') TO FILEGROUP [MemoryOptimizedData];

Creating a Memory-Optimized Table

To create a memory-optimized table, use the MEMORY_OPTIMIZED=ON option in your CREATE TABLE statement:

CREATE TABLE dbo.MyMemoryOptimizedTable
(
    ID INT NOT NULL PRIMARY KEY NONCLUSTERED,
    Name NVARCHAR(100) NOT NULL,
    Quantity INT NOT NULL,
    CONSTRAINT CHK_Quantity CHECK (Quantity > 0)
) WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_AND_DATA);

Options for Durability

There are two options for durability when creating a memory-optimized table:

  1. DURABILITY=SCHEMA_AND_DATA: Both schema and data are durable, meaning they are persisted to disk.
  2. DURABILITY=SCHEMA_ONLY: Only the schema is durable, which means data is lost on server restart.

Indexes on Memory-Optimized Tables

Indexes on memory-optimized tables must be created when the table is created. For example, creating a hash index for faster lookups:

CREATE TABLE dbo.MyMemoryOptimizedTable
(
    ID INT NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
    Name NVARCHAR(100) NOT NULL,
    Quantity INT NOT NULL
) WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_AND_DATA);

Inserting Data into Memory-Optimized Tables

You can insert data into memory-optimized tables the same way as you would with regular tables:

INSERT INTO dbo.MyMemoryOptimizedTable (ID, Name, Quantity)
VALUES (1, 'Product A', 10),
       (2, 'Product B', 20);

Updating and Deleting Data

Updating and deleting data also follow the same syntax as regular tables:

UPDATE dbo.MyMemoryOptimizedTable
SET Quantity = 15
WHERE ID = 1;

DELETE FROM dbo.MyMemoryOptimizedTable
WHERE ID = 2;

Considerations and Limitations

  • Concurrency: Memory-optimized tables use optimistic concurrency control and support high levels of concurrent transactions.
  • Natively Compiled Stored Procedures: To further enhance performance, you can use natively compiled stored procedures with memory-optimized tables.
  • Limitations: Certain T-SQL features are not supported with memory-optimized tables, such as FOREIGN KEY constraints and certain data types.

Example of a Natively Compiled Stored Procedure

CREATE PROCEDURE dbo.InsertMyMemoryOptimizedTable
@ID INT,
@Name NVARCHAR(100),
@Quantity INT
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS BEGIN ATOMIC WITH
(
    TRANSACTION ISOLATION LEVEL = SNAPSHOT,
    LANGUAGE = N'English'
)
    INSERT INTO dbo.MyMemoryOptimizedTable (ID, Name, Quantity)
    VALUES (@ID, @Name, @Quantity);
END;

Memory-Optimized Tables can significantly improve performance for certain types of workloads, especially those that involve high transaction rates and require low-latency data access.

Friday, August 2, 2024

Query Store in SQL Server

 The Query Store in SQL Server is a feature that helps with performance monitoring and troubleshooting by capturing a history of queries, plans, and runtime statistics. It enables you to analyze the performance of queries over time and identify performance regressions.

Enabling Query Store

You can enable Query Store on a database using the following T-SQL command:

ALTER DATABASE [YourDatabaseName] SET QUERY_STORE = ON;

Configuring Query Store

You can configure various settings of the Query Store such as the operation mode, data retention period, and more. Here’s an example:

ALTER DATABASE [YourDatabaseName] 
SET QUERY_STORE = ON (
    OPERATION_MODE = READ_WRITE,
    CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30),
    DATA_FLUSH_INTERVAL_SECONDS = 900,
    INTERVAL_LENGTH_MINUTES = 60,
    MAX_STORAGE_SIZE_MB = 1000
);

Querying Query Store

You can query the Query Store views to retrieve information about the queries. Some useful views include:

  • sys.query_store_query: Contains information about the queries.
  • sys.query_store_query_text: Contains the text of the queries.
  • sys.query_store_plan: Contains information about the query plans.
  • sys.query_store_runtime_stats: Contains runtime statistics for the queries.

Here's an example query to get the top 10 queries by execution count:

SELECT TOP 10
    qsqt.query_sql_text,
    qsp.plan_id,
    qsq.execution_count,
    qsq.last_execution_time
FROM 
    sys.query_store_query_text AS qsqt
JOIN 
    sys.query_store_query AS qsq
    ON qsqt.query_text_id = qsq.query_text_id
JOIN 
    sys.query_store_plan AS qsp
    ON qsq.query_id = qsp.query_id
JOIN 
    sys.query_store_runtime_stats AS qsrs
    ON qsp.plan_id = qsrs.plan_id
ORDER BY 
    qsq.execution_count DESC;

Forcing a Plan

If you identify a query plan that performs better, you can force SQL Server to use that plan for a specific query:

EXEC sp_query_store_force_plan @query_id = 1, @plan_id = 1;

Unforcing a Plan

To unforce a plan, use the following command:

EXEC sp_query_store_unforce_plan @query_id = 1, @plan_id = 1;

Disabling Query Store

If you want to disable the Query Store, you can do so with the following command:

ALTER DATABASE [YourDatabaseName] SET QUERY_STORE = OFF;

Query Store is a powerful tool for monitoring and improving query performance in SQL Server, providing valuable insights into query behavior over time.