Should we use a flag for soft deletions or a separate carpentry table? Which is more efficient? The database is SQL Server.
Background Information
While we have a database consultant, and take a look at our database schema. When we gently delete a record, we update the IsDeleted flag in the corresponding tables. It has been suggested that instead of using the flag, save the deleted entries in a separate table and use the join, as that would be better. I put this proposal to the test, but at least on the surface, an extra table and join look more expensive than using a flag.
Initial testing
I installed this test.
Two tables, an example and a remote example. I added a non-clustered index to the IsDeleted column.
I performed three tests, downloading a million records with the following deleted / not deleted relationships:
- Deleted / Uninstalled
- 50:50
- 10/90
- 1/99
Results - 50/50 
Results - 10/90 
Results - 1/99 
Database scripts, for reference, example, deletion, and example for example .IsDeleted
CREATE TABLE [dbo].[Example]( [ID] [int] NOT NULL, [Column1] [nvarchar](50) NULL, [IsDeleted] [bit] NOT NULL, CONSTRAINT [PK_Example] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Example] ADD CONSTRAINT [DF_Example_IsDeleted] DEFAULT ((0)) FOR [IsDeleted] GO CREATE TABLE [dbo].[DeletedExample]( [ID] [int] NOT NULL, CONSTRAINT [PK_DeletedExample] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[DeletedExample] WITH CHECK ADD CONSTRAINT [FK_DeletedExample_Example] FOREIGN KEY([ID]) REFERENCES [dbo].[Example] ([ID]) GO ALTER TABLE [dbo].[DeletedExample] CHECK CONSTRAINT [FK_DeletedExample_Example] GO CREATE NONCLUSTERED INDEX [IX_IsDeleted] ON [dbo].[Example] ( [IsDeleted] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO
indexing sql-server-2008-r2 soft-delete
Jon raynor
source share