Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

SQL Memory Issue

$
0
0

Dear Expert,

     I hope someone already dicuss before i post this topic. But I believe someone can explain to me why SQL memory usage is high after run the T-SQL or process (i want to hear my testing is wrong or not). I have Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)   Microsoft Corporation  Developer Edition (64-bit) (Build 3790: Service Pack 2)  on Windows XP-64Bit. I set 10240 MB at "Maximum Server Memory". I want to know memory usage before and after finish working process. How am i test it, I run rebuild index used below sql script.

SET NOCOUNT ON
GO
--Set the fillfactor
DECLARE @FillFactor TINYINT
SELECT @FillFactor=80
DECLARE @StartTime DATETIME
SELECT @StartTime=GETDATE()
if object_id('tempdb..#TablesToRebuildIndex') is not null
begin
drop table #TablesToRebuildIndex
end
DECLARE @NumTables VARCHAR(20)
SELECT
s.[Name] AS SchemaName,
t.[name] AS TableName,
SUM(p.rows) AS RowsInTable
INTO #TablesToRebuildIndex
FROM
sys.schemas s
LEFT JOIN sys.tables t
ON  s.schema_id = t.schema_id
LEFT JOIN sys.partitions p
ON  t.object_id = p.object_id
LEFT JOIN sys.allocation_units a
ON  p.partition_id = a.container_id
WHERE
p.index_id IN ( 0, 1 ) -- 0 heap table , 1 table with clustered index
AND p.rows IS NOT NULL
AND a.type = 1  -- row-data only , not LOB
GROUP BY
s.[Name],
t.[name]
SELECT @NumTables=@@ROWCOUNT
DECLARE RebuildIndex CURSOR FOR
SELECT
ROW_NUMBER() OVER (ORDER BY ttus.RowsInTable),
ttus.SchemaName,
ttus.TableName,
ttus.RowsInTable
FROM
#TablesToRebuildIndex AS ttus
ORDER BY
ttus.RowsInTable
OPEN RebuildIndex
DECLARE @TableNumber VARCHAR(20)
DECLARE @SchemaName NVARCHAR(128)
DECLARE @tableName NVARCHAR(128)
DECLARE @RowsInTable VARCHAR(20)
DECLARE @Statement NVARCHAR(300)
DECLARE @Status NVARCHAR(300)
FETCH NEXT FROM RebuildIndex INTO @TableNumber, @SchemaName, @tablename, @RowsInTable
WHILE ( @@FETCH_STATUS = 0 )
BEGIN
SET @Status='Table '+@TableNumber+' of '+@NumTables+': Rebuilding indexes on '+@SchemaName+'.'+@tablename + ' ('+@RowsInTable+' rows)'
--RAISERROR (@Status, 0, 1) WITH NOWAIT  --RAISERROR used to immediately output status
PRINT @Status
SET @Statement = 'ALTER INDEX ALL ON ['+@SchemaName+'].['+@tablename +'] REBUILD WITH (FILLFACTOR = '+CONVERT(VARCHAR(3), @FillFactor)+' )'
EXEC sp_executesql @Statement
FETCH NEXT FROM RebuildIndex INTO @TableNumber, @SchemaName, @tablename, @RowsInTable
END
CLOSE RebuildIndex
DEALLOCATE RebuildIndex
drop table #TablesToRebuildIndex
Print 'Total Elapsed Time: '+CONVERT(VARCHAR(100), DATEDIFF(minute, @StartTime, GETDATE()))+' minutes'
GO

Before i run above script, i run below script for memory usage of each database.

select db_name(database_id),(cast(count(*) as bigint)*8192)/1024 as "size in mb" from sys.dm_os_buffer_descriptors 
group by db_name(database_id) 

My Database size

MDF - 184 MB

NDF - 52.3 GB

LDF - 2.03 GB

Here is memory usage before and after run rebuild index.

Before Memory usage - 55808MB(using T-SQL)

After Memory usage - 9883216MB (using T-SQL) - I wait 1 hour but memory usage is high. That's why i restart service.

After restart SQL Service Memory usage - 55808MB

Is it something wrong my testing? Or is it anyway to test memory usage? Why memory usage is high until restart the service?

Best Regards,

Yukon


Make Simple & Easy


Viewing all articles
Browse latest Browse all 15889

Trending Articles