We are running quite a few tests for our software that require a certain set of databases. These databases are usually huge, while most of the data in there will not be used by any testcase.
In order to reduce stress on our database server due to many CI tests, we would like to remove the unused data and it would be great if that would be possible in an automated way.
Here I'm thinking of something like "Run the tests and log which objects (e.g. Table and PK) are accessed (SELECT, UPDATE, DELETE)". Then afterwards we could remove all which haven't been accessed. Is there a way to collect this information in SQL Server 2016?
Thanks, Alex
Best Answer
Updating answer
There are two parts to your question:
This is to do with data inside tables, meaning we have to get to the row level in order to capture what has been updated/deleted(DML). This can be achieved by enabling tracking. Copied from Microsoft site:
You may refer this link from Microsoft to understand details. Alternatively you can enable auditing on tables for any DML operations and you may also club these two as mentioned here.
For more details on Change tracking feature in SQL server database, you may refer another link from Mr. Arshad Ali.
Above will help you for tracking DML operation only however for tracking select, its really difficult and can be traced only by enabling auditing as mentioned here.
As data level is tracked, we can go a level higher i.e. object level. In order to handle the usage/non-usage of any object, you may refer below.
There is a wonderful article from Mr. Ahmad Yaseen on finding unused objects in any given databases. You may Below is the query which can be used:
Kindly note that, this will work from the last restart time of SQL service/server as mentioned:
You may read more details here.
There are other articles also, which could be referred for this purpose. For instance, you may use sp_blitzindex for finding similar info from Mr. Brent Ozar.
For checking access date/time for views/function/SPs, you may refer below question:
Query last time a view, funcion or sp was last used/read
There is another article from Mr. Bart Gawrych on the same topic:
This again depends on system DMV, so be wary of last reboot of service/server.
Note: Your question is on how to find list of objects which are getting used however there are tonnes of article on finding unused objects, you may tweak them a bit to get list of used objects or else if you do manage to find unused objects, that should also serve your purpose.
Please let us know if this helped you in achieving your objective.