Administration of SQL Server

15 downloads 610 Views 870KB Size Report
10. Passing the requested page(s) back to the AM. 11. Passing the results set to the. Relational Engine credit: SQL Server 2008 Internals and Troubleshooting.
Administration of SQL Server Memory RNDr. David Hoksza, Ph.D. http://siret.cz/hoksza

Overview • Query lifecycle

• Data storage

• SQL Server Memory

SELECT Query Lifecycle 1. 2.

3. 4. 5. 6. 7. 8. 9. 10. 11.

SNI client-server connection using TCP/IP (or other protocol) SELECT statement using TDS (Tabular Data Streams) messages between TDS endpoints Unwrapping TDS message Sending “SQL Command” to Command Parser Checking Plan Cache in the Buffer Pool If the plan is not cached, passing a query tree to the Optimizer Query plan passed to Query Executor Passing Query Plan to Access Methods (AM) Checking the existence of the required page in the Data Cache by the Buffer Manager Passing the requested page(s) back to the AM Passing the results set to the Relational Engine

SQLOS credit: SQL Server 2008 Internals and Troubleshooting

UDPATE Query Lifecycle 1. 2.

3. 4.

5. 6.

Identification of the page to update as in the SELECT lifecycle The Access Methods require Write-Ahead Logging from the Log Manager (part of the Transaction Manager) Page changes stored in the Transaction Log The AM receive confirmation and pass the request on to the Buffer Manager Modification of the page in cache Confirmation sent to the AM and the client

SQLOS credit: SQL Server 2008 Internals and Troubleshooting

Buffer Manager • Buffer o

8KB page in memory

• Dirty pages o o o

o

o

pages modified in cache but not written to disk durability maintained by the transaction log flushing dirty pages • low free buffer list o worker thread issues a read request and the free buffer list is low • lazywriter process • checkpoint DBCC DROPCLEANBUFFERS • flush clean pages from cache • suitable for testing purposes sys.dm_os_buffer_descriptors • is_modified

• Lazywriter process o o o

process which periodically checks the size of the free buffer list ages-out long enough not used pages releases memory to OS

• Checkpoint process o o o o

dirty pages of committed transactions are written to disk does not remove pages from cache occurs automatically or using CHECKPOINT command trace flag 3502 provides logging DBCC TRACEON(3502, -1) • DBCC TRACEOFF(3502, -1) • checkpoint; • EXEC xp_readerrorlog

Pages • 8KB

o header o rows o row offsets

• Allocation units

o IN_ROW_DATA (hobt - Heap Or B-Tree) • majority of data o LOB_DATA (LOB) • TEXT, NTEXT, IMAGE, … • stored out-row o sp_tableoption – “text in row” o ROW_OVERFLOW_DATA (SLOB - Small-LOB) • VARCHAR, VARBINARY, .NET, … • stored in-row unless the row exceeds 8KB o sys.allocation_units

Extent • 8 pages ⇒ 64KB • Types

o uniform o mixed

• First page of a new table → mixed extent • New index

o large → uniform extents o small → mixed extents

Space Management • Global Allocation Map (GAM) o 1 = free extent, 0 = allocated extent o covers 64,000 extents

• Shared Global Allocation Map (SGAM) o covers 64,000 extents o 1 = extent is being used as a mixed extent and has a free page, 0 = extent is not used as a mixed extent, or it is a mixed extent and all its pages are being used

• Page Free Space (PFS) o allocation status of each page – 1B

Extent status

GAM SGAM bit bit setting setting

Free

1

Uniform extent (allocated)

0

0

Mixed extent with free pages

0

1

Full mixed extent

0

0

Space Management (cont.)

• Index Allocation Map (IAM)

o tracking extents in 4GB used by an allocation unit o can be linked → IAM chain o maps space allocation for • heaps and b-trees • LOB data • row-overflow data o IAM page • 96-byte page header • IAM page header (8 page-pointer slots) • bitmap for extents belonging to the allocation unit o sys.system_internals_allocation_units

• Inserting a new row

o IAM → extents for the allocation unit o PFS → free pages in identified extents

Database File Structure

Memory – 32 bit system • 4GB address space o 2GB user mode o 2GB kernel mode

• 4GB tuning o o o

o o

allows to use 3GB user mode address space Windows Server 2003 • /3GB in boot.ini Windows Server 2008 • BCDEdit /set increaseUserVA 3072 application needs to be linked with /LARGEADDRESSAWARE nonpaged pool, paged pool and system PTEs (Page Table Entry) need to be monitored

• Physical Address Extension (PAE) o o o o o o

o o o

introduced by Intel address bus = 36 bits → 64GB Windows Enterprise and Datacenter Windows Server 2003 • /PAE in boot.ini Windows Server 2008 • bcdedit /set [{ID}] pae ForceEnable applications must be written to be able to use AWE (Address Windowing Extensions) which allows a process to access memory outside of its VAS (by mapping this memory into VAS) PAE must be enabled in SQL Server by sp_configure or SSMS SQL Server service needs to have “Lock Pages in Memory” privilege exploitable by data cache only

SQL Server Memory Management • Memory size restriction o min server memory o max server memory • sp_configure 'max server memory', 4096;

• sys.dm_os_sys_info

o information about the computer on which SQL Server is installed including the resources available to and consumed • bpool_committed o number of 8-KB buffers in the buffer pool. o committed physical memory in the buffer pool • bpool_commit_target

o number of 8-KB buffers needed by the buffer pool

• Memory Clerks

o each consumer allocates memory through a memory clerk o sys.dm_os_memory_clerks

• Memory Nodes

o at least one node depending on using the NUMA (Non-Uniform Memory Access) architecture • SELECT DISTINCT memory_node_id FROM sys.dm_os_memory_clerks

• Each cache is a clerk

Cache

• sys.dm_os_memory_cache_c ounters

• SQLOS implements common caching framework • DBCC FREESYSTEMCACHE (DBCC FREESYSTEMCACHE ('SQL Plans')) • common caching mechanism o object store • Data Cache • simple store, homogeneous data (SNI – pooling network buffers) o cache store • SQLOS management of life time and visibility control (plan cache) o user store • cache store + storage semantics (metadata cache)

o o

largest cache in the buffer pool sys.dm_os_buffer_descriptors

• Plan Cache o o

caching of execution plans sys.dm_exec_cached_plans

Tasks 1.

Identify names of 10 objects with highest number of data pages (sys.allocation_units, sys.partitions, OBJECT_NAME)

2.

Create a table with rows exceeding page size and check how ROW_OVERFLOW_DATA allocation units are used (sys.allocation_units, REPLICATE)

3.

Stored procedure returning number of records in a table based on the sys.partitions view

4.

Find out number of dirty pages in each database on the server (verify by using the CHECKPOINT command) (sys.dm_os_buffer_descriptors)

5.

Is page set to dirty if an UPDATE changes a column to the same value (UPDATE t SET col = col)?

6.

Find out plan cache size in MB and average use counts for each object type for which a plan is being cached (sys.dm_exec_cached_plans)