我正在尝试优化此查询。对于如何在估计的执行计划中降低估计的子树成本,我有点难过。结果通常平均返回 50k 行,并且花费的时间比我想要的要长。任何时间刮胡子都会很有用。谢谢。
SELECT
tbl1.DID AS "CID",
tbl1.ID AS "ID",
tbl2.Column2 AS "Col2",
tbl2.Column3 AS "Col3",
tbl3.Column4 AS "Col4",
tbl3.Column5 AS "Col5",
tbl4.Column6 AS "Col6"
FROM TABLE1 tbl1 WITH (NOLOCK)
INNER JOIN TABLE2 tbl2 WITH (NOLOCK) ON tbl1.ID = tbl2.CID
INNER JOIN TABLE3 tbl3 WITH (NOLOCK) ON tbl1.ID = tbl3.CID
INNER JOIN TABLE4 tbl4 WITH (NOLOCK) ON tbl1.DID = tbl4.CID
更新 - 主键信息。每个表的内部连接中的所有列都已经有索引。如果是主键,那么它看起来像下面的那个,否则它是一个非集群的,就像底部的那个。:主键索引看起来像这样。
ALTER TABLE [dbo].[IDX_TABLE1] ADD CONSTRAINT [PK_TABLE1] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
非聚集的其他列索引如下所示:
CREATE NONCLUSTERED INDEX [IDX_TABLE1] ON [dbo].[TABLE1]
(
[DID] 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]
表定义:
CREATE TABLE [dbo].[TABLE1](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DID] [int] NULL
CONSTRAINT [PK_TABLE1] 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 TABLE [dbo].[TABLE2](
[CID] [int] NOT NULL,
[Column3] [varchar](50) NULL,
CONSTRAINT [PK_TABLE2] PRIMARY KEY CLUSTERED
(
[CID] 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 TABLE [dbo].[TABLE3](
[CID] [int] NOT NULL,
[Column4] [varchar](50) NULL
[Column5] [varchar](50) NULL
CONSTRAINT [PK_TABLE3] PRIMARY KEY CLUSTERED
(
[CID] 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 TABLE [dbo].[TABLE4](
[CID] [int] NOT NULL,
[Column6] [char](2) NULL
CONSTRAINT [PK_TABLE4] PRIMARY KEY CLUSTERED
(
[CID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]