snippetsqlModerate
SQL Server how to detect unused views and/or get usage stats
Viewed 0 times
sqlunusedandstatsviewsusagegethowserverdetect
Problem
Is there a way to determine if a view is no longer being used (without removing them)?
Ideally I would like to know the views' usage for SQL Server 2000 and 2012.
I am upgrading some databases and suspect that many of the views are no longer being used. Also, some of the views will be difficult to compile on the new server as they access multiple databases, some of which are not being moved to the new server.
Ideally I would like to know the views' usage for SQL Server 2000 and 2012.
I am upgrading some databases and suspect that many of the views are no longer being used. Also, some of the views will be difficult to compile on the new server as they access multiple databases, some of which are not being moved to the new server.
Solution
Unfortunately if you want to be 100% accurate, the plan cache isn't going to cut it, because there are all types of scenarios in SQL Server where a plan may not get cached at all. For example, OPTION(RECOMPILE), zero cost plans, optimize for ad hoc workloads and single use plan stubs, etc.
Since you want a method that works across 2000-2012, your only real option to guarantee you catch the usage is to use a server-side trace with the
http://msdn.microsoft.com/en-us/library/ms175013.aspx
You will want to filter on the
http://msdn.microsoft.com/en-us/library/ms180953.aspx
Then setup a job that pulls the file data in and aggregates the counts up every couple of hours (or even days depending on the data generation rate), and you'll be able to accurately trace out the accesses that are occurring.
Yeah I hate Trace as much as the next guy, but this is one of those scenarios where it is the right tool for the task at hand.
FWIW, on 2012 you could use Server Audit or Extended Events for this to track the object access as well, but once you create the Trace definition for 2000, it should be portable for the most part to 2012, and you can use Profiler to generate the scripts to make it much easier to work with.
Since you want a method that works across 2000-2012, your only real option to guarantee you catch the usage is to use a server-side trace with the
Audit Database Object Access event:http://msdn.microsoft.com/en-us/library/ms175013.aspx
You will want to filter on the
ObjectType = 8278 so that you only catch Views being accessed:http://msdn.microsoft.com/en-us/library/ms180953.aspx
Then setup a job that pulls the file data in and aggregates the counts up every couple of hours (or even days depending on the data generation rate), and you'll be able to accurately trace out the accesses that are occurring.
Yeah I hate Trace as much as the next guy, but this is one of those scenarios where it is the right tool for the task at hand.
FWIW, on 2012 you could use Server Audit or Extended Events for this to track the object access as well, but once you create the Trace definition for 2000, it should be portable for the most part to 2012, and you can use Profiler to generate the scripts to make it much easier to work with.
Context
StackExchange Database Administrators Q#39596, answer score: 15
Revisions (0)
No revisions yet.