我们有下表:
CREATE TABLE [dbo].[CampaignCustomer](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CampaignID] [int] NOT NULL,
[CustomerID] [int] NULL,
[CouponCode] [nvarchar](20) NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[ModifiedDate] [datetime] NULL,
[Active] [bit] NOT NULL,
CONSTRAINT [PK_CampaignCustomer] 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]
和以下唯一索引:
CREATE UNIQUE NONCLUSTERED INDEX [IX_CampaignCustomer_CouponCode] ON [dbo].[CampaignCustomer]
(
[CouponCode] 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, FILLFACTOR = 20) ON [PRIMARY]
GO
我们使用 CouponCode 和其他外键(为简单起见,上面未显示)进行非常持续的查询。CampaignCustomer 表有近 400 万条记录并且还在不断增长。我们还开展不需要优惠券代码的活动,因此我们不会插入这些记录。现在我们还需要开始跟踪这些活动以用于其他目的。所以我们有两个选择:
- 我们将 CouponCode 列更改为允许空值,并创建一个唯一的过滤索引以不包含空值并允许表变得更大更快。
- 为此特定目的创建一个单独的表来跟踪所有活动。
请记住,CampaignCustomer 表经常用于兑换优惠券和插入新优惠券。底线是我们不希望我们的客户兑换优惠券并一直等到他们放弃或其他流程失败。那么,从效率的角度来看,您认为哪个选项最好,为什么?