SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query - show tables on some condition.


query - show tables on some condition.

Author
Message
mark.bienkowski
mark.bienkowski
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 9
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 Wink
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64788 Visits: 17979
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 Wink


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.

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)
mark.bienkowski
mark.bienkowski
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 9
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.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64788 Visits: 17979
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.

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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search