惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 匡匡

Vue 父子组件通信方式 Vue: 组件扩展 WebClient 指定出口 IP IIS8 下 JS, CSS 等静态文件出现 500 错误 使用 ffmpeg 转换 mov 视频 使用 ildasm 和 ilasm 修改程序集的的引用信息 2020-01-08 工作日记:无题 .Net Core 程序集管理说明(加载) .NET CORE 动态加载 DLL 的问题 ASP.NET 后台 COOKIE 的设置 Nginx深入详解之upstream分配方式 使用 HttpWebRequest 类做 POST 请求没有应反 webpack 里的 import, exports 实现原理 使用 pdf.js 查看发票时,显示不了台头和印章的解决办法 Flex 布局里 input 宽度最小 150px 的问题, 浏览器 BUG? 使用像素单位设置 EXCEL 列宽或行高 sweetalert 快速显示两个提示, 第二个显示不出的问题 加权轮询和加权随机算法 在 Docker 中部署 ASP.NET CORE 应用
使用 sql server 默认跟踪分析执行的 SQL 语句
匡匡 · 2019-11-15 · via 博客园 - 匡匡

如果没有启用 SQL SERVER 的跟踪器来跟踪 SQL SERVER 的 SQL 执行情况,又想查最近的 SQL 执行情况,网上一般说是使用 LogExprorer 这个工具,网上找了这个工具很久也没有找到。

今天找了到了一篇文章,原因 SQL SERVER 现在有个默认跟踪文件,默认情况下,是启用了 SQL 的跟踪:

https://www.cnblogs.com/DBFocus/archive/2010/05/19/1739535.html

首先使用 select * from sys.configurations where configuration_id = 1568 查一下是否默认有打开默认跟踪,我查询了几台服务器,默认都是打开的,说明这个功能默认是打开的。

如果没有打开,就使用:

sp_configure 'show advanced options', 1;
go
reconfigure;
go
sp_configure 'default trace enabled', 1;
go
reconfigure;
go

来打开。

使用 select * from ::fn_trace_getinfo(0) 可以获取跟踪文件保存的路径,默认跟踪的数据保存在文件中。

使用以下语句查询相关的信息:

select * from from ::fn_trace_gettable('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\log.trc',0)

整理一个:

select
loginname,
loginsid,
spid,
hostname,
applicationname,
servername,
databasename,
objectname,
e.category_id,
cat.name as [CategoryName],
textdata,
starttime,
eventclass,
eventsubclass, --0表示begin,1表示commit
e.name as EventName
from ::fn_trace_gettable('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\log.trc',0)
inner join sys.trace_events e
on eventclass = trace_event_id
inner join sys.trace_categories as cat
on e.category_id = cat.category_id
where databasename = 'TraceDB' and
objectname is null and --根据objectname来过滤
e.category_id = 5 and --category 5表示对象
e.trace_event_id = 46 --trace_event_id: 46表示Create对象,47表示Drop对象,164表示修改对象

查询修改表的信息:

where databasename = 'TraceDB' and
objectname = 'MyTable' and
e.category_id = 5 and
e.trace_event_id = 164

查义删除表的信息:

where databasename = 'TraceDB' and
objectname = 'MyTable' and
e.category_id = 5 and
e.trace_event_id = 47

Default Trace还能跟踪到其他一些事件。例如你的日志文件快速增长,这时需要知道其原因。Default Trace会捕获日志增长事件,这对于排查问题很有价值。下面的查询会获得Default Trace中所有的log auto growth事件。

select
loginname,
loginsid,
spid,
hostname,
applicationname,
servername,
databasename,
objectname,
e.category_id,
cat.name,
textdata,
starttime,
endtime,
duration,
eventclass,
eventsubclass,
e.name as EventName
from ::fn_trace_gettable('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\log.trc',0)
inner join sys.trace_events e
on eventclass = trace_event_id
inner join sys.trace_categories as cat
on e.category_id = cat.category_id
where databasename = 'TraceDB' and
e.category_id = 2 and --categroy 2表示database
e.trace_event_id = 93 --93表示日志文件自动增长事件

与跟踪相关的函参考:

https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008-r2/ff848738(v=sql.105)