LLBL에서 많은 수의 느린 프리 페치 쿼리를 처리하고 있습니다. 여기서 생성 된 SQL의 단순화 된 버전입니다 :하위 쿼리 IN 절이 느림?
SELECT DISTINCT
Column1
FROM
Table1
WHERE
Table1.Table2ID IN
(
SELECT Table2.Table2ID AS Table2ID
FROM
Table2
INNER JOIN Table1 ON Table2.Table2ID=Table1.Table2ID
INNER JOIN
(
SELECT DISTINCT
Table1.Table2ID AS Table2ID,
MAX(Table1.EffectiveDate) AS EffectiveDate
FROM Table1
WHERE Table1.EffectiveDate <= '2012-01-03 00:00:00:000'
GROUP BY Table1.Table2ID
) MaxEffective
ON
MaxEffective.Table2ID = Table1.Table2ID
AND MaxEffective.EffectiveDate = Table1.EffectiveDate
)
내가 찾는거야 것은 서브 쿼리가 빠르게 실행하는 내가 실제 결과와 그 하위 쿼리를 대체 할 경우, 외부 쿼리가 빠릅니다. 그러나 함께, 그들은 느립니다.
조금 도움이 된 데이터베이스 엔진 튜닝 어드바이저를 실행했지만 여전히 느립니다.
저는 실행 계획을 잘 이해하지 못하고 있지만 Table1에서 인덱스 탐색을하는 데는 대부분 시간이 소요됩니다.
상관이없는 하위 쿼리이므로 더 빨리 실행할 것으로 예상했습니다. 내가 보지 못하는게 있니?
바로 SQL이라면 쿼리를 다시 작성하고 조인 할 것이지만 LLBL로 인해 막을 수 있습니다. 조인을 강제로 수행하는 데 사용할 수있는 설정이 있습니까? SQL Server가 조인과 동일한 실행 계획을 생성하지 않는 이유가 있습니까? 생성 문에 대한
실제 쿼리 편집 ...
SELECT DISTINCT
ResidentialComponentValues.ResidentialComponentValueID AS ResidentialComponentValueId,
ResidentialComponentValues.ResidentialComponentTypeID AS ResidentialComponentTypeId,
ResidentialComponentValues.Value,
ResidentialComponentValues.Story,
ResidentialComponentValues.LastUpdated,
ResidentialComponentValues.LastUpdatedBy,
ResidentialComponentValues.ConcurrencyTimestamp,
ResidentialComponentValues.EffectiveDate,
ResidentialComponentValues.DefaultQuantity
FROM
ResidentialComponentValues
WHERE
ResidentialComponentValues.ResidentialComponentTypeID IN
(
SELECT ResidentialComponentTypes.ResidentialComponentTypeID AS ResidentialComponentTypeId
FROM
ResidentialComponentTypes INNER JOIN ResidentialComponentValues
ON ResidentialComponentTypes.ResidentialComponentTypeID=ResidentialComponentValues.ResidentialComponentTypeID
INNER JOIN
(
SELECT DISTINCT
ResidentialComponentValues.ResidentialComponentTypeID AS ResidentialComponentTypeId,
MAX(ResidentialComponentValues.EffectiveDate) AS EffectiveDate
FROM ResidentialComponentValues
WHERE ResidentialComponentValues.EffectiveDate <= '2012-01-03 00:00:00:000'
GROUP BY ResidentialComponentValues.ResidentialComponentTypeID
) LPA_E1
ON
LPA_E1.ResidentialComponentTypeId = ResidentialComponentValues.ResidentialComponentTypeID
AND LPA_E1.EffectiveDate = ResidentialComponentValues.EffectiveDate
)
편집 :
/****** Object: Table [dbo].[ResidentialComponentTypes] Script Date: 01/03/2012 13:49:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[ResidentialComponentTypes](
[ResidentialComponentTypeID] [int] IDENTITY(1,1) NOT NULL,
[ComponentTypeName] [varchar](255) NOT NULL,
[LastUpdated] [datetime] NOT NULL,
[LastUpdatedBy] [varchar](50) NOT NULL,
[ConcurrencyTimestamp] [timestamp] NOT NULL,
[Active] [bit] NOT NULL,
CONSTRAINT [PK_ResidentialComponentTypes] PRIMARY KEY CLUSTERED
(
[ResidentialComponentTypeID] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[ResidentialComponentValues] Script Date: 01/03/2012 13:49:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[ResidentialComponentValues](
[ResidentialComponentValueID] [int] IDENTITY(1,1) NOT NULL,
[ResidentialComponentTypeID] [int] NOT NULL,
[Value] [decimal](18, 3) NOT NULL,
[Story] [varchar](255) NOT NULL,
[LastUpdated] [datetime] NOT NULL,
[LastUpdatedBy] [varchar](50) NOT NULL,
[ConcurrencyTimestamp] [timestamp] NOT NULL,
[EffectiveDate] [datetime] NOT NULL,
[DefaultQuantity] [int] NOT NULL,
CONSTRAINT [PK_ResidentialComponentPrices] PRIMARY KEY CLUSTERED
(
[ResidentialComponentValueID] 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
SET ANSI_PADDING OFF
GO
CREATE NONCLUSTERED INDEX [_dta_index_ResidentialComponentValues_71_56543435__K1] ON [dbo].[ResidentialComponentValues]
(
[ResidentialComponentValueID] 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
CREATE NONCLUSTERED INDEX [_dta_index_ResidentialComponentValues_71_56543435__K1_2_3_4_5_6_7_8_9] ON [dbo].[ResidentialComponentValues]
(
[ResidentialComponentValueID] ASC
)
INCLUDE ([ResidentialComponentTypeID],
[Value],
[Story],
[LastUpdated],
[LastUpdatedBy],
[ConcurrencyTimestamp],
[EffectiveDate],
[DefaultQuantity]) 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
CREATE NONCLUSTERED INDEX [_dta_index_ResidentialComponentValues_71_56543435__K2_K1] ON [dbo].[ResidentialComponentValues]
(
[ResidentialComponentTypeID] ASC,
[ResidentialComponentValueID] 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
CREATE NONCLUSTERED INDEX [_dta_index_ResidentialComponentValues_71_56543435__K2_K8_K1] ON [dbo].[ResidentialComponentValues]
(
[ResidentialComponentTypeID] ASC,
[EffectiveDate] ASC,
[ResidentialComponentValueID] 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
CREATE NONCLUSTERED INDEX [_dta_index_ResidentialComponentValues_71_56543435__K2_K8_K1_3_4_5_6_7_9] ON [dbo].[ResidentialComponentValues]
(
[ResidentialComponentTypeID] ASC,
[EffectiveDate] ASC,
[ResidentialComponentValueID] ASC
)
INCLUDE ([Value],
[Story],
[LastUpdated],
[LastUpdatedBy],
[ConcurrencyTimestamp],
[DefaultQuantity]) 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
/****** Object: ForeignKey [FK_ResidentialComponentValues_ResidentialComponentTypes] Script Date: 01/03/2012 13:49:06 ******/
ALTER TABLE [dbo].[ResidentialComponentValues] WITH CHECK ADD CONSTRAINT [FK_ResidentialComponentValues_ResidentialComponentTypes] FOREIGN KEY([ResidentialComponentTypeID])
REFERENCES [dbo].[ResidentialComponentTypes] ([ResidentialComponentTypeID])
GO
ALTER TABLE [dbo].[ResidentialComponentValues] CHECK CONSTRAINT [FK_ResidentialComponentValues_ResidentialComponentTypes]
GO
내가 당신이 말한 알고 기록 2와 4를 반환 이것은 단순화되었지만 내부 쿼리와 외부 쿼리 사이에 중복 별칭이있을 수 있습니다. 그렇게하면 엔진이 훨씬 느린 상관 관계가있는 하위 쿼리로이를 수행하려고 할 수 있습니다. – JNK
별칭이 없지만 테이블 이름을 직접 참조하는 것이 동일한 작업을 수행하는 것일 수 있습니다. 하위 쿼리의 Table1에 별칭을 추가해보고 도움이되는지 확인해 보겠습니다. – Dan
@JNK - 이것은 * scope *의 모든 규칙을 어기는 것이지, 이것이 정말로 문제라고 상상할 수는 없습니다. – MatBailie