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

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
D
Docker
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
美团技术团队
V
V2EX
Last Week in AI
Last Week in AI
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
B
Blog RSS Feed
博客园_首页
B
Blog
博客园 - 叶小钗
I
InfoQ
WordPress大学
WordPress大学
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Latest news
Latest news
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
S
Secure Thoughts
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News

博客园 - 匡匡

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)