博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQLServer Note
阅读量:6414 次
发布时间:2019-06-23

本文共 5057 字,大约阅读时间需要 16 分钟。

1. Grant necessory permission to user account, so it can use SQL profiler.

USE master

GRANT ALTER TRACE TO TEST;
GRANT VIEW SERVER STATE TO TEST;
USE testDB
GRANT VIEW DATABASE STATE TO TEST;
GRANT SHOWPLAN TO TEST;

 

2. SQL Server Computed Column Example

CREATE TABLE dbo.Products 

(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
, InventoryValue AS QtyAvailable * UnitPrice persisted
);

-- Insert values into the table.

INSERT INTO dbo.Products (QtyAvailable, UnitPrice)
VALUES (25, 2.00), (10, 1.5);

-- Display the rows in the table.

SELECT ProductID, QtyAvailable, UnitPrice, InventoryValue
FROM dbo.Products;

ALTER TABLE dbo.Products ADD RetailValue AS (QtyAvailable * UnitPrice * 1.35);

ALTER TABLE dbo.Products DROP COLUMN RetailValue;

GO
ALTER TABLE dbo.Products ADD RetailValue AS (QtyAvailable * UnitPrice * 1.5) persisted;

 

3. Useful SQL statement using DMV

------------------------find the most cost missing index---------------------

set transaction isolation level read uncommitted

select top 20

round(s.avg_total_user_cost * s.avg_user_impact * (s.user_seeks+s.user_scans),0) as [Total Cost]
, s.avg_user_impact
, d.statement as tableName
, d.equality_columns
, d.inequality_columns
, d.included_columns
from sys.dm_db_missing_index_groups g
inner join sys.dm_db_missing_index_group_stats s
on s.group_handle=g.index_group_handle
inner join sys.dm_db_missing_index_details d
on d.index_handle = g.index_handle
order by [Total Cost] DESC

------Populate tables with sp_executesql---------
set nocount on;
go
DECLARE @val INT
SELECT @val=1
WHILE @val < 100000
BEGIN
exec sp_executesql N'INSERT INTO dbo.Customer(CustomerID, CustomerName) VALUES(@v1,@v2)', N'@v1 int,@v2 int',@v1=@val,@v2=@val;
SELECT @val=@val+1
END
GO

-----identify the top 20 slowest sql -----

select top 20

cast(qs.total_elapsed_time/1000000 as decimal(28,2)) as [Total Elaspsed Duration(s)],
qs.execution_count,

SUBSTRING

(
qt.text,
qs.statement_start_offset/2+1,
(case when qs.statement_end_offset = -1 then len(convert(NVARCHAR(MAX),qt.text)) * 2 else qs.statement_end_offset end - qs.statement_start_offset/2)+ 1
)
as [Individual Query]
,
qt.text as [Parent Query],
DB_NAME(qt.dbid) as [Database Name],
qp.query_plan

from sys.dm_exec_query_stats qs

cross apply sys.dm_exec_sql_text(qs.sql_handle) qt
cross apply sys.dm_exec_query_plan(qs.plan_handle) qp
order by total_elapsed_time

----------identify what SQL is running now---------

select

er.session_id AS [SPID]
, sp.ecid
, db_name(sp.dbid) as [Database]
, sp.nt_username
, er.status
, er.wait_type
, SUBSTRING
(
qt.text,
er.statement_start_offset/2+1,
(case when er.statement_end_offset = -1 then len(convert(NVARCHAR(MAX),qt.text)) * 2 else er.statement_end_offset end - er.statement_start_offset/2)+ 1
)
as [Individual Query]
, qt.text as [Parent Query]
, sp.program_name
, sp.hostname
, sp.nt_domain
, er.start_time
from sys.dm_exec_requests er
inner join sys.sysprocesses sp on er.session_id = sp.spid
cross apply sys.dm_exec_sql_text(er.sql_handle) as qt
where er.session_id>50
and er.session_id NOT in(@@spid)
order by er.session_id, ecid

------------------ if xxx is not null-----------------

IF (select 1 from sys.indexes where name='PK_CUSTOMERID' and object_id=object_id('dbo.customer')) is not null

BEGIN
DROP INDEX DBO.CUSTOMER.PK_CUSTOMERID;
END

create index [PK_CUSTOMERID] ON DBO.CUSTOMER(CUSTOMERID) include(customername)

-----------------------------fn_get_sql  and sys.dm_exec_sql_text function ------------------------------------

select * from fn_get_sql(0x0300FF7F8FB46906B80B0F01D49A00000100000000000000);

select * from sys.dm_exec_query_stats cross apply fn_get_sql(sql_handle);

select * from sys.dm_exec_query_stats cross apply sys.dm_exec_sql_text(sql_handle);

fn_get_sql and sys.dm_exec_sql_text are the same

 

-----------------------------Query deadlock xml value------------------------------------

declare @deadlock xml

set @deadlock = 'put your deadlock graph here'

select

[PagelockObject] = @deadlock.value('/deadlock-list[1]/deadlock[1]/resource-list[1]/pagelock[1]/@objectname', 'varchar(200)'),
[DeadlockObject] = @deadlock.value('/deadlock-list[1]/deadlock[1]/resource-list[1]/objectlock[1]/@objectname', 'varchar(200)'),
[Victim] = case when Deadlock.Process.value('@id', 'varchar(50)') = @deadlock.value('/deadlock-list[1]/deadlock[1]/@victim', 'varchar(50)') then 1 else 0 end,
[Procedure] = Deadlock.Process.value('executionStack[1]/frame[1]/@procname[1]', 'varchar(200)'),
[LockMode] = Deadlock.Process.value('@lockMode', 'char(1)'),
[Code] = Deadlock.Process.value('executionStack[1]/frame[1]', 'varchar(1000)'),
[ClientApp] = Deadlock.Process.value('@clientapp', 'varchar(100)'),
[HostName] = Deadlock.Process.value('@hostname', 'varchar(20)'),
[LoginName] = Deadlock.Process.value('@loginname', 'varchar(20)'),
[TransactionTime] = Deadlock.Process.value('@lasttranstarted', 'datetime'),
[InputBuffer] = Deadlock.Process.value('inputbuf[1]', 'varchar(1000)')
from @deadlock.nodes('/deadlock-list/deadlock/process-list/process') as Deadlock(Process)

 

转载地址:http://dgbra.baihongyu.com/

你可能感兴趣的文章
topcoder srm 545 div1
查看>>
PowerShell收发UDP消息包
查看>>
ios 开发日记 13-剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
查看>>
数组去重的几种方式
查看>>
Pascal's Triangle II
查看>>
2019-2-22集合作业
查看>>
页面导航
查看>>
算法:二叉搜索树的后序遍历序列
查看>>
System.Net.WebException: 请求因 HTTP 状态 503 失败
查看>>
拓展随记
查看>>
服务器远程链接
查看>>
所谓的日常 #6 - 焚金闕董卓行兇 匿玉璽孫堅背約
查看>>
[Winform]DataGridView列自适应宽度
查看>>
进程与线程
查看>>
编写css让div2在div1的右下角?
查看>>
将NSString写入到文件中
查看>>
SpringTask定时任务
查看>>
Log4j之使用demo
查看>>
会议02
查看>>
人月神话读后感
查看>>