Create Clustered index on all heap tables by on single click


images (1)Hi Guys today i am coming to write in very interesting subject for any DBA and DB analyst , any DBA should be care from more things one of this is Database design for this database design we should take care from heap tables and this one of the steps of Database assessment to check all heap tables and fix them by creating new Clustered index.
based on that i write one script to return the tables with more information then i will filtered it to return only the tables without any index then i will loop on this amount of tables to create dynamic statement for Create cluster index for each table.

 

— =============================================
— Author: Mustafa EL-masry
— Create date: 01/03/2015
— Description: DMV Create Clustered index on all heap tables by on single click
— =============================================

SET NOCOUNT ON
CREATE TABLE #Table_Policy
(
ID INT PRIMARY KEY
IDENTITY(1, 1)
NOT NULL ,
Table_Name NVARCHAR(100) ,
Rows_Count INT ,
Is_Heap INT ,
Is_Clustered INT ,
num_Of_nonClustered INT
);
WITH cte
AS ( SELECT table_name = o.name ,
o.[object_id] ,
i.index_id ,
i.type ,
i.type_desc
FROM sys.indexes i
INNER JOIN sys.objects o ON i.[object_id] = o.[object_id]
WHERE o.type IN ( ‘U’ )
AND o.is_ms_shipped = 0
AND i.is_disabled = 0
AND i.is_hypothetical = 0
AND i.type <= 2
),
cte2
AS ( SELECT *
FROM cte c PIVOT
( COUNT(type) FOR type_desc IN ( [HEAP], [CLUSTERED], [NONCLUSTERED] ) ) pv
)
INSERT INTO #Table_Policy
( Table_Name ,
Rows_Count ,
Is_Heap ,
Is_Clustered ,
num_Of_nonClustered
)
SELECT c2.table_name ,
[rows] = MAX(p.rows) ,
is_heap = SUM([HEAP]) ,
is_clustered = SUM([CLUSTERED]) ,
num_of_nonclustered = SUM([NONCLUSTERED])
FROM cte2 c2
INNER JOIN sys.partitions p ON c2.[object_id] = p.[object_id]
AND c2.index_id = p.index_id
GROUP BY table_name
————————————————————————————–
–DMV
————————————————————————————–
—-Tables didn’t have Primary key and didn’t have any index
SELECT *
FROM #Table_Policy
WHERE num_Of_nonClustered = 0
AND Is_Heap = 1

DECLARE @name NVARCHAR(100)
DECLARE db_cursor CURSOR
FOR
SELECT Table_Name
FROM #Table_Policy
WHERE num_Of_nonClustered = 0
AND Is_Heap = 1
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
——-Cusror NO 2
DECLARE @name2 NVARCHAR(100)
DECLARE db_cursor2 CURSOR
FOR
SELECT name
FROM sys.columns
WHERE object_id = OBJECT_ID(@name)
AND column_id = 1
OPEN db_cursor2
FETCH NEXT FROM db_cursor2 INTO @name2
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @SQL NVARCHAR(MAX)= N’Create Clustered index [IX_’
+ @name + ‘] on [‘ + @name + ‘]
(‘ + @name2
+ ‘ ASC) with (Fillfactor=80,Data_Compression=page)
GO’
PRINT @SQL

FETCH NEXT FROM db_cursor2 INTO @name2
END

CLOSE db_cursor2
DEALLOCATE db_cursor2
—-End of Cursor 2

FETCH NEXT FROM db_cursor INTO @name
END

CLOSE db_cursor
DEALLOCATE db_cursor
GO
DROP TABLE #Table_Policy

———————————————————————————————————————————————–

Here i can found 3 tables without any clustered index or Non clustered index

Untitled

If you check the massage you will find the T-SQL print for the Creating of Clustered index for the three tables.

Untitled

by this way by one single click you can fix all heap tables on your databases

Follow the author:

View all my tips , LinkedIn Website Slideshare  , ABOUT ME

My first technical Video in SQL Server Technology


Hello Guys “a journey of a thousand miles begins with a single step.” based on that i started my journey to MVP award i am now have more than 200 Post in SQL server Technology i am founder of Community SQL Database administration from 2012 and i started today New step to Promote my work I published today my first Technical Video in SQL Server 2014 “How we can crate Clustered Columns Store index on all tables by Single Click” this the Description of the video

Download Scripts : http://sqlserver-performance-tuning.n…

SQL Server Cumulative update package #6 is available now for SQL Server 2014


downloadHello Guys SQL Server 2014 is the latest version from Microsoft SQL Server and it is coming with awoosem enhancement and features and we are still waiting SP 1 but until now still not released only we are updated our enviroment by the latest comulative update package to fix some issues in SQL Server 2014

Now Microsoft announced about SQL Server Cumulative update package 6 for more information about the Hotfixes that are included in this cumulative update package check this link.including nearly 400 fixes and enhancements

and for more information about the Latest Builds of SQL Server 2014 check this post for sqlsentry Community

  • KB Article: KB #3031047
  • 64 fixes total (55 fixes listed publicly at time of publication)
  • Build number is 12.0.2480
  • Relevant for @@VERSION 12.0.2000 through 12.0.2479

Follow the author:

View all my tips , LinkedIn Website Slideshare 

Keep your database in safe mode


images (1)Hi Seniors DBA HI junior DBA HI DBA Consultant , There is no one without errors or defects if you agree with me Complete the post if not don’t waste your time !, How you can keep your database in safe Mode from any mistake can by happen by any accident ? we should put our database in the safe mode Under any circumstances ,so what you can do if you deleted your database by wrong ! and in the same time you lost your last backup for this database !! really it is very bad situation and may be not happened put may be happen also, to be calm and to Keep your database in safe mode you should take preventive action one of this preventive action Forcing the Drop database Command or Rename database to rollback and you can do it easily without more effort and without more maintenance cost by using DDL trigger ,  to know more about triggers and DML , DDL and Login triggers go for this link (CREATE TRIGGER (Transact-SQL)) Trigger can be created on tables , Databases and on All Server what we will do now Create trigger on all Server

Create Trigger 

IF EXISTS (SELECT * FROM sys.server_triggers
WHERE name = ‘KeepYourDBinSafeMode’)
DROP TRIGGER KeepYourDBinSafeMode
ON ALL SERVER;
GO
Create TRIGGER KeepYourDBinSafeMode
ON ALL SERVER
FOR Drop_Database ,Alter_Database
AS
PRINT ‘You must disable Trigger “KeepYourDBinSafeMode” to drop or alter Database!’
ROLLBACK
GO

Check the trigger from Mangment Studio 

Keepyourdatabaseinsavemode

Continue reading “Keep your database in safe mode”

Filling all your tables with test data VII


introduction

Hi guys in my last post  (Filling all your tables with test data) i explained how we can fill multiple tables by test data by using My own Stored procedure , but after i creating the script and i do on it more test on our staging environment and it’s  working very well i got  one issues on the script ( SP can not cover the tables with composite primary key )  at this time I reviewed  the Stored procedure from the scratch to know where is the issue and how i can solve it at the end i asked on e of my best friends Mohamed Abdel Kariem mentor T-SQL to help me in this subject and at the end we do awesome script and here the below new enhancement on our main stored procedure .

Stored procedure enhancement:

  1. SP now supported tables with composite primary key
  2. SP now supported tables with composite primary key with data type uniqueidentifier.
  3. reduced the liens in the SP by using CASE technology
  4. using QUOTENAME function instead of more concat.

Continue reading “Filling all your tables with test data VII”