SQLServer Note
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)
SQLServer Note的更多相关文章
- [转]在SqlServer 中解析JSON数据
在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...
- Change Tracking of SQLServer
1.Enable the change tracking at the database level. ALTER DATABASE AdventureWorks2008 SET CHANGE_TRA ...
- Declare Cusror of SQLServer
Defines the attributes of a Transact-SQL server cursor, such as its scrolling behavior and the query ...
- SQLSERVER执行计划详解
序言 本篇主要目的有二: 1.看懂t-sql的执行计划,明白执行计划中的一些常识. 2.能够分析执行计划,找到优化sql性能的思路或方案. 如果你对sql查询优化的理解或常识不是很深入,那么推荐几骗博 ...
- 如何把SQLServer数据库从高版本降级到低版本?
http://blog.csdn.net/dba_huangzj/article/details/7952403 由于目前还广泛使用着SQLServer2000,很多公司又想使用新的SQLServer ...
- 网站sqlserver提权操作
在入侵过程中,得到SQLserver的权限,想进一步得到system权限的方法总结 *************************** 利用xp_cmdshell **************** ...
- Tomcat - java.lang.UnsupportedClassVersionError:Unsupported major.minor version 51.0 (unable to load class com.microsoft.sqlserver.jdbc.SQLS
今天使用Tomcat连接sql Server 2008 enterprise的时候,报错: HTTP Status 500 - Servlet execution threw an exception ...
- Telnet运用and Sqlserver connection failed
今天的工作中,需要远程访问服务器上的数据库.但是,连接错误,Error code is 1326.说句实话,关于SqlServer 不能远程访问这个问题,我遇到过N次.可是每次都不认真去研究到底是什么 ...
- 史上最大的CPU Bug(幽灵和熔断的OS&SQLServer补丁)
背景 最近针对我们的处理器出现了一系列的严重的bug.这种bug导致了两个情况,就是熔断和幽灵. 这就是这几天闹得人心惶惶的CPU大Bug.消息显示,以英特尔处理器为代表的现代CPU中,存在可以导致数 ...
随机推荐
- <META http-equiv=X-UA-Compatible content=IE=EmulateIE7>
未来兼容性中的 META 标记和锁定 注意:本文档是预备文档,随时可能变更. 对于 Web 开发人员来说,文本兼容性是一个要考虑的重要问题.Windows Internet Explorer 8 引入 ...
- android之打开网页
首先改写strings.xml文件 代码如下: <resources> <string name="app_name">Intent应用</strin ...
- Java泛型总结
1. 什么是泛型?泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的 ...
- 通过sqlplus 登录数据库服务器
点击“运行”,输入"sqlplus",弹出 , 再往里面输入账号scott,密码tiger 或者,点击“运行”,输入"sqlplus /nolog",此时只打开 ...
- Symantec更新服务器
HTTP liveupdate.symantec.com liveupdate.symantecliveupdate.com FTP update.symantec.com/opt/content ...
- Powershell变量的类型
Powershell 默认支持的.NET类型如下: [order], [pscustomobject], [array], [bool], [byte], [char], [datetime], ...
- 在CSV文件中增加一列属性值
具体参见:系统管理\将文件夹复制到列表中的远程主机 修改前: column1, column2 1,b 2,c 3,5 修改后: column1, column2, column3 1,b, ...
- SonarQube代码质量管理平台安装与使用
Sonar简介 Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从七个维度检测代码质量 通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaS ...
- Uncaught RangeError: Maximum call stack size exceeded解决思路
今天突然碰到这样的JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 这个翻译过来就是堆栈溢出了. 1.原因:有小类到 ...
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...