Query last time a view, funcion or sp was last used/read












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 15 '16 at 15:02









      HikariHikari

      57031828




      57031828






















          5 Answers
          5






          active

          oldest

          votes


















          1














          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.






          share|improve this answer































            2














            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.






            share|improve this answer


























            • 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



















            1














            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





            share|improve this answer
























            • Thanks, but object_name(p.objectid, db_id('DBNAME')) is null for all records I got.

              – Hikari
              Aug 17 '16 at 13:10



















            1














            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.






            share|improve this answer


























            • Thanks, unfortunately this query didn't show any user sps :/

              – Hikari
              Nov 25 '16 at 19:20



















            0














            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




            share








            New contributor




            Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              1














              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.






              share|improve this answer




























                1














                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.






                share|improve this answer


























                  1












                  1








                  1







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 15 '16 at 17:03









                  Solomon RutzkySolomon Rutzky

                  48.6k581177




                  48.6k581177

























                      2














                      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.






                      share|improve this answer


























                      • 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
















                      2














                      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.






                      share|improve this answer


























                      • 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














                      2












                      2








                      2







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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



















                      • 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











                      1














                      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





                      share|improve this answer
























                      • Thanks, but object_name(p.objectid, db_id('DBNAME')) is null for all records I got.

                        – Hikari
                        Aug 17 '16 at 13:10
















                      1














                      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





                      share|improve this answer
























                      • Thanks, but object_name(p.objectid, db_id('DBNAME')) is null for all records I got.

                        – Hikari
                        Aug 17 '16 at 13:10














                      1












                      1








                      1







                      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





                      share|improve this answer













                      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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 15 '16 at 16:28









                      Zi0n1Zi0n1

                      1114




                      1114













                      • 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

















                      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











                      1














                      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.






                      share|improve this answer


























                      • Thanks, unfortunately this query didn't show any user sps :/

                        – Hikari
                        Nov 25 '16 at 19:20
















                      1














                      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.






                      share|improve this answer


























                      • Thanks, unfortunately this query didn't show any user sps :/

                        – Hikari
                        Nov 25 '16 at 19:20














                      1












                      1








                      1







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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



















                      • 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











                      0














                      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




                      share








                      New contributor




                      Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

























                        0














                        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




                        share








                        New contributor




                        Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.























                          0












                          0








                          0







                          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




                          share








                          New contributor




                          Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.










                          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





                          share








                          New contributor




                          Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.








                          share


                          share






                          New contributor




                          Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 2 mins ago









                          Kasim HusainiKasim Husaini

                          1012




                          1012




                          New contributor




                          Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Kasim Husaini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              SQL Server 17 - Attemping to backup to remote NAS but Access is denied

                              Always On Availability groups resolving state after failover - Remote harden of transaction...

                              Restoring from pg_dump with foreign key constraints