Log table access using SQL Server Profiler

本小妞迷上赌 提交于 2019-11-28 04:07:39

问题


Is there a way to use Profiler to determine whether a table is being accessed by queries?

I saw an event named Object:Opened (Indicates when an object has been accessed, such as for SELECT, INSERT, or DELETE statements) and Object:Closed, but these do not seem to work.

In particular, I created a simple trace with both Object:Opened and Object:Closed with no filters (except the standard "Application Name not like 'SQL Profiler'" filter) and ran SELECT TOP 1 * FROM TableName, but no events were reported.

So, is there a way to use Profiler to determine if a table is being SELECTed from?


回答1:


It may help to investigate the locks SQL is acquiring. Select statements will generally aquire shared Locks (LCKMS), so you can filter for this.

In profiler look for the Locks:Acquired event. The ObjectID will resolve to the table which you can easily lookup with OBJECT_NAME(objectid). The Mode will tell you the kind of lock being acquired shared locks are 3. For more information look here.




回答2:


There is a way to do that with Profiler, but it's got a heck of a performance impact.

Instead, can you clarify your question with the version of SQL Server you're using? If you're using SQL Server 2008, look into the Audit object, which is designed to do exactly that, plus it's got a very low performance impact.

Here's an article explaining how to set up an audit:

Implementing Security Audits in SQL Server 2008

Other posters have noted that you can filter TextData on object name, but that doesn't work if someone uses a view to access the object.




回答3:


I have found a way through profiler, the EventClass "Audit Schema Object Access Event".

Whilst I discovered this independantly after some digging I found this excellent article;

http://www.databasejournal.com/features/mssql/article.php/3887996/Determining-Object-Access-Using-SQL-Server-Profiler.htm

Regards,

Darren.




回答4:


I'm not seeing those in SQL Server 2005.

In my experience, I look at SQL:StmtStarting AND SP:StmtStarting - you can filter TextData on %TABLE_NAME%. This will even catch things inside SPs when you use SP:StmtStarting.

It's not bulletproof, because it has to use the LIKE syntax, but it might get you what you are looking for.



来源:https://stackoverflow.com/questions/296906/log-table-access-using-sql-server-profiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!