Do you think you are paying for whatever space you are using or you are paying extra?

Most of the people think that they won’t have fragmentation in SQL Azure, but that’s not right SQL Azure is also SQL residing on machines which are present in cloud environment and we have as equal probability of fragmentation in Azure as it is there in on-premise environment.

Let me give you some recent problems on which I worked on,

One of my client was having database of 120GB, he was using federation and having multiple shards, he did split operation which does not involve any data movement but it just creates new shard which will hold data of next month and after Split operation finished his 120GB shard was showing size as 80 GB

Now what happened to those 40 GB?

Number of rows were same there was no data loss so was he paying more than what he was actually storing?

Reason of that data size difference was fragmentation, when he did Split operation SQL Azure engine did rebuild of indexes in background which removed fragmentation and which resulted in reduction in size of database shard.

In another example database size became 350 MB from original 1.5 GB after running query which does rebuild on all indexes of database.(isn’t that great considering how much dollar it would have saved!!!!)

Fragmentation becomes nightmare when we have unique identifier as primary key and large columns like varchar(1000).

Now question comes how to find out whether my database is having fragmentation or not.

 

First let’s find size of all tables in database, most of the people use database size feature of SQL Azure management portal to find out size , I would say instead use SQL Query to find perfect number.

Query:

 

select obj.name, sum(reserved_page_count) * 8.0 as "size in KB" from sys.dm_db_partition_stats part, sys.objects obj where part.object_id = obj.object_id group by obj.name

 

This query gives size of each table in given database.

Connect to database in concern and run this query.

 

 

select sum(reserved_page_count) * 8.0 / 1024 as "size in MB" from sys.dm_db_partition_stats

This query gives size of overall database… In case you are not interested in size of each table!

 

Now next task is finding out % of fragmentation…

 

For that also we have query which uses SQL Azure DMV’s to find out % of fragmentation

 

SELECT
DB_NAME() AS DBName
,OBJECT_NAME(ps.object_id) AS TableName
,i.name AS IndexName
,ips.index_type_desc
,ips.avg_fragmentation_in_percent
FROM sys.dm_db_partition_stats ps
INNER JOIN sys.indexes i
ON ps.object_id = i.object_id
AND ps.index_id = i.index_id
CROSS APPLY sys.dm_db_index_physical_stats(DB_ID(), ps.object_id, ps.index_id, null, 'LIMITED') ips
ORDER BY ps.object_id, ps.index_id

 

Running this query will give you result something like…

 

 

 

So it gives,

1)      Database Name

2)      Table Name

3)      Index Name

4)      Index type

And

5)      % of Fragmentation for that index

 

Any index which shows more than 10% of fragmentation needs rebuild or reorganization of index.

If fragmentation is less than 30% we can go with reorganization of index and if fragmentation is more than 30% then we need to rebuild indexes.

When we rebuild indexes we can do it with ONLINE=ON option which will keep table live while rebuilding happens, it will slow down overall rebuilding process.

 

There is a limitation that when we have any table which has columns which has large data like

varchar(50) varbinary(max) etc , we cannot do rebuild operation with ONLINE=ON .

 

For such table when we do rebuild of indexes table won’t be accessible.

 

Now question is how to do rebuild or reorganization of indexes…?

 

We have query for that also…

 

Query:

 

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
(
SELECT '[' + IST.TABLE_SCHEMA + '].[' + IST.TABLE_NAME + ']' AS [TableName]
FROM INFORMATION_SCHEMA.TABLES IST
WHERE IST.TABLE_TYPE = 'BASE TABLE'
)

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0

BEGIN
PRINT('Rebuilding Indexes on ' + @TableName)
Begin Try
EXEC('ALTER INDEX ALL ON ' + @TableName + ' REBUILD with (ONLINE=ON)')
End Try
Begin Catch
PRINT('Cannot do rebuild with Online=On option, taking table ' + @TableName+' down for douing rebuild')
EXEC('ALTER INDEX ALL ON ' + @TableName + ' REBUILD')
End Catch
FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor
DEALLOCATE TableCursor

 

 

 

You need to connect to database which is having fragmentation problem and run this query, it will do rebuild on all tables present in database.

If it is possible to rebuild indexes while keeping database online it will do that else it will run without online=on option.

 

Output of this query will be something like,

 

 

Once this query finishes executing run query to find database size again and see the magic!

 

Send some gifts if this blog help you in saving some money!!! J

 

Query to find fragmentation on specific database and specific table

 

 

SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(N'<Database name>'), OBJECT_ID(N'<table name>'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id;
GO

 

 

Some links which gives more details,

 

http://msdn.microsoft.com/en-us/library/ms189858%28v=sql.105%29.aspx

http://msdn.microsoft.com/en-us/library/ms188917.aspx

http://beyondrelational.com/modules/2/blogs/76/posts/15290/index-fragmentation-in-sql-azure.aspx