Monday, February 24, 2014

sql server concatenation

TSQL provides 2 ways to concatenate data, the + sign and the new CONCAT() function. This tip will cover the differences in the two, so you can achieve the expected behavior in your code.

The way most us are used to concatenating data together is using the + sign. The problem we run into using this method is it expects the different items being concatenated together to be of the same data type. You have to perform an explicit conversion if they are not the same data type. The following example is trying to concatenate a INT and a String together, you can see SQL cannot handle the different data types.

Refer:

http://www.mssqltips.com/sqlservertip/3014/concatenation-of-different-sql-server-data-types/?utm_source=dailynewsletter&utm_medium=email&utm_content=headline&utm_campaign=20140224

sql server performance optimization steps

 sql server performance optimization steps

 Investigate first and take action.

Step : 1  Check block query


              select *from sys.sysprocesses WHERE BLOCKED >=1

             check SP ID which query/SP is blocked

              dbcc inputbuffer(<<SPID>>)

              Kill SP ID

              Kill <<SPID>>


Step : 2   DBCC checkdb

Step : 3  Sp_updatestats

Step : 4  sp_msforeachdb 'checkpoint'

Step : 5  Some use begin_tran forgot to commit  statement. Because of blocking happen.
              Action :  Restart sql service.

Step : 6  CPU usage goes high, No doubt missed index in your database.  using below link to find missed index and create indexes.

http://selvasqlserversolutions.blogspot.in/2014/02/missing-index-script-sql-server.html





Missing Index Script - sql server

Performance Tuning is quite interesting and Index plays a vital role in it.

A proper index can improve the performance and a bad index can hamper the performance.

Here is the script from my script bank which I use to identify missing indexes on any database.

Please note, if you should not create all the missing indexes this script suggest. This is just for guidance. You should not create more than 5-10 indexes per table. Additionally, this script sometime does not give accurate information so use your common sense.

Query :

SELECT TOP 25
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') +
CASE
WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
END
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')
+ ']'
+ ' ON ' + dm_mid.statement
+ ' (' + ISNULL (dm_mid.equality_columns,'')
+ CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN ',' ELSE
'' END
+ ISNULL (dm_mid.inequality_columns, '')
+ ')'
+ ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC
GO

Thursday, February 6, 2014

SQL SERVER – List all the database and Find the Size of Database File – Find the Size of Log File




SQL SERVER - List all the database


EXEC sp_databases
EXEC sp_helpdb

old version

SELECT name
FROM sys.databases
SELECT name
FROM sys.sysdatabases

===================================================

 Find the Size of Database File – Find the Size of Log File



SELECT DB_NAME(database_id) AS DatabaseName,
Name AS Logical_Name,
Physical_Name, (size*8)/1024 SizeMB
FROM sys.master_files
WHERE DB_NAME(database_id) = 'employee'
GO