query - show tables on some condition.

  • Hello world.

    I need to show all tables from my database that meet some conditions. For example tables with entries only after a particular date.

    I want to see what user tables have been used recently.

    Such query gives me the names of tables and the number of entries but how can I add condition on the entires themselves.

    select tab.name, i.rows

    from dbo.sysindexes i WITH (NOLOCK)

    join dbo.sysobjects tab WITH (NOLOCK) on i.id = tab.id

    where i.indid in (0, 1)

    and tab.xtype = 'U'

    --and i.rows <>0

    order by tab.name

    Please help ladies and gentelmen 😉

  • mark.bienkowski (7/16/2013)


    Hello world.

    I need to show all tables from my database that meet some conditions. For example tables with entries only after a particular date.

    I want to see what user tables have been used recently.

    Such query gives me the names of tables and the number of entries but how can I add condition on the entires themselves.

    select tab.name, i.rows

    from dbo.sysindexes i WITH (NOLOCK)

    join dbo.sysobjects tab WITH (NOLOCK) on i.id = tab.id

    where i.indid in (0, 1)

    and tab.xtype = 'U'

    --and i.rows <>0

    order by tab.name

    Please help ladies and gentelmen 😉

    At best you can find out what indexes have been used. This is not the same as what tables. So often this type of request is followed up by an explanation that the intention is to delete "unused" tables. Tread lightly here. There are often processes that run only once a year and might fall on your "to be deleted" list.

    I am guessing that you want to find some sort of date information with this? Take a look here.

    select * from sys.dm_db_index_usage_stats

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (7/16/2013)


    At best you can find out what indexes have been used. This is not the same as what tables. So often this type of request is followed up by an explanation that the intention is to delete "unused" tables. Tread lightly here. There are often processes that run only once a year and might fall on your "to be deleted" list.

    I am guessing that you want to find some sort of date information with this? Take a look here.

    select * from sys.dm_db_index_usage_stats

    Hi, thanks for quick reply.

    I am about to upgrade my crm system and it requires upgrading the database too.

    The task is to select the tables that our users have used recently (during last 2 years for instance) and concentrate on them at first.

    The database consists of over 300 tables.

    Tell me please what i should understand by columns: last user seek, last user scan, last user lookup, last user update and system respectively in sys.dm_db_index_usage_stats table

    Which one is the most important in my case?

    Can I just order by last lookup etc. to obtain the desired effect ie. if the table has been used or not for the specified time?

    Thank you very much in advance.

  • mark.bienkowski (7/17/2013)


    Sean Lange (7/16/2013)


    At best you can find out what indexes have been used. This is not the same as what tables. So often this type of request is followed up by an explanation that the intention is to delete "unused" tables. Tread lightly here. There are often processes that run only once a year and might fall on your "to be deleted" list.

    I am guessing that you want to find some sort of date information with this? Take a look here.

    select * from sys.dm_db_index_usage_stats

    Hi, thanks for quick reply.

    I am about to upgrade my crm system and it requires upgrading the database too.

    The task is to select the tables that our users have used recently (during last 2 years for instance) and concentrate on them at first.

    The database consists of over 300 tables.

    Tell me please what i should understand by columns: last user seek, last user scan, last user lookup, last user update and system respectively in sys.dm_db_index_usage_stats table

    Which one is the most important in my case?

    Can I just order by last lookup etc. to obtain the desired effect ie. if the table has been used or not for the specified time?

    Thank you very much in advance.

    First of all you should look at bol for a complete definition. http://msdn.microsoft.com/en-us/library/ms188755.aspx

    However, you absolutely cannot use that if you are trying to find TABLES. That is ONLY indexes. It also only shows information since the last time the service was started.

    You really don't have anyway to determine what tables have been used in the last 2 years unless you have some sort of auditing on those tables. If your task is to upgrade the database why are you worried about the tables? If you are using this only to determine the order that you want to do your analysis that is one thing but DO NOT use this as a way to determine if you should drop the table or not. If anything I would just rename the tables instead of dropping them. That way if something comes up you can simply rename the table back and all is well. Then after a year or two you can drop all the tables that never needed recovery.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply