x
login about faq Site discussion (meta-askssc)

managing views

We currently have many views. Some created long before my time. Is the a way to determine which views are being used actively and which once are obsolete?

more ▼

asked Mar 06 '12 at 07:48 AM in Default

Natascha gravatar image

Natascha
1 1

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Unfortunately, there is no certain way to find such information. There are some workarounds but they have so many consequences and dependencies. For e.g. Below script would give you the name of all of your VIEWS and the last execution time

SELECT  V.NAME
,       MAX(DEQS.last_execution_time)
FROM    sys.[views] AS V
        OUTER APPLY ( SELECT    MAX(last_execution_time) last_execution_time
                      FROM      sys.dm_exec_query_stats AS DEQS WITH (NOLOCK)
                                CROSS APPLY sys.dm_exec_sql_text(DEQS.sql_handle)
                                AS DEST
                      WHERE     DEST.text LIKE '%' + V.[name] + '%'
                    ) DEQS
GROUP BY v.[name]

But the caveat is the cache as the lifetime of the output field execution time is tied to the cached plans itself. When a plan is removed from the cache, the corresponding execution time would be eliminated from the output. So one has to be very very careful to use information of that kind to draw conclusion of whether a VIEW is in use.

The next options are run a trace for over a certain duration for e.g. over a month. Not to forget month ends, quarter ends, half year and so on.. according to your environment.

But at the end it all would be a laborious work and It cannot be sorted out in one go.

more ▼

answered Mar 06 '12 at 09:35 AM

Usman Butt gravatar image

Usman Butt
13.8k 6 8 14

(comments are locked)
10|1200 characters needed characters left

This is pretty tricky to track down, you can query the executing queries dmvs with something like

IF OBJECT_ID('tempdb..#queries') IS NOT NULL 
    DROP TABLE #queries
GO


SELECT  SUBSTRING(dest.text, ( deqs.statement_start_offset / 2 ) + 1,
            ( ( CASE deqs.statement_end_offset
                 WHEN -1 THEN DATALENGTH(dest.text)
                 ELSE deqs.statement_end_offset
                END - deqs.statement_start_offset ) / 2 ) + 1) AS statement_text
INTO    #queries
FROM    [sys].[dm_exec_query_stats] AS deqs
       CROSS APPLY [sys].[dm_exec_sql_text]([deqs].[sql_handle]) AS dest

SELECT  *
FROM    [#queries] AS q
WHERE   [q].[statement_text] LIKE '%view%'' -- use your view names here if poss

but this could be a bad query to run on a busy production server, it will take a lot of effort to complete.

You could use extended events to catch executing code too but that again has performance considerations.

You may be best simply reviewing the system with the system administrator and carefully renaming views and so on until you identify those that are in use.

more ▼

answered Mar 06 '12 at 08:49 AM

Fatherjack gravatar image

Fatherjack ♦♦
38.8k 55 69 104

(comments are locked)
10|1200 characters needed characters left

Thank you for responding. We have many reports and our LIMS application runs of these views.

When a view is called via a report will the execution time also be updated?

We have been considering going the trace route as you suggested but I was hoping that we would find a magical solution here. :-)

Thank you.

more ▼

answered Mar 07 '12 at 11:30 AM

Natascha gravatar image

Natascha
1 1

Adhoc queries would also be part of the system view used in the script. So the execution time should have been updated. As far as magical solution is concerned, I do not think there is one yet. :)

Mar 07 '12 at 11:55 AM Usman Butt

Besides, if I think of obsolete, then obsolete would be those VIEWs, which are no more valid. Like VIEWs referencing a non-existing table, field, user data-type etc. So have you tried that way? This way the real obsoletes could be sort out in a better manner. Sounds good?

Mar 07 '12 at 12:04 PM Usman Butt
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x26

asked: Mar 06 '12 at 07:48 AM

Seen: 277 times

Last Updated: Mar 07 '12 at 12:04 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.