Query last time a view, funcion or sp was last used/read
I use sys.dm_db_index_usage_stats.last_user_seek
, sys.dm_db_index_usage_stats.last_user_scan
and sys.dm_db_index_usage_stats.last_user_lookup
to query last time a table was read.
For views, functions and stored procedures I'm using sys.objects
but it doesn't have any field for read datetime.
Is there any system table where I can see the last time these types of objects were read/used?
My objective is to find objects that aren't being used and possibly drop them.
sql-server sql-server-2012
add a comment |
I use sys.dm_db_index_usage_stats.last_user_seek
, sys.dm_db_index_usage_stats.last_user_scan
and sys.dm_db_index_usage_stats.last_user_lookup
to query last time a table was read.
For views, functions and stored procedures I'm using sys.objects
but it doesn't have any field for read datetime.
Is there any system table where I can see the last time these types of objects were read/used?
My objective is to find objects that aren't being used and possibly drop them.
sql-server sql-server-2012
add a comment |
I use sys.dm_db_index_usage_stats.last_user_seek
, sys.dm_db_index_usage_stats.last_user_scan
and sys.dm_db_index_usage_stats.last_user_lookup
to query last time a table was read.
For views, functions and stored procedures I'm using sys.objects
but it doesn't have any field for read datetime.
Is there any system table where I can see the last time these types of objects were read/used?
My objective is to find objects that aren't being used and possibly drop them.
sql-server sql-server-2012
I use sys.dm_db_index_usage_stats.last_user_seek
, sys.dm_db_index_usage_stats.last_user_scan
and sys.dm_db_index_usage_stats.last_user_lookup
to query last time a table was read.
For views, functions and stored procedures I'm using sys.objects
but it doesn't have any field for read datetime.
Is there any system table where I can see the last time these types of objects were read/used?
My objective is to find objects that aren't being used and possibly drop them.
sql-server sql-server-2012
sql-server sql-server-2012
asked Aug 15 '16 at 15:02
HikariHikari
57031828
57031828
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
Unfortunately, there is no 100% accurate way to determine this. However, you can get as close as possible by examining all code that could potentially make DB calls, as well as the definitions of all of your DB objects and SQL Server Agent jobs. There are often code paths that are infrequently accessed and hence could easily not show up in DMVs, extended events, SQL Profiler traces, or even custom logging. Checking ALL code will get you as close as possible.
The only area left out of an exhaustive code search is ad hoc queries that are not in code anywhere. In this case DMVs, traces, etc at least have the potential to identify usage, but are certainly no guarantee.
add a comment |
Unless you have a server side trace running or XEvent running, there is no guaranteed way of telling a particular object is used or not. Some objects might be referenced by a report that is ran once a year by your CIO or business user. So unless you have a concrete data for your business life cycle, there is always a room for error.
I would suggest that once you identify possible candidates for removal, do not drop them, instead rename them and after a year or so, drop them.
When using DMVs you need to log them to a persisted table as DMV data get flushed out due to instance restart, certain sp_configure
changes, etc.
I have answered a similar question here that will get you started.
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
add a comment |
Your best bet without built in logging would be using the below query:
select s.last_execution_time
from sys.dm_exec_query_stats s
cross apply sys.dm_exec_query_plan (s.plan_handle) p
where object_name(p.objectid, db_id('DBNAME')) = @proc_nm
Thanks, butobject_name(p.objectid, db_id('DBNAME'))
is null for all records I got.
– Hikari
Aug 17 '16 at 13:10
add a comment |
There is a dedicated DMV for stored procedures.
select DB_NAME(eps.database_ID) AS DatabaseName
, OBJECT_NAME(eps.object_ID) AS ObjectName
, eps.type_desc
, eps.last_execution_time
, eps.execution_count
from sys.dm_exec_procedure_stats eps
WHERE database_id = DB_ID()
The DMV will show the execution stats since the last server restart or detach (might need to confirm that). Meaning if a stored procedure has not been run since the database was attached or restarted, there will be no entry in this dmv. You can join it against the object table to find NULL's and assume what has not been run.
If you need trigger activity, you can use the same query as above except use sys.dm_exec_trigger_stats
as the table.
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
add a comment |
For DMV to give correct results for all databases, we can modify the script shared by @dfundako as below:
SELECT
DB_NAME(eps.database_ID) AS DatabaseName
,OBJECT_NAME(eps.object_ID, eps.database_id) AS ObjectName
,eps.type_desc
,eps.last_execution_time
,eps.execution_count
FROM sys.dm_exec_procedure_stats eps
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f146847%2fquery-last-time-a-view-funcion-or-sp-was-last-used-read%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Unfortunately, there is no 100% accurate way to determine this. However, you can get as close as possible by examining all code that could potentially make DB calls, as well as the definitions of all of your DB objects and SQL Server Agent jobs. There are often code paths that are infrequently accessed and hence could easily not show up in DMVs, extended events, SQL Profiler traces, or even custom logging. Checking ALL code will get you as close as possible.
The only area left out of an exhaustive code search is ad hoc queries that are not in code anywhere. In this case DMVs, traces, etc at least have the potential to identify usage, but are certainly no guarantee.
add a comment |
Unfortunately, there is no 100% accurate way to determine this. However, you can get as close as possible by examining all code that could potentially make DB calls, as well as the definitions of all of your DB objects and SQL Server Agent jobs. There are often code paths that are infrequently accessed and hence could easily not show up in DMVs, extended events, SQL Profiler traces, or even custom logging. Checking ALL code will get you as close as possible.
The only area left out of an exhaustive code search is ad hoc queries that are not in code anywhere. In this case DMVs, traces, etc at least have the potential to identify usage, but are certainly no guarantee.
add a comment |
Unfortunately, there is no 100% accurate way to determine this. However, you can get as close as possible by examining all code that could potentially make DB calls, as well as the definitions of all of your DB objects and SQL Server Agent jobs. There are often code paths that are infrequently accessed and hence could easily not show up in DMVs, extended events, SQL Profiler traces, or even custom logging. Checking ALL code will get you as close as possible.
The only area left out of an exhaustive code search is ad hoc queries that are not in code anywhere. In this case DMVs, traces, etc at least have the potential to identify usage, but are certainly no guarantee.
Unfortunately, there is no 100% accurate way to determine this. However, you can get as close as possible by examining all code that could potentially make DB calls, as well as the definitions of all of your DB objects and SQL Server Agent jobs. There are often code paths that are infrequently accessed and hence could easily not show up in DMVs, extended events, SQL Profiler traces, or even custom logging. Checking ALL code will get you as close as possible.
The only area left out of an exhaustive code search is ad hoc queries that are not in code anywhere. In this case DMVs, traces, etc at least have the potential to identify usage, but are certainly no guarantee.
answered Aug 15 '16 at 17:03
Solomon RutzkySolomon Rutzky
48.6k581177
48.6k581177
add a comment |
add a comment |
Unless you have a server side trace running or XEvent running, there is no guaranteed way of telling a particular object is used or not. Some objects might be referenced by a report that is ran once a year by your CIO or business user. So unless you have a concrete data for your business life cycle, there is always a room for error.
I would suggest that once you identify possible candidates for removal, do not drop them, instead rename them and after a year or so, drop them.
When using DMVs you need to log them to a persisted table as DMV data get flushed out due to instance restart, certain sp_configure
changes, etc.
I have answered a similar question here that will get you started.
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
add a comment |
Unless you have a server side trace running or XEvent running, there is no guaranteed way of telling a particular object is used or not. Some objects might be referenced by a report that is ran once a year by your CIO or business user. So unless you have a concrete data for your business life cycle, there is always a room for error.
I would suggest that once you identify possible candidates for removal, do not drop them, instead rename them and after a year or so, drop them.
When using DMVs you need to log them to a persisted table as DMV data get flushed out due to instance restart, certain sp_configure
changes, etc.
I have answered a similar question here that will get you started.
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
add a comment |
Unless you have a server side trace running or XEvent running, there is no guaranteed way of telling a particular object is used or not. Some objects might be referenced by a report that is ran once a year by your CIO or business user. So unless you have a concrete data for your business life cycle, there is always a room for error.
I would suggest that once you identify possible candidates for removal, do not drop them, instead rename them and after a year or so, drop them.
When using DMVs you need to log them to a persisted table as DMV data get flushed out due to instance restart, certain sp_configure
changes, etc.
I have answered a similar question here that will get you started.
Unless you have a server side trace running or XEvent running, there is no guaranteed way of telling a particular object is used or not. Some objects might be referenced by a report that is ran once a year by your CIO or business user. So unless you have a concrete data for your business life cycle, there is always a room for error.
I would suggest that once you identify possible candidates for removal, do not drop them, instead rename them and after a year or so, drop them.
When using DMVs you need to log them to a persisted table as DMV data get flushed out due to instance restart, certain sp_configure
changes, etc.
I have answered a similar question here that will get you started.
edited Apr 13 '17 at 12:42
Community♦
1
1
answered Aug 15 '16 at 16:48
KinKin
53.8k481190
53.8k481190
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
add a comment |
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
Thanks. I'm renaming objects before deleting. But I don't wanna do it only once, I'm creating some monitoring procedures to warn us when some stuff happens. I need the date of last use to sort it.
– Hikari
Aug 17 '16 at 13:05
add a comment |
Your best bet without built in logging would be using the below query:
select s.last_execution_time
from sys.dm_exec_query_stats s
cross apply sys.dm_exec_query_plan (s.plan_handle) p
where object_name(p.objectid, db_id('DBNAME')) = @proc_nm
Thanks, butobject_name(p.objectid, db_id('DBNAME'))
is null for all records I got.
– Hikari
Aug 17 '16 at 13:10
add a comment |
Your best bet without built in logging would be using the below query:
select s.last_execution_time
from sys.dm_exec_query_stats s
cross apply sys.dm_exec_query_plan (s.plan_handle) p
where object_name(p.objectid, db_id('DBNAME')) = @proc_nm
Thanks, butobject_name(p.objectid, db_id('DBNAME'))
is null for all records I got.
– Hikari
Aug 17 '16 at 13:10
add a comment |
Your best bet without built in logging would be using the below query:
select s.last_execution_time
from sys.dm_exec_query_stats s
cross apply sys.dm_exec_query_plan (s.plan_handle) p
where object_name(p.objectid, db_id('DBNAME')) = @proc_nm
Your best bet without built in logging would be using the below query:
select s.last_execution_time
from sys.dm_exec_query_stats s
cross apply sys.dm_exec_query_plan (s.plan_handle) p
where object_name(p.objectid, db_id('DBNAME')) = @proc_nm
answered Aug 15 '16 at 16:28
Zi0n1Zi0n1
1114
1114
Thanks, butobject_name(p.objectid, db_id('DBNAME'))
is null for all records I got.
– Hikari
Aug 17 '16 at 13:10
add a comment |
Thanks, butobject_name(p.objectid, db_id('DBNAME'))
is null for all records I got.
– Hikari
Aug 17 '16 at 13:10
Thanks, but
object_name(p.objectid, db_id('DBNAME'))
is null for all records I got.– Hikari
Aug 17 '16 at 13:10
Thanks, but
object_name(p.objectid, db_id('DBNAME'))
is null for all records I got.– Hikari
Aug 17 '16 at 13:10
add a comment |
There is a dedicated DMV for stored procedures.
select DB_NAME(eps.database_ID) AS DatabaseName
, OBJECT_NAME(eps.object_ID) AS ObjectName
, eps.type_desc
, eps.last_execution_time
, eps.execution_count
from sys.dm_exec_procedure_stats eps
WHERE database_id = DB_ID()
The DMV will show the execution stats since the last server restart or detach (might need to confirm that). Meaning if a stored procedure has not been run since the database was attached or restarted, there will be no entry in this dmv. You can join it against the object table to find NULL's and assume what has not been run.
If you need trigger activity, you can use the same query as above except use sys.dm_exec_trigger_stats
as the table.
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
add a comment |
There is a dedicated DMV for stored procedures.
select DB_NAME(eps.database_ID) AS DatabaseName
, OBJECT_NAME(eps.object_ID) AS ObjectName
, eps.type_desc
, eps.last_execution_time
, eps.execution_count
from sys.dm_exec_procedure_stats eps
WHERE database_id = DB_ID()
The DMV will show the execution stats since the last server restart or detach (might need to confirm that). Meaning if a stored procedure has not been run since the database was attached or restarted, there will be no entry in this dmv. You can join it against the object table to find NULL's and assume what has not been run.
If you need trigger activity, you can use the same query as above except use sys.dm_exec_trigger_stats
as the table.
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
add a comment |
There is a dedicated DMV for stored procedures.
select DB_NAME(eps.database_ID) AS DatabaseName
, OBJECT_NAME(eps.object_ID) AS ObjectName
, eps.type_desc
, eps.last_execution_time
, eps.execution_count
from sys.dm_exec_procedure_stats eps
WHERE database_id = DB_ID()
The DMV will show the execution stats since the last server restart or detach (might need to confirm that). Meaning if a stored procedure has not been run since the database was attached or restarted, there will be no entry in this dmv. You can join it against the object table to find NULL's and assume what has not been run.
If you need trigger activity, you can use the same query as above except use sys.dm_exec_trigger_stats
as the table.
There is a dedicated DMV for stored procedures.
select DB_NAME(eps.database_ID) AS DatabaseName
, OBJECT_NAME(eps.object_ID) AS ObjectName
, eps.type_desc
, eps.last_execution_time
, eps.execution_count
from sys.dm_exec_procedure_stats eps
WHERE database_id = DB_ID()
The DMV will show the execution stats since the last server restart or detach (might need to confirm that). Meaning if a stored procedure has not been run since the database was attached or restarted, there will be no entry in this dmv. You can join it against the object table to find NULL's and assume what has not been run.
If you need trigger activity, you can use the same query as above except use sys.dm_exec_trigger_stats
as the table.
edited Mar 28 '17 at 18:18
marc_s
7,09053849
7,09053849
answered Aug 15 '16 at 16:35
dfundakodfundako
351111
351111
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
add a comment |
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
Thanks, unfortunately this query didn't show any user sps :/
– Hikari
Nov 25 '16 at 19:20
add a comment |
For DMV to give correct results for all databases, we can modify the script shared by @dfundako as below:
SELECT
DB_NAME(eps.database_ID) AS DatabaseName
,OBJECT_NAME(eps.object_ID, eps.database_id) AS ObjectName
,eps.type_desc
,eps.last_execution_time
,eps.execution_count
FROM sys.dm_exec_procedure_stats eps
New contributor
add a comment |
For DMV to give correct results for all databases, we can modify the script shared by @dfundako as below:
SELECT
DB_NAME(eps.database_ID) AS DatabaseName
,OBJECT_NAME(eps.object_ID, eps.database_id) AS ObjectName
,eps.type_desc
,eps.last_execution_time
,eps.execution_count
FROM sys.dm_exec_procedure_stats eps
New contributor
add a comment |
For DMV to give correct results for all databases, we can modify the script shared by @dfundako as below:
SELECT
DB_NAME(eps.database_ID) AS DatabaseName
,OBJECT_NAME(eps.object_ID, eps.database_id) AS ObjectName
,eps.type_desc
,eps.last_execution_time
,eps.execution_count
FROM sys.dm_exec_procedure_stats eps
New contributor
For DMV to give correct results for all databases, we can modify the script shared by @dfundako as below:
SELECT
DB_NAME(eps.database_ID) AS DatabaseName
,OBJECT_NAME(eps.object_ID, eps.database_id) AS ObjectName
,eps.type_desc
,eps.last_execution_time
,eps.execution_count
FROM sys.dm_exec_procedure_stats eps
New contributor
New contributor
answered 2 mins ago
Kasim HusainiKasim Husaini
1012
1012
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f146847%2fquery-last-time-a-view-funcion-or-sp-was-last-used-read%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown