Saturday 20 October 2012

Some useful blocking (or i/o blocking) useful query

use tempdb
--DBCC SQLPERF (LOGSPACE);
--DBCC SQLPERF (WAITSTATS)
--DBCC SQLPERF ('sys.dm_os_latch_stats', CLEAR);
--DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);
--CHECKPOINT
--DBCC FREEPROCCACHE
--DBCC DROPCLEANBUFFERS
--sp_whoisactive
--sp_who
--sp_who2
--dbcc opentran
--
go
--
-- real time ddl bottleneck
select  
    session_id
    ,wait_type
    ,wait_duration_ms
    ,resource_description
    from    sys.dm_os_waiting_tasks
    where    wait_type like 'PAGE%LATCH_%'
            and resource_description like '2:%'
    --sp_whoisactive
go
sp_spaceused
go
--
-- check dirty pages
SELECT    
    db_name(database_id) AS 'DatabaseName'
    ,count(page_id) AS 'DirtyPages'
    ,count(page_id)*8/1024 AS 'TotalSize(MB)'
    FROM    sys.dm_os_buffer_descriptors
    WHERE    is_modified =1
    GROUP BY db_name(database_id)
    ORDER BY count(page_id) DESC
go
--
-- temp db i/o wait stat
-- switch to any database and run stat for that db
SELECT
    DB_NAME(fs.database_id)    as    [Database]
    ,f.[Name]                  as    [LogicName]
    ,f.[filename]              as    [FileName]
    ,fs.[size_on_disk_bytes] / 1024 /1024  as    [Size(MB)]
    -- this seems to be off! data could be written by dropped later
    -- this is NOT space used
    ,fs.[num_of_bytes_written]/1024/1024  as    [TotalWrite(MB)]
    ,fs.[io_stall_write_ms]/fs.[num_of_writes] as [AverageWriteDelay(ms)]
    ,1024.00*1024*fs.[io_stall_write_ms]/fs.[num_of_bytes_written] as [AverageWrite Delay(ms/MB)]
    ,fs.[num_of_bytes_read]/1024/1024  as    [TotalRead(MB)]
    ,fs.[io_stall_read_ms]/fs.[num_of_reads] as [AverageReadDelay(ms)]
    ,1024.00*1024*fs.[io_stall_read_ms]/fs.[num_of_bytes_read] as [AverageReadDelay(ms/MB)]
    --,fs.*
    FROM    -- 2(tempdb database id), NULL(all the files related to tempdb)
            --sys.dm_io_virtual_file_stats(2, NULL) fs inner join sys.sysfiles f
            -- current database
            sys.dm_io_virtual_file_stats(db_id(), NULL) fs inner join sys.sysfiles f
            on fs.file_id = f.fileid
    WHERE [num_of_writes]>0 AND [num_of_reads]>0
    order by 3,6
go
-- reset all counters to 0
--DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR);
GO
--
-- i/o related wait stat
select
    ([signal_wait_time_ms]+0.0)/[waiting_tasks_count] as AverageSignalWait
    ,([wait_time_ms]-[signal_wait_time_ms]+0.0)/[waiting_tasks_count] as AverageOprationWait
    ,*
    from sys.dm_os_wait_stats
    where    (wait_type like 'PAGEIO%'
            or wait_type like '%IO_COMPLETION'
            or wait_type like 'DISKIO%'
            or wait_type like 'BACKUPIO%'
            or wait_type like 'WRITE%'
            ) and
            waiting_tasks_count>0
    order by wait_time_ms desc
go
--
-- temp table list
select
    crdate
    ,name
    ,refdate
    ,*
    from    tempdb.dbo.sysobjects  with (nolock)
    where    type='U'
            and name like '#%'
    order by 1 asc
go
SELECT
    [create_date]
    ,[modify_date]
    ,[name]
    ,[object_id]
    ,[principal_id]
    ,[schema_id]
    ,[parent_object_id]
    ,[lock_escalation]
    ,[lock_escalation_desc]
    ,[is_tracked_by_cdc]
    /*
    ,[type]
    ,[type_desc]
    ,[is_published]
    ,[is_schema_published]
    ,[lob_data_space_id]
    ,[filestream_data_space_id]
    ,[max_column_id_used]
    ,[lock_on_bulk_load]
    ,[uses_ansi_nulls]
    ,[is_replicated]
    ,[has_replication_filter]
    ,[is_merge_published]
    ,[is_sync_tran_subscribed]
    ,[has_unchecked_assembly_data]
    ,[text_in_row_limit]
    ,[large_value_types_out_of_row]
    */
    FROM    [tempdb].[sys].[tables]
    WHERE type='U' and is_ms_shipped=0
    order by 1,2
go
--
-- get session for temp table

DECLARE @FileName VARCHAR(MAX)

SELECT @FileName = SUBSTRING(path, 0, LEN(path)-CHARINDEX('\', REVERSE(path))+1) + '\Log.trc'
FROM sys.traces 
WHERE is_default = 1;

SELECT 
    o.name, 
    o.OBJECT_ID,
    o.create_date,
    gt.NTUserName,
    gt.HostName,
    gt.SPID,
    gt.DatabaseName,
    gt.TEXTData
    FROM    sys.fn_trace_gettable( @FileName, DEFAULT ) AS gt
            JOIN tempdb.sys.objects AS o 
            ON gt.ObjectID = o.OBJECT_ID
    WHERE    gt.DatabaseID = 2
            AND gt.EventClass = 46 -- (Object:Created Event from sys.trace_events)
            --AND o.create_date >= DATEADD(ms, -100, gt.StartTime) 
            --AND o.create_date <= DATEADD(ms, 100, gt.StartTime)
            and o.name='#53FF5DCD'
            --#023D5A04
    ORDER BY o.create_date
go
select
    ([signal_wait_time_ms]+0.0)/[waiting_tasks_count] as AverageSignalWait
    ,([wait_time_ms]-[signal_wait_time_ms]+0.0)/[waiting_tasks_count] as AverageOprationWait
    ,*
    from sys.dm_os_wait_stats
    where    waiting_tasks_count>0
    order by [wait_time_ms]
    desc 
go
--
-- what are we waiting for recently?
select * into #BufferWaitStats from sys.dm_os_wait_stats

-- wait ten seconds or so
select ws.wait_type,
      ws.waiting_tasks_count - bws.waiting_tasks_count as waiting_tasks_count,
      ws.wait_time_ms - bws.wait_time_ms as wait_time_ms,
      ws.max_wait_time_ms,
      ws.signal_wait_time_ms - bws.signal_wait_time_ms as signal_wait_time_ms
from sys.dm_os_wait_stats ws
join #BufferWaitStats bws
      on ws.wait_type = bws.wait_type
order by wait_time_ms desc

--clean up
drop table #BufferWaitStats
go


select 
    scheduler_id,
    current_tasks_count,
    runnable_tasks_count
from 
    sys.dm_os_schedulers
where 
    scheduler_id < 255


select top 50 
    sum(qs.total_worker_time) as total_cpu_time, 
    sum(qs.execution_count) as total_execution_count,
    count(*) as  number_of_statements, 
    qs.plan_handle 
from 
    sys.dm_exec_query_stats qs
group by qs.plan_handle
order by sum(qs.total_worker_time) desc

sp_whoisactive

select top 50
    sum(qs.total_worker_time) as total_cpu_time,
    sum(qs.execution_count) as total_execution_count,
    count(*) as  number_of_statements,
    qs.plan_handle
from
    sys.dm_exec_query_stats qs inner join dm_exec_text_query_plan qp on
    qs.plan_handle=qp.plan_handle
group by qs.plan_handle
order by sum(qs.total_worker_time) desc

go

--
-- monitor real time i/o stall
select
    database_id,
    db_name(database_id) as databaseName,
    file_id,
    io_type,
    io_stall,
    io_pending_ms_ticks,
    scheduler_address
from    sys.dm_io_virtual_file_stats(NULL, NULL) fs inner join
        sys.dm_io_pending_io_requests as ir on
        fs.file_handle = ir.io_handle
where    io_pending_ms_ticks>0
order by io_pending_ms_ticks desc


go
sp_helpfile


sp_whoisactive @get_plans=1


select * from sys.dm_io_pending_io_requests

Check All SQL Server Database or Individual File I/O stall stats

--
-- step 1: over all view across all databases

SELECT    DB_NAME(fs.database_id) AS [Database Name]
        ,ms.name as LogicName
        ,ms.type_desc
        ,io_stall/1000./3600. as [io_stall_hr]
        ,CAST((io_stall_read_ms + io_stall_write_ms)/(num_of_reads + num_of_writes) AS NUMERIC(10,1)) AS [avg_io_stall_ms] 
        ,num_of_reads + num_of_writes AS [total_io]
        ,num_of_reads
        ,num_of_writes
        ,CAST(io_stall_read_ms/(case when num_of_reads=0 then 1 else 0.0 end + num_of_reads) AS NUMERIC(25,2)) AS [avg_read_stall_ms]
        ,CAST(io_stall_write_ms/(case when num_of_writes=0 then 1.0 else 0.0 end +num_of_writes) AS NUMERIC(25,2)) AS [avg_write_stall_ms]
        ,io_stall_read_ms
        ,io_stall_write_ms
        ,io_stall_read_ms + io_stall_write_ms AS [io_stalls]
        ,fs.file_id
FROM    sys.dm_io_virtual_file_stats(null,null) fs inner join sys.master_files ms
        on ms.database_id=fs.database_id and ms.file_id=fs.file_id
--WHERE    (num_of_reads + num_of_writes)>0
WHERE    (num_of_reads + num_of_writes)>10
--ORDER BY (io_stall) desc;
ORDER BY avg_io_stall_ms DESC;

--
-- step 2: check each database individually
-- temp db i/o or any other database wait stat
-- switch to any database and run stat for that db
SELECT
    DB_NAME(fs.database_id)    as    [Database]
    ,f.[Name]                  as    [LogicName]
    ,f.[filename]              as    [FileName]
    ,cast((fs.[size_on_disk_bytes]+0.00) / 1024 /1024 as decimal(25,2)) as    [Size(MB)]
    -- this seems to be off! data could be written by dropped later
    -- this is NOT space used
    ,cast((fs.[num_of_bytes_written]+0.00)/1024/1024 as decimal(25,2)) as    [TotalWrite(MB)]
    ,cast((fs.[io_stall_write_ms]+0.00)/fs.[num_of_writes] as decimal(25, 2)) as [AverageWriteDelay(ms)]
    -- deal with 0 write or read case
    ,cast(1024.00*1024*fs.[io_stall_write_ms]/(case when fs.[num_of_bytes_written]=0 then 1 else fs.[num_of_bytes_written] end) as decimal(25, 2)) as [AverageWrite Delay(ms/MB)]
    ,cast((fs.[num_of_bytes_read]+0.00)/1024/1024 as decimal(25, 2)) as    [TotalRead(MB)]
    ,cast((fs.[io_stall_read_ms]+0.00)/(case when fs.[num_of_reads]=0 then 1 else fs.[num_of_reads] end) as decimal(25, 2)) as [AverageReadDelay(ms)]
    ,cast(1024.00*1024*fs.[io_stall_read_ms]/(case when fs.[num_of_bytes_read]=0 then 1 else fs.[num_of_bytes_read] end) as decimal(25, 2)) as [AverageReadDelay(ms/MB)]
    ,fs.[num_of_writes]
    ,fs.[num_of_reads]
    --,fs.*
    FROM    -- 2(tempdb database id), NULL(all the files related to tempdb)
            --sys.dm_io_virtual_file_stats(2, NULL) fs inner join sys.sysfiles f
            -- current database
            sys.dm_io_virtual_file_stats(db_id(), NULL) fs inner join sys.sysfiles f
            on fs.file_id = f.fileid
    WHERE [num_of_writes]>0 OR [num_of_reads]>0
    order by 3--,6

Indexes not used, Missing index and stats, table fragmentation for sql server

0)

select
object_name(I.object_id) as TableName
,I.name
,'drop index '+ I.name + ' on '+ object_name(I.object_id) as DropIndexStatement
,I.object_id
,I.index_id
--,I.*
from sys.indexes I inner join sys.objects O
on I.object_id=O.object_id
left join (select object_id, index_id from sys.dm_db_index_usage_stats where database_id=db_id()) S on
I.object_id=S.object_id and I.index_id=S.index_id
where
-- only looking for no clustered and non primary key indexes
--type_desc='NONCLUSTERED'
I.type=2
and I.is_primary_key=0

-- index not used so far
and (S.object_id is null or S.index_id is null)
-- excluding system tables
--and I.object_id>100
-- a better way to identify system tables
-- this is confirmed by the fact that the following query always return empty result set
-- select * from sys.objects where object_id<100 and is_ms_shipped<>1
-- otherwise we don't need sys.objects at all
and O.is_ms_shipped<>1
order by 1,2

1)
http://blog.sqlauthority.com/2011/01/03/sql-server-2008-missing-index-script-download/

SELECT TOP 25
    dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact
    ,db_name(dm_mid.database_id) as DatabaseName
    ,OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName]
    ,dm_mid.equality_columns
    ,dm_mid.inequality_columns
    ,dm_mid.included_columns
    ,dm_migs.last_user_seek AS Last_User_Seek
    ,dm_mid.database_id AS DatabaseID
    ,'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 1 desc--Avg_Estimated_Impact DESC
go
-- Missing Index Script
-- Original Author: Pinal Dave (C) 2011
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


2)
SELECT
    Last_Updated    = STATS_DATE(si.id, si.indid)
    ,TableName      = object_name(si.id)
    ,Name           = RTRIM(si.name)
    ,Size           = DATALENGTH (si.statblob)
FROM
    sysindexes si WITH (nolock)
WHERE OBJECTPROPERTY(si.id, N'IsUserTable') = 1
--AND INDEXPROPERTY (si.id , si.name , 'IsAutoStatistics' ) = 0
order by last_updated, tablename

3)

if (object_id('tempdb..#IndexStat') is not null) drop table #IndexStat;
SELECT
    --top 10
    db_name(DB_ID()) as DatabaseName
    ,object_name(s.object_id, db_id()) as TableName
    ,i.name as IndexName
    ,s.object_id AS objectid
    ,i.index_id AS indexid
    ,partition_number AS PartitionNumber
    ,avg_fragmentation_in_percent AS ExternalFragmentation
    ,s.page_count
    ,s.record_count
    into  #IndexStat
    FROM    sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL , NULL, 'LIMITED')  s  
            inner join sys.indexes i
            on s.index_id=i.index_id and s.object_id=i.object_id
    WHERE    --avg_fragmentation_in_percent > 5 AND
            s.index_id > 0 and
            s.database_id=DB_ID()
    --order by avg_fragmentation_in_percent desc;

SELECT top 10 * from #IndexStat order by ExternalFragmentation desc;


http://sqlserverplanet.com/dmv-queries/find-table-fragmentation
--
-- table fragmentation
SELECT
    Last_Updated    = STATS_DATE(si.id, si.indid)
    ,TableName      = object_name(si.id)
    ,Name           = RTRIM(si.name)
    ,Size           = DATALENGTH (si.statblob)
FROM
    sysindexes si WITH (nolock)
WHERE OBJECTPROPERTY(si.id, N'IsUserTable') = 1
--AND INDEXPROPERTY (si.id , si.name , 'IsAutoStatistics' ) = 0
order by last_updated, tablename

Or you can still do it the old fashioned way. Just substitute the name of the table or index below. It should not be in quotes.
–Just the table
DBCC SHOWCONTIG (tablename)
–Table with the index
DBCC SHOWCONTIG (tablename, indexname)
–Do not block anything (run during prod hours)
DBCC SHOWCONTIG (tablename, indexname) WITH FAST –(2005)


Saturday 13 October 2012

Send text message by email


websms.rogers.page.ca/2way

From http://smartcanucks.ca/how-to-send-free-text-messages-to-rogers-fido-telus-bell-virgin-cell-phones-canada/

you can easily send emails to your friends' cell phones directly from your email. You just need to know the number and the carrier of the recipient to start emailing them to their cell phone. Here is a list of the emails of different carriers. You need to replace the part before the "@" with your friend's number.
  • Rogers Wireless: [10-digit phone number]@pcs.rogers.com
  • Fido: [10-digit phone number]@fido.ca
  • Telus: [10-digit phone number]@msg.telus.com
  • Bell Mobility: [10-digit phone number]@txt.bell.ca
  • Kudo Mobile: [10-digit phone number]@msg.koodomobile.com
  • MTS: [10-digit phone number]@text.mtsmobility.com
  • President's Choice: [10-digit phone number]@txt.bell.ca
  • Sasktel: [10-digit phone number]@sms.sasktel.com
  • Solo: [10-digit phone number]@txt.bell.ca
  • Virgin: [10-digit phone number]@vmobile.ca
You need to replace the [10-digit phone number] with your friend's number. For example, If your friend's number was 613-123-4567 and their carrier was Rogers, you'd send an email to 6131234567@pcs.rogers.com