sp_now : rewrite sp_who2
This SP is very helpful to troubleshooting concurrent issue.
use master
go
if object_id('sp_now') is not null
drop procedure sp_now
go
create procedure [dbo].[sp_now]
@session_id int = null
,@xml_on bit = 0
,@text nvarchar(500) =''
,@back_on bit = 0
as
/***********************************************************************************************
**Object Name: dbo.sp_now
**
**Description: Revise sp_who2
**
**Input Parameters:
@session_id int = null
, @text nvarchar(500) =''
, @back_on bit = 0
**
**Return Value: N/A
**
**Return Result Set: session info
**
**Creator: Simon Cho
**
**Example: exec [sp_now]
**
*************************************************************************************************/
begin
set nocount on;
set transaction isolation level read uncommitted;
select
sp.spid as s_id
, max(sp.blocked) as BlkBy
, substring(max(convert(varchar(23),r.start_time,121)+sp.status), 24, 50) status
, rtrim(max(sp.loginame)) as Login
, rtrim(ltrim(max(sp.hostname))) as hostname
, min(r.start_time) as st_time
, convert(numeric(10,2),max(r.total_elapsed_time/1000)/60.0) [run_time(min)]
--, r.total_elapsed_time
, GETDATE() as current_datetime
, max(sp.waittime) as waittime
, substring(max(convert(varchar(23),r.start_time,121)+r.wait_type), 24, 50) wait_type
, db_name(max(sp.dbid)) as db
, substring(max(convert(varchar(23),r.start_time,121)+command), 24, 50) command
, max(r.cpu_time) as cpu_time
, max(r.reads) as reads
, max(r.writes) as writes
, max(r.logical_reads) as logical_reads
, rtrim(max(sp.program_name)) as program_name
, max(c.client_net_address) ip
, min(sp.login_time) as login_time
, convert(numeric(10,2),max(r.granted_query_memory)/128.0) as granted_query_memory_MB
--, t.objectid
, OBJECT_NAME(t.objectid, t.dbid) objname
, convert(xml,'<?Q--'+CHAR(13)+CHAR(10)+substring(max(case when text like 'Fetch%' then substring(text, 1,32) else text end)
, (case when (statement_start_offset/2)+1') as text_stmt_xml
--, max(t.text) as text
, case when @xml_on = 1 then (select query_plan from sys.dm_exec_query_plan(max(r.plan_handle)))
else convert(xml, '')
end as query_plan
from sys.sysprocesses sp with(nolock)
join sys.dm_exec_requests r with(nolock) on r.session_id=sp.spid
left join sys.dm_exec_connections c with(nolock) on r.connection_id=c.connection_id
outer apply sys.dm_exec_sql_text(r.sql_handle) t
where 1=1
-- and sp.spid!=@@SPID
and case when @session_id is null then 1 else sp.spid end
= case when @session_id is null then 1 else @session_id end
and case when @text='' then '1' else text end
like case when @text='' then '1' else '%'+@text+'%' end
and case when @back_on = 0 then sp.spid else 51 end > 50
and case when @back_on = 0 then sp.status else 'all' end != 'background'
group by sp.spid
, sp.dbid, t.dbid
, t.objectid
, r.statement_start_offset
, r.statement_end_offset
, r.transaction_id
, c.connection_id
, sp.blocked
end
--exec sys.sp_MS_marksystemobject 'sp_now'
Excel import using XML and T-SQL
TempDB Misconceptions around TF 1118
http://www.sqlskills.com/blogs/paul/misconceptions-around-tf-1118/
[Edit 2012:] 4a) What is Paul’s recommendation for using trace flag 1118? Everyone should turn it on, on all instances of SQL Server, from SQL Server 2000 onwards. There’s no down-side to having it turned on.
But there’s now even better guidance, and what I also recommend. At PASS in 2011, my good friend Bob Ward, who’s the top guy in SQL Product Support, espoused a new formula: if you have less than 8 cores, use #files = #cores. If you have more than 8 cores, use 8 files and if you’re seeing in-memory contention, add 4 more files at a time.
http://technet.microsoft.com/library/Cc966545
Use TF-1118. Under this trace flag SQL Server allocates full extents to each tempdb object, thereby eliminating the contention on SGAM page. This is done at the expense of some waste of disk space in tempdb. This trace flag has been available since SQL Server 2000. With improvements in tempdb object caching in SQL Server 2005, there should be significantly less contention in allocation structures. If you see contention in SGAM pages, you may want to use this trace flag. Cached tempdb objects may not always be available. For example, cached tempdb objects are destroyed when the query plan with which they are associated is recompiled or removed from the procedure cache.
Bulk load data conversion error (overflow) SQLINT (BCP)
http://msdn.microsoft.com/en-us/library/ms189110%28v=sql.90%29.aspx
Only SQLCHAR is allowed for fmt files if the file storage type is Char.
1 Data files that are stored in character format use char as the file storage type. Therefore, for character data files, SQLCHAR is the only data type that appears in a format file.
2 You cannot bulk import data into text, ntext, and image columns that have DEFAULT values.
When you bulk export data from an instance of SQL Server to a data file:
- You can always specify char as the file storage type.
- If you enter a file storage type that represents an invalid implicit conversion, bcp fails; for example, though you can specify int for smallint data, if you specify smallint for int data, overflow errors result.
- When noncharacter data types such as float, money, datetime, or int are stored as their database types, the data is written to the data file in the SQL Server native format.
Paging file for SQL server
Please don’t put 1.5 time of Physical memory.
Less amount is better for SQL server if you have a x64 machine and enough physical memory.
http://support.microsoft.com/kb/889654/en-us
http://blogs.msdn.com/b/buckwoody/archive/2010/06/29/the-windows-page-file-and-sql-server.aspx
http://www.brentozar.com/archive/2009/11/christian-bolton-on-sql-server-memory/
http://blogs.technet.com/b/askperf/archive/2007/12/14/what-is-the-page-file-for-anyway.aspx
http://www.brentozar.com/archive/2008/03/sql-server-2005-setup-checklist-part-1-before-the-install/
BACKUP DATABASE TO DISK = N‘NUL’
Veeam can truncate log base on some option.
It make backup chain broken.
When they perform truncate log, you can see 2 of the log like this.
SQL VSS is normal behavior to create SnapShot on disk.
But, Veeam Backup and replication with truncate log isn’t good option to manage database.
I recommend that do not enable truncate log on Veeam replication manager.
select top 10 *
from msdb.dbo.backupset
where is_snapshot=1
--It's due to SQL VSS.
--Program_Name : "Microsoft SQL Server VSS Writer"
--ex)
--BACKUP DATABASE [DBNAME] TO VIRTUAL_DEVICE='{4C5C20F9-FE32-4FCA-BE43-FFFFFFFFFFFF}1' WITH SNAPSHOT,BUFFERCOUNT=1,BLOCKSIZE=1024
select top 10 *
from msdb.dbo.backupmediafamily
where physical_device_name='NUL'
--It's due to Veeam.
--Program_Name : "Veeam Backup and Replication"
--ex)
--BACKUP LOG [DBNAME] TO DISK = 'NUL'
uniqueidentifier : GUID in SQL
1. newsequentialid()
http://msdn.microsoft.com/en-us/library/ms189786%28v=sql.100%29.aspx
This is one of the function to generate sequence GUID to reduce fragmentation.
But, it’s not grantee uniqueness.
It’s wrapped by windows function UuidCreateSequential, http://msdn.microsoft.com/en-us/library/aa379322%28VS.85%29.aspx.
Creates a GUID that is greater than any GUID previously generated by this function on a specified computer since Windows was started.
After restarting Windows, the GUID can start again from a lower range, but is still globally unique.
When a GUID column is used as a row identifier, using NEWSEQUENTIALID can be faster than using the NEWID function.
This is because the NEWID function causes random activity and uses fewer cached data pages. Using NEWSEQUENTIALID also helps to completely fill the data and index pages.
2. uniqueidentifier sorting.
http://sqlblog.com/blogs/alberto_ferrari/archive/2007/08/31/how-are-guids-sorted-by-sql-server.aspx
3. uniqueidentifier converting
SELECT CONVERT(uniqueidentifier, 0x000102030405060708090A0B0C0D0E0F) UNION ALL SELECT CONVERT(uniqueidentifier, 0x00000000000000000123456789ABCDEF) UNION ALL SELECT CONVERT(uniqueidentifier, 0x000000000123456789ABCDEF00000000) UNION ALL SELECT CONVERT(UNIQUEIDENTIFIER, '00000000-0123-4567-89AB-CDEF00000000')
Contact information
simon@simonsql.com
Tuning, Consulting and all DB related questions.