Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Wednesday, March 18, 2015

Identify Index Details in SQL Server


The below Query will give all index details in a database.


Index Details :

Declare @IndexDetails as table (IndexName  Varchar(100) ,index_description varchar(max),index_keys varchar(400))

insert into @IndexDetails
exec sp_msforeachtable 'sp_helpindex  ''?'''

select * from @IndexDetails

Index Size and Table Size Details

The below query provides the size of each table ,Number of rows  and index size details.

declare @spacedetails as table(name varchar(100),totalnofofrows int,reservedspace varchar(100),allocatedspace varchar(100),indexsize varchar(100),unusedspace varchar(100))


insert into @spacedetails
exec sp_msforeachtable 'sp_spaceused  ''?'''


select * from @spacedetails
order by totalnofofrows desc


Alternatively we can use query the system tables also

select distinct st.name,rows,sum(total_pages*8)/1024  as size from sys.tables st
join sys.partitions sp on st.object_id = sp.object_id
join sys.allocation_units au on au.container_id = sp.partition_id
where rows > 0
group by st.name,rows
order by size desc






Query to Identify Duplicate Indexes in SQL Server


-- =============================================
-- Author:                   MalleswaraReddy
-- Create date: 26-09-2014
-- Description:            The below script is used to identify the exact duplicate index on a table.
-- =============================================

;WITH CTE AS
(
SELECT so.name as TableName,si.name AS IndexName,sc.name AS Name,ic.key_ordinal as KeyOrder ,
CASE si.is_unique   WHEN 1 THEN 'UNIQUE ' ELSE 'NON-UNIQUE ' END + si.type_desc AS type_desc
from sys.indexes si
join sys.tables so on si.object_id = so.object_id
join sys.index_columns ic on si.index_id = ic.index_id and ic.object_id = si.object_id
join sys.columns sc on  sc.column_id = ic.column_id and ic.object_id = sc.object_id
),
 CT AS
(

select  Row_number () Over( order by TableName,IndexName) AS Row,
                         T2.TableName ,
                         T2.IndexName,
                         T2.type_desc AS IndexType,
                                                                  STUFF((SELECT ','+T.name
                                                                                    FROm CTE T
                                                                                    WHERE (T.TableName = T2.TableName AND T.IndexName = T2.IndexName)
                                                                                    ORDER BY T.TableName,T.IndexName,T.KeyOrder
                                                                                    FOR XML PATH('')),1,1,'') AS IndexColumns
                                                                                   
FROM CTE T2
GROUP BY TableName,IndexName,type_desc

)
Select Row,TableName,IndexName,IndexColumns,IndexType FROM CT
WHERE Row IN
(
  SELECT max(Row) FROM CT
  GROUP BY TableName,IndexColumns
  Having Count(*) > 1
)
UNION
Select Row,TableName,IndexName,IndexColumns,IndexType FROM CT
WHERE Row IN
(
  SELECT MIN(Row) FROM CT
  GROUP BY TableName,IndexColumns
  Having Count(*) > 1
)
ORDER BY TableName,IndexColumns


Thursday, May 15, 2014

New Features in SQL Server 2014

Microsoft introduced new features in SQL Server 2014.   Among them some of the important features for developers and DBA I listed here.

High Availability enhancements
Support s widows azure as secondary replica.
Maximum numbers of secondary replicas are increased from 4 to 8.
Numerous databases can failover at a time.
Clustered shared volumes feature associated with windows 2012 server for storing SQL server virtual machines.
New Dynamic managements views for high availability options & enhancements to existing DMVS                 
sys.dm_io_cluster_valid_path_names
sys.dm_hadr_cluster
sys.dm_hadr_cluster_members – no of nodes running on WSFC cluster
sys.dm_hadr_cluster_networks

Security and DB backup enhancements
 Now with SQL Server 2014
We have the ability to encrypt the data while creating database backup.
Database backup to URLS
Supports backup to windows azure.
Managed backups
Introduced new server level  permissions

Performance enhancements
In Memory OLTP
IO is added to resource governor to pool SQLServer resource workload
Column store index up-datable.

Encrypt data while taking database backup:

SQL Server 2014 has the ability to encrypt the data while backup the database. This backup option
Is only available when the database backup is set to new media .
In order to use this option first master key should be created in master database.
Then create the certificate in master database.
USE MASTER;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Malls@123'
--select * from sys.symmetric_keys
GO
--Create a certificate to use with TDE
--select * from sys.certificates

CREATE CERTIFICATE MYTDECert
WITH SUBJECT = 'TDE Certificate'

 Once we have executed the above we can encrypt the database while taking database backup.



In order to restore this database you need the certificate protected by master key. You can refer this post for restoring the encrypted database




Database backups to URL:
SQL Server 2014 introduced the ability to take database to URLs. Its supports database backup directly to windows azure. 



Similarly using managed backup s we can configure database backup to windows azure.





New Server level permissions:
SQL Server 2014 introduced new server level permissions to further enhance security and accessibility.
o   IMPERSONATE ANY LOGIN (server level)
o   CONNECT ANY DATABASE (server level)
o   SELECT ALL USER SECURABLES (Server level)
o   ALTER ANY DATABASE EVENT Notification (database level)



 In-memory OLTP:


Sample Script for creating the In-memory OLTP Database

CREATE DATABASE [SSISDatabase]
 CONTAINMENT = NONE
 ON  PRIMARY
( NAME = N'SSISDatabase', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.SHANMUKREDDY\MSSQL\DATA\SSISDatabase.mdf' , SIZE = 11264KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
 FILEGROUP [memory_optimized_filegroup_0] CONTAINS MEMORY_OPTIMIZED_DATA  DEFAULT
( NAME = N'memory_optimized_file_345491253', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.SHANMUKREDDY\MSSQL\DATA\memory_optimized_file_345491253' , MAXSIZE = UNLIMITED)
 LOG ON
( NAME = N'SSISDatabase_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.SHANMUKREDDY\MSSQL\DATA\SSISDatabase_log.ldf' , SIZE = 1280KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

ALTER DATABASE [SSISDatabase] SET COMPATIBILITY_LEVEL = 120
GO
 
By default In-Memory OLTP is not enabled in database. While create the you can specify the keyword CONTAINS MEMORY_OPTIMIZED_DATA

Or we can alter the database to set In-Memory OLTP

ALTER DATABASE Test
ADD FILEGROUP memory_optimized_data_filegroup CONTAINS MEMORY_OPTIMIZED_DATA

Once we created the in memory database then we can able to create the memory optimized tables.Let us take a simple OLTP table to demonstrate the In-Memory OLTP


CREATE TABLE [dbo].[emp_old_old](
       [empid] [int] NOT NULL,
       [name] [varchar](255) NULL,
PRIMARY KEY CLUSTERED
(
       [empid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

The above emp table contains clustered index. Let us insert some sample data to the above table

INSERT [dbo].[emp_old_old] ([empid], [name]) VALUES (1, N'malls')
INSERT [dbo].[emp_old_old] ([empid], [name]) VALUES (2, N'reddy')
INSERT [dbo].[emp_old_old] ([empid], [name]) VALUES (3, N'mamidi')
INSERT [dbo].[emp_old_old] ([empid], [name]) VALUES (4, N'naga')
INSERT [dbo].[emp_old_old] ([empid], [name]) VALUES (5, N'shanmukh')
INSERT [dbo].[emp_old_old] ([empid], [name]) VALUES (6, N'reddy')

Once data inserted into the table. Let us take the execution plan and statistics

Execution plan and statistics for normal table

statistics

Table 'emp_old_old'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Execution plan 




As from the above screen shot the data storage is RowStore and data is stored in row by row. verify the sub tree cost and compare the sub tree cost with the in memory table.

Then create the In-memory optimized table for the same table and load the same data and verify the statistics and execution plan

CREATE TABLE [dbo].[emp]
(
       [empid] [int] NOT NULL,
       [name] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

CONSTRAINT [emp_primaryKey] PRIMARY KEY NONCLUSTERED HASH
(
       [empid]
)WITH ( BUCKET_COUNT = 8)
)WITH ( MEMORY_OPTIMIZED = ON , DURABILITY = SCHEMA_AND_DATA )

Execution plan and statistics for In-memory table

 execution plan 


  From the above execution plan the storage of table is memory optimized. and subtree cost is very less compared to normal table with same data.

Alternatively we can use the Memory Optimization advisor for upgrading the existing table into In-Memory table.


Right Click on the selected table



It will open the table memory optimization advisor. Click on next. It will open memory optimization check-list



 All the above check-list should pass in order to proceed to next page. otherwise we cannot convert the table into In-memory table. Once all steps passed next page is memory optimization warnings.



Server All the above warning messages are informational messages and need to consider to avoid the usage of the above things given as warning.


Review optimization options to  rename the old table and optimization file path. It provides the ability to copy the old table data to new table data by check the check box copy table data to the new optimized table. by default its off.


Primary key conversion will convert the primary key index into the above type of indexes (non-clustered hash index or non-clustered index)


Summary tab will display information about the content that is going to migrate.




SQL Server introduced column store index which is a ready only non-clustered index for increasing OLAP performance. Now SQL server 2014 enhanced the column-store index for updating data.