我們應該使用軟標記進行軟刪除還是使用單獨的連接器表?哪個更有效率?數據庫是SQL Server。軟刪除 - 使用IsDeleted標誌或單獨的連接器表?
背景信息
前陣子我們有一個DB顧問進來看看我們的數據庫模式。當我們軟刪除一條記錄時,我們會在適當的表上更新一個IsDeleted標誌。有人建議,不要使用標誌,而應將已刪除的記錄存儲在單獨的表中,並使用更好的連接。我已經提出了這個建議,但至少在表面上看來,額外的表和連接看起來比使用標誌更昂貴。
初步測試
我已經建立了這個測試。
兩個表,Example和DeletedExample。我在IsDeleted列上添加了一個非聚集索引。
我做了三次測試,加載一百萬條記錄與下面的刪除/非刪除比率:
- 刪除/ NonDeleted
- 50/50
- 10/90
- 1/99
結果 - 50/50
個結果 - 10/90
結果 - 1/99
數據庫腳本,作爲參考,實施例,DeletedExample,和索引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
很好的答案。還要考慮使用過濾的索引。如果只在記錄未被刪除時按Column1查詢Example表,則索引Column1「WHERE IsDelete = 0」。 – 2012-01-16 16:27:33