S
S
shell_guy2021-10-22 18:51:46
Database administration
shell_guy, 2021-10-22 18:51:46

How to delete data for a period and check for integrity?

The database has grown in 5 years to a huge size. What commands to do the cleaning of data, leaving the last year? What commands to check for integrity and optimize?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2021-10-22
@shell_guy

What commands should be used to trunk data and leave data for the last year?

If there are partitions, delete the partitions
https://docs.microsoft.com/ru-ru/sql/t-sql/stateme...
TRUNCATE TABLE PartitionTable1   
WITH (PARTITIONS (2, 4, 6 TO 8));  
GO

If there are no partitions and there is little data, it is easy to delete . If there is a lot of data, make a task in SQL Agent. The number of rows to delete is up to you.
DELETE FROM table WHERE date <= %date%
DELETE TOP (1000) FROM table WHERE date <= %date%
What commands to check for integrity and optimize?

Check the integrity and consistency - only manually. The necessity and possibility of deleting your data is determined only by you.
If you need to check if the database is broken, then use the DBCC CHECKDB command
https://docs.microsoft.com/ru-ru/sql/t-sql/databas...
PS Backups and other stuff would be nice

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question