我正在尝试更快地在 MS SQL Server 2008 R2 中进行一些查询,有两种方法:
1)通过创建临时表:
IF OBJECT_ID('tempdb..#Rec') IS NOT NULL
BEGIN
DROP TABLE #Rec
END
CREATE TABLE #Rec
(
ID int NULL,
DateBeg datetime NULL,
DateEnd datetime NULL,
Artist varchar(200) NULL,
DescriptionFull text NULL,
ActionPlaceID int NULL,
ActionTypeID smallint NULL,
Visible tinyint NULL,
Created datetime NULL,
DateList varchar(4000) NULL,
DatesAsPeriod tinyint NULL,
ShowReservLegend tinyint NULL,
ProviderID int NULL
)
INSERT INTO #Rec
SELECT ID,
DateBeg,
DateEnd,
Artist,
DescriptionFull,
ActionPlaceID,
ActionTypeID,
Visible,
Created,
DateList,
DatesAsPeriod,
ShowReservLegend,
ProviderID
FROM [ConcertDev].[dbo].[T_Action]
WHERE DateBeg BETWEEN '2012-01-01' AND '2012-06-01'
INSERT INTO #Rec
SELECT ID,
DateBeg,
DateEnd,
Artist,
DescriptionFull,
ActionPlaceID,
ActionTypeID,
Visible,
Created,
DateList,
DatesAsPeriod,
ShowReservLegend,
ProviderID
FROM [ConcertDev].[dbo].[T_Action]
WHERE DateBeg BETWEEN '2013-01-01' AND '2013-06-01'
SELECT * FROM #Rec
2) 通过使用 UNION ALL:
SELECT ID,
DateBeg,
DateEnd,
Artist,
DescriptionFull,
ActionPlaceID,
ActionTypeID,
Visible,
Created,
DateList,
DatesAsPeriod,
ShowReservLegend,
ProviderID
FROM [ConcertDev].[dbo].[T_Action]
WHERE DateBeg BETWEEN '2012-01-01' AND '2012-06-01'
UNION ALL
SELECT ID,
DateBeg,
DateEnd,
Artist,
DescriptionFull,
ActionPlaceID,
ActionTypeID,
Visible,
Created,
DateList,
DatesAsPeriod,
ShowReservLegend,
ProviderID
FROM [ConcertDev].[dbo].[T_Action]
WHERE DateBeg BETWEEN '2013-01-01' AND '2013-06-01'
我预计 UNION ALL 会快得多,但事实并非如此。它的速度只有 1 秒。
记录数为6147。使用临时表方法,执行时间为18秒。通过使用 UNION ALL 方法,执行时间为 17 秒。
那么,这是加快查询速度的正确方法吗?