简体   繁体   中英

Best way to execute table compaction algorithm on SQL Server 2012 Express

Let me start by saying that I have limited SQL experience but I have searched for a few days to find a good solution to my problem and I haven't found it yet. If an obvious answer exists I haven't used the right key words to find it.

I have a hardware data collection system that inserts a system health row into a table, 1 every 10 minutes over long periods of time. The data are used as diagnostic information in case of a hardware failure. The table contains records for multiple devices which are identified with a unique DeviceID column. I plan to keep at most 100K records in this table for each device. I plan to occasionally compact the table using the following algorithm:

When the number of rows for a specific DeviceID exceeds 100K, select the oldest 50% of the rows for the specific DeviceID and delete every 2nd of these rows, starting with the 2nd row in the selection.

This algorithm has the desired feature that the most recent data is retained at 10 minute intervals and older data progressively spreads out in time. Also the first (oldest) record is never deleted.

I can easily implement this in C# with multiple single row deletes after a query to select the oldest 50K rows. I hope someone knows a much better way to do this.

If it helps I can reorganize the data so that each device has a different table but that complicates presenting the data for review in a single virtualizing grid view in C#.

Thanks for your Help.

I think I found a solution to my problem here ( Deleting every nth row SQL ) using a common table expression. In the following code HealthID is the index in my DeviceHealth table. The DeviceID = 1 clause is just for testing. The real code will use a parameter instead of 1.

WITH cte AS (
    SELECT TOP 50 PERCENT t.*, ROW_NUMBER() OVER (ORDER BY t.HealthID)
        AS rank FROM [DeviceHealth] t WHERE DeviceID = 1)
DELETE cte WHERE rank%2 != 0

This seems to work. Is there a better way to do it or have a found a good solution?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM