In SQL Server, both TRUNCATE and DELETE are used to remove data from a table, but they differ significantly in terms of performance, logging, and their impact on the table structure. Below, we explore their differences through advanced real-world scenarios.

Deleting Specific Rows

You have a large customer database and need to delete records based on a condition, such as all customers from a specific country.

DELETE FROM Customers
WHERE Country = 'USA';

Why Use DELETE?

Note. Since DELETE logs each row individually, it can be slower for large datasets, especially when dealing with a significant number of rows.

Resetting a Table for Data Migration

During data migration, you need to clear all rows from a table, such as the Users or Orders table, before inserting new data.

TRUNCATE TABLE Users;

Why Use TRUNCATE?

Note. TRUNCATE cannot be used if there are foreign key constraints, even if those constraints are defined with ON DELETE CASCADE.

Managing Temporary Tables for Large Datasets

While working with large datasets, you need to clear the contents of a temporary table after processing it, such as Temp_SessionData.

TRUNCATE TABLE Temp_SessionData;

Why Use TRUNCATE?

Note. Using TRUNCATE avoids performance bottlenecks associated with row-by-row deletions.

Deleting Data with Referential Integrity

You need to delete all records in a parent table (e.g., Customers) while ensuring that related records in child tables (e.g., Orders) are also removed.

DELETE FROM Customers WHERE Country = 'USA';

Why Use DELETE?

Note. While DELETE is slower than TRUNCATE, it ensures referential integrity and cascading actions across related tables.

Regular Data Resets for Large Tables

You regularly refresh data in a table (e.g., SalesData) from an external system and need to reset it completely.

TRUNCATE TABLE SalesData;

Why Use TRUNCATE?

Note. Check that no foreign key dependencies exist, as these will block the use of TRUNCATE.

Partial Table Cleanup with Complex Conditions

You need to clean up a specific subset of data from a large table where conditions involve multiple columns (e.g., inactive users who haven’t logged in for a year).

DELETE FROM Users
WHERE 
    LastLogin < DATEADD(YEAR, -1, GETDATE()) 
    AND IsActive = 0;

Why Use DELETE?

Note. For large datasets, indexing the columns used in the WHERE clause can improve performance.

Archiving Old Data

You need to archive old transactional data from a table (e.g., Orders) into an archive table before removing it from the main table.

INSERT INTO ArchivedOrders
SELECT *
FROM Orders
WHERE OrderDate < '2023-01-01';

DELETE FROM Orders
WHERE OrderDate < '2023-01-01';

Why Use DELETE?

Note. Using DELETE in combination with INSERT INTO helps retain historical data while managing table size.

Clearing Audit Logs Periodically

Use Case

Your application generates a large number of audit logs, and you periodically clear logs older than a specific timeframe to maintain performance.

TRUNCATE TABLE AuditLogs;

Why Use TRUNCATE?

Note. Check retention policies are implemented before truncating, as all data will be permanently removed.

Performance Considerations

Summary of When to Use Each

Now Let’s walk through a stock exchange scenario where you can apply both DELETE and TRUNCATE commands in SQL Server, depending on the requirements. This covers a realistic stock trading system scenario, including market operations, account management, and transaction logs.

Stock exchange scenario

In a stock exchange system, you might have multiple tables like,

Multiple tables

We will look at how DELETE and TRUNCATE can be used for various operations in this stock exchange system, depending on whether we need to delete specific records, reset tables, or manage large datasets efficiently.

Deleting a Specific Stock Order from the Orders Table

Let’s say a trader cancels a buy/sell order. You need to delete the specific order record from the Orders table.

SQL Query

DELETE FROM Orders
WHERE OrderID = 12345;

Why use DELETE?

Impact

This operation is slow compared to TRUNCATE, especially if there are a large number of active orders in the system, but it is necessary for deleting specific rows based on user actions.

Resetting All Orders for the End of the Day

At the end of each trading day, the stock exchange needs to clear all orders from the Orders table to prepare for the next trading day. The system clears all records, regardless of whether the orders are pending or executed.

SQL Query

TRUNCATE TABLE Orders;

Why use TRUNCATE?

Consideration

Ensure that there are no foreign key constraints in place, or if there are, ensure TRUNCATE is allowed (i.e., no dependencies with cascading deletes).

Impact

Clearing Historical Data for Trades

The exchange wants to archive the trades older than a year, as they are no longer relevant for active trading or reporting but need to be stored for historical purposes.

Trades that happened more than a year ago need to be archived into a backup system, and the records should be removed from the main Trades table.

SQL Query

DELETE FROM Trades
WHERE TradeDate < DATEADD(YEAR, -1, GETDATE());

Why use DELETE?

Consideration

If you have millions of trades, DELETE might be slow, but this is necessary if you want to keep the data that is still relevant (for example, trades made within the last year).

Impact

Deleting specific records ensures that important data (such as current trades) is not deleted by mistake, and you can archive old data efficiently.

Resetting All Stock Prices for a New Trading Day

On the stock exchange, stock prices need to be reset every morning to the opening prices of the day. You want to clear all the previous day’s data and set the new day's prices.

Scenario

SQL Query

TRUNCATE TABLE StockPrices;

Why use TRUNCATE?

Consideration

If there are foreign key constraints or dependencies on the StockPrices table (e.g., historical trades), TRUNCATE may not be possible. In such cases, you would need to first delete or archive related data.

Impact

Faster performance compared to DELETE and useful for daily resets. This is a classic use of TRUNCATE when the data doesn't need to be retained across days.

Deleting Specific Trade Records Based on a Condition

Let’s say an anomaly or error occurred in the trading system where certain trades were mistakenly recorded (perhaps due to a bug or a trading error), and you need to delete them.

Scenario

SQL Query

DELETE FROM Trades
WHERE StockSymbol = 'INVALID' OR TradeAmount > 1000000;

Why use DELETE?

Consideration

This could be a slow operation if the trades table is very large and the condition affects a significant number of rows.

Impact

It’s essential to identify and delete only the erroneous rows based on conditions, so DELETE allows for precise control.

In stock exchange systems, TRUNCATE is usually reserved for bulk operations where all data can be removed quickly (such as resetting stock prices), while DELETE is used for more granular, specific removals or when data integrity constraints are involved (such as removing erroneous trades or orders).