Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

query - show tables on some condition. Expand / Collapse
Author
Message
Posted Tuesday, July 16, 2013 7:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 1, 2013 8:06 AM
Points: 2, Visits: 8
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 ;)
Post #1474143
Posted Tuesday, July 16, 2013 8:57 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1474170
Posted Wednesday, July 17, 2013 7:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 1, 2013 8:06 AM
Points: 2, Visits: 8
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.

Post #1474593
Posted Wednesday, July 17, 2013 7:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:05 PM
Points: 13,327, Visits: 12,820
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1474610
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse