使用带有标识和日期 ON分区的PRIMARY KEY CLUSTERED创建了两个表,因为任务会拆分并截断早于30天的分区。仅保留最近的记录。
我有一个特殊字段来关联表格,但即使使用索引,加入也会非常缓慢。你能建议如何优化吗?
接下来是表和Join语句:
CREATE TABLE [dbo].[Redeem](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Ticket] [nvarchar](64) NOT NULL,
[CorrelationTicket] [nvarchar](64) NOT NULL,
[CreatedUTC] [datetime] NOT NULL,
[CreatedDate] [date] NOT NULL,
[Redeem fields here...]
CONSTRAINT [PK_Redeem] PRIMARY KEY CLUSTERED
(
[CreatedDate] ASC,
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
ON myPS([CreatedDate]);
CREATE NONCLUSTERED INDEX [IX_Redeem_CorrelationTicket]
ON [dbo].[Redeem]([CreatedDate] ASC, [CorrelationTicket] ASC)
ON [myPS] ([CreatedDate]);
CREATE TABLE [dbo].[Validate](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Ticket] [nvarchar](64) NOT NULL,
[CorrelationTicket] [nvarchar](64) NOT NULL,
[CreatedUTC] [datetime] NOT NULL,
[CreatedDate] [date] NOT NULL,
[Validate fields here...]
CONSTRAINT [PK_Validate] PRIMARY KEY CLUSTERED
(
[CreatedDate] ASC,
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
ON myPS([CreatedDate]);
CREATE NONCLUSTERED INDEX [IX_Validate_CorrelationTicket]
ON [dbo].[Validate]([CreatedDate] ASC, [CorrelationTicket] ASC)
ON [myPS] ([CreatedDate]);
这是加入:
SELECT top 100
v.*,
r.*
from
Validate v
LEFT OUTER join Redeem r
on v.CorrelationTicket = r.CorrelationTicket
ORDER BY v.CreatedDate DESC
答案 0 :(得分:0)
谢谢Krintner!
问题是您建议的ORDER BY。它正在整理整个结果集(不确定原因)。 我确实需要排序,但将索引更改为DESC就可以了。
我也遵循建议并仅在INDEX中使用CorrelationTicket。
CONSTRAINT [PK_Redeem] PRIMARY KEY CLUSTERED
(
[CreatedDate] DESC,
[Id] DESC
)