SQL Server中追踪器Trace的介绍和简单使用
一.What is Trace? 对于 SQL Profiler这个工具相信大家都不是很陌生,没用过的朋友可以在SQL Server Management Studio工具SQL Server Profiler处使用。这个工具是用来监控SQL,存储过程的执行,用户登录等等信息。但这个工具只是一个GUI,他的本质就是Trac
一.What is Trace?
对于SQL Profiler这个工具相信大家都不是很陌生,没用过的朋友可以在SQL Server Management Studio>工具>SQL Server Profiler处使用。这个工具是用来监控SQL,存储过程的执行,用户登录等等信息。但这个工具只是一个GUI,他的本质就是Trace。下面是Trace的架构:
数据库引擎会产生一系列事件,然后各个trace可以去订阅自己感兴趣的事件,一旦数据库产生了相关事件就会发给订阅该事件的trace,各个trace通过自己的过滤器对该事件的信息过滤(例如:捕获执行时间超过1秒的SQL语句),然后放到一个缓冲队列里,最终写入文件或者是一些客户端应用。
简单介绍完理论,下面就开始实践:
How to Create a Trace?
要创建一个追踪器,总共分三步:
1.执行存储过程sp_trace_create创建一个追踪器
2.执行存储过程sp_trace_setevent添加自己想订阅的事件以及最终结果集的列名
3.执行存储过程sp_trace_setfilter设置过滤器来对过滤产生数据
下面是一个创建一个追踪器的SQL脚本
; ; ; ; sp_trace_create 7 @traceid OUTPUT , , , , ; sp_trace_setevent , , , ; sp_trace_setevent , , , ; sp_trace_setevent , , , ; sp_trace_setevent , , , ; sp_trace_setevent , , , ; sp_trace_setevent , , , ;; ; sp_trace_setfilter , , , , ; TraceID;
对于第九行中的C:\TraceFiles\LongRunningQueries,请确保TraceFiles文件夹存在,而LongRunningQueries是文件名,创建后会自动加上.trc后缀。
对于过滤器中具体的事件以及列名,大家可以参考:(v=sql.105).aspx
对于像我一样的一些初学者,写上面的脚本可能比较吃力,那么我们可以通过SQL Server Profiler配置各种需求,然后导出脚本:
然后把路径等一系列其他参数设置下即可。
三.How to Operate a Trace?
上面我们已经把创建了一个追踪器,但这个追踪器目前并未开始运行,我们可以通过下面的脚本来查看trace的状态
sys.traces
执行之后你会发现有2个trace记录,第一个是SQL Server默认的trace,它提供极其有限的功能,第二个就是我们刚刚创建的trace
status就是指追踪器的状态
@status Action
0 Stops the trace
1 Starts the trace
2 Closes the trace and deletes its definition
操作status的脚本:
; ; sp_trace_setstatus ,; sp_trace_setstatus ,; sp_trace_setstatus ,;-- start trace
四.How to Viewing Trace Data?
前面已经说过,追踪器最终把追踪到的信息写到了一个文件里,也就是我们创建时指定的路径。通过一个系统函数就可以查看分析这些数据了:
fn_trace_gettable(N,DEFAULT);
五.Summary
SQL Trace对象提供服务器端的追踪的技术,包括一些存储过程,,数据库,视图来创建trace, 通过使用T-SQL和trace暴露的元数据来管理trace数据,这些都是SQL Profiler所无法提供的

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

It is possible to recover an Apple phone if it is lost and turned off. The method is also very simple. Users can choose to log in to the official iCloud website to search, or a friend who also uses an Apple phone can use his phone to search for your iPhone. How to track the precise location of an Apple phone if it is lost and turned off? Answer: Search on the official iCloud website or borrow someone else's iPhone device to find it. 1. Users find that their Apple phone is lost or missing, and it can be found even if it is turned off. 2. Users directly log in to the iCloud official website, click Find My iPhone, and be sure to enter the correct account number. 3. Make sure your account is consistent with the account of the lost phone so that you have a chance to recover the phone. 4. If the phone is turned on and connected

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.
