Total Pageviews

Showing posts with label Memory. Show all posts
Showing posts with label Memory. Show all posts

Saturday, November 1, 2014

Buffer Pool Extension (BPE) in SQL 2014 - A brief introduction

What is Buffer Pool (BP)?
The Buffer Pool (also known as the buffer cache) is a shared memory area which consists of data/index pages. A buffer page is 8KB in size and the Max Server Memory determines the total size of the Buffer Pool. For example, when we set max server memory=16GB, it means we are allocating 16GB of memory to the Buffer Pool. The plan cache is also the part of the Buffer Pool.

What is Buffer Pool Extension (BPE)?
This is a new feature in SQL 2014 where we can extend the Buffer Pool beyond the limitation of the maximum physical memory. This means that we can use a persistent disk file system as part of the buffer. The file can be placed in any disk including the network shared folder.

Suitability of BPE:
The primary goal of BPE implementation is to improve Read performance of an OLTP application where physical memory is less than 64GB (according MSDN blogs). The application which is more READ intensive and less WRITE prone, implementing this feature will enhance I/O performance greatly. However, this feature is not suitable for data warehouse and WRITE intensive type workload.

Where to place the file?
The SSD provides very low random and read access latency, thus the best place to put the BPE file in a SSD. If it is placed in a normal disk system, there will be a negative impact on I/O.

BPE size:
The BPE size can be 32 times greater than Max Server’s Memory. So if we set Max Server Memory to 4 GB, then BPE can be 4X32 GB = 128 GB. Microsoft recommends 4 to 8 times of the amount of Max Server Memory which will be an optimal size for overall I/O performance.

What will be written in a BPE file?
The BPE file only holds “clean pages” in it. A “clean page” (aka un-modified page) is a data or index page which is exact and equivalent to a disk page residing in the data file. A BPE file will not hold any “dirty pages” at any time.

Good to know and good practices:
Before taking advantage of the features of BPE, we should be sure that adding extra physical memory is not an option due to hardware limitation. In addition to this, we should have optimal T-SQL code and we should not have I/O overhead index schema such as overlapping, duplicating and multi-column indexes.

1.      Placing the BPE file in a high performance SSD is recommended.
2.      Make sure the disk alignment is correct.
3.      Consider formatting the disk with 64K block (cluster) size.
4.      Set the Max Server Memory and leave enough memory for the OS.
5.      “Lock pages in Memory” has no interaction with BPE.
6.      Before modifying the BPE, the Buffer Pool Extension feature should be disabled first.
7.      When the BPE is disabled, all related configuration settings are removed automatically and the memory used to support the feature is not reclaimed until the instance of SQL Server is restarted. The Buffer Pool Extension file will be deleted once the instance of SQL Server is shutdown.
8.      BPE can be configured and disabled at any time while the instance of SQL Server is running. If the feature is re-enabled, the memory will be reused without restarting the instance.
9.      As BPE contains only clean pages, data loss is out of question.

How to configure BPE:
Configuring BPE is very straight forward and simple. Following are the steps we need to follow to configure a 16GB BPE file when the Max Server Memory is set to 4GB.

Set the “max server memory” to 4GB:
EXEC sp_configure 'max server memory (MB)', 4096
GO
RECONFIGURE

Configure the Buffer Pool Extention to 16GB:
ALTER SERVER CONFIGURATION
SET BUFFER POOL EXTENSION ON
    (FILENAME = 'E:\SQLBuffer\ExtendedBuffer.BUF', SIZE = 16 GB);

To see the configuration, execute following DMV query:
SELECT * FROM   sys.dm_os_buffer_pool_extension_configuration

To turn off the BPE feature, simply execute the following: 
ALTER SERVER CONFIGURATION
SET BUFFER POOL EXTENSION OFF

Querying BPE utilization:
The following query can be used to identify utilization of BPE:
SELECT CASE
            WHEN database_id = 32767 THEN 'ms_resource_db'
            ELSE DB_NAME(database_id)
       END       AS database_name,
       COUNT(*)  AS cached_pages_count,
       CONVERT(NUMERIC(25, 4), COUNT(row_count) * 8.00 / 1024.00) AS size_mb,
       CONVERT(
           NUMERIC(25, 4),
           COUNT(row_count) * 8.00 / 1024.00 / 1024.00
       )         AS size_gb
FROM   sys.dm_os_buffer_descriptors
WHERE  is_in_bpool_extension = 1
GROUP BY
       database_id

    
Performance measurement of BPE:
There are some performance counters that have been introduced in SQL 2014 to measure BPE performance. It can be monitored using Windows PerfMon or with T-SQL. Following is the T-SQL query:

SELECT [object_name], counter_name, instance_name, cntr_value
FROM   sys.dm_os_performance_counters
WHERE  [object_name] LIKE '%Buffer Manager%'
       AND [counter_name] LIKE '%Extension%'


References:
Buffer Pool Extension

Buffer Pool Extension to SSDs in SQL Server 2014

Wednesday, May 7, 2014

Memory allocation for SQL Server - points to consider


Memory Allocation:
When we are talking about memory allocation for SQL Server, we usually mean memory allocation for the buffer pool. The two well-known parameters - the “max server memory” and the “min server memory” - always refer to the data buffer only.

These options do not include the memory that is allocated for other components within the SQL Server process. These components include the following:
(a)    The SQL Server worker threads.
(b)   The Multipage Allocator of SQL Server Memory Manager.
(c)    Various DLLs and components that the SQL Server process loads within the address space of the SQL Server process.
(d)   The backup and restore operations.
(e)   SQL Server Agent process.

Windows 2003 vs. Windows 2008/2008R2/2012:
In Windows 2003, OS manages its memory aggressively and always tries to free up physical memory; this generally introduces “paging out” issues in SQL Server. Windows 2008 and later versions are more educated and can manage memory dynamically and non-intrusively. Therefore a SQL Server running on a Windows 2008 or later edition has better experience and needs extra consideration for configuring SQL Server memory.

Windows 32-bit vs. 64-bit:
Windows 32-bit edition can’t use more than 4GB of physical memory while 64-bit Windows has varying limitations based on the edition.
(a)    In a 32-bit Windows Server, the /PAE switch is required to access more than 4GB of physical memory if installed. Enabling AWE in SQL Server is also required.
(b)   In a 64-bit environment, /PAE and /AWE are not required to be configured.

PAE and AWE Switch (physical memory <= 4GB):
If a 32-bit environment has less than or equal to 4GB of memory, the /3GB switch can be used to allocate more memory to the SQL Server. Once a /3GB switch is used, the total virtual memory will be splitting since1GB is for kernel mode and 3GB is for user mode usages.

PAE and AWE Switch (physical memory > 4GB):
In a 32-bit Windows Server and SQL Server implementation, if more than 4GB of memory is installed then both the PAE in Windows and AWE in SQL Server need to be turned on. In a 32-bit physical environment, the following configurations must be configured should the physical memory be used beyond 4GB.

(a)    LPIM – Assign this local right to the SQL Server service account.
(b)   Max Server Memory – use a fixed amount of memory for SQL Server.
(c)    /PAE – Enable switch in Windows if it is not done yet.
(d)   /AWE – Enable this switch in SQL Server.

If this is a Virtual implementation, then the provisioned Guest Memory must be reserved in VMWare ESXi or in MS Hyper-V to prevent memory swapping (balloon driver effect).

Memory configuration in 32-bit environment:
Follow the guidelines below when configuring memory for 32-bit SQL Server in a 32-bit environment.

Physical RAM
AWE
Boot.ini
Maximum “Max Server Memory”
4 GB
0 (Disabled)
/3GB
Dynamic (default)
8 GB
1 (Enabled)
/3GB, /PAE
6.5 GB
16 GB
1 (Enabled)
/3GB, /PAE
14.5 GB
32 GB
1 (Enabled)
/PAE
29.5 GB

Max Server Memory Allocation Guideline:
Although Microsoft recommends using dynamic memory configuration in a 64-bit environment, this recommendation does not consider suboptimal SQL database design, and other executing applications and processes. So the “Max Server Memory” setting is strongly recommended to limit the buffer pool of SQL Server. Following is the guideline for configuring Max server memory in a 64-bit SQL Server. Always consider leaving some additional memory for Windows and other applications based on workload.

Physical Memory
Minimum Memory for Windows
Maximum “Max SQL Server Memory”
<16
2
Dynamic / best judgment
16
4
12
32
6
26
64
8
56
128
16
112
256
16
240
512
32
480
1TB
48
976

Max and Min Server memory:
Consider allocating an equal amount of memory to the “Min Server memory” and “Max Server Memory” option. This will eliminate internal page allocation and de-allocation overhead.

NUMA and Memory Configuration:
When SQL Server detects NUMA in a physical or virtual server, it calculates memory allocation from “Max Server Memory” and assigns an equal amount of memory to each NUMA node. In case of dynamic memory configuration, SQL Server Engine assumes an arbitrary value which may not be sufficient for the current workload or over estimation may introduce OS memory issues.

Please note that SQL Server 2000 SP4 and later editions support NUMA memory.

Lock Pages in Memory (LPIM) and SQL Server Editions:
By default, Enterprise and Developer 64-bit editions of SQL Server 2005 and later versions support Locked Pages.

(a)    The standard edition of SQL 2005, SQL 2008 and SQL 2008 R2 support Lock Pages in Memory if the startup trace flag 845 is set.
(b)   The standard edition of SQL 2012 and SQL 2014 supports Lock Pages in Memory natively and does not require a 845 startup trace flag.
(c)    All 32-bit versions support Lock Pages in Memory with /PAE and /AWE switch.

Lock Pages in Memory (LPIM):
In 64-bit Windows 2008 or later editions, LPIM is not required as Windows manages memory dynamically in a non-intrusive way. However, certain workloads may need a generous allocation of guaranteed fixed memory to operate smoothly. In this case, LPIM can be assigned explicitly to the SQL Server Service account.

Following is the guideline for LPIM:

(a)    When Windows sits on a physical Machine:
(1)    Make sure that the “Max Server Memory” has been assigned.
(2)    For standard editions of SQL Server, add startup trace flag 845.

(b)   When Windows sits on a virtual Machine:
(1)    Make sure that the “Max Server Memory” has been assigned.
(2)    For standard editions, add startup trace flag 845.
(3)    Make sure that the provisioned guest memory has reservation in VMWare or in Hyper-V.

The trace flag 845 has no effect on the SQL Server Enterprise edition.

Index Creation Memory:
The ‘index creation memory’ option is self-configuring and usually works without requiring adjustment. A larger value might be helpful to improve index creation performance but there is no specific recommended value. Please note that the run value for this parameter will not exceed the actual amount of memory that can be used for the operating system and hardware platform on which SQL Server is running. On 32-bit operating systems, the run value will be less than 3 GB.

The run value needs to be equal or greater than the “Min memory per query” when set.

Min memory per query:
The default value of 1024 KB works well for almost all cases. If some queries experience excessive sort and hash operation, and if optimizing the query and refactoring associated indexes out of scope then this value can be bumped up gradually to improve performance. If changing the setting helps then consider adding more physical memory and allocating more memory to the data buffer pool. The maximum limit is 2GB for this setting.

-g Switch (mostly applicable for SQL 2000 and 2005):
The –g switch reserves additional memory outside SQL Server’s buffer pool in the SQL Server process for extended stored procedures, executing distributed queries, loading DLLs of extended stored procedures and calling OLE automation objects from Transact-SQL. Consider using this switch if the following message is logged in the Error Log.

WARNING: Failed to reserve <n> bytes of contiguous memory
WARNING: Failed to reserve contiguous memory of Size= <allocation_size>

More reading: