您认为SQL语句 BELOW 能否以比呈现方式更好的方式执行。
以下是要求:
代码位于图片
下方
以下是一些潜在的情况:
QUERY:
DECLARE @UserName NVARCHAR(255)
DECLARE @TextIdentifier NVARCHAR(255)
DECLARE @ProximityDate AS DATETIME
SET @UserName = 'Pete'
SET @TextIdentifier = '456'
SELECT @ProximityDate = [DateCreated]
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND TextIdentifier = @TextIdentifier
SELECT * FROM (
SELECT TOP 10 *
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND [DateCreated] < @ProximityDate
UNION
SELECT *
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND TextIdentifier = @TextIdentifier
UNION
SELECT TOP 10 *
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND [DateCreated] > @ProximityDate
) x
ORDER BY x.DateCreated
表格
CREATE TABLE [dbo].[ProximitySelectTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[TextIdentifier] [nvarchar](255) NULL,
[UserName] [nvarchar](255) NULL,
[FileName] [nvarchar](255) NULL,
[DateCreated] [datetime] NULL
) ON [PRIMARY]
测试数据
/****** Object: Table [dbo].[ProximitySelectTable] Script Date: 06/15/2016 15:15:09 ******/
SET IDENTITY_INSERT [dbo].[ProximitySelectTable] ON
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (1, N'2has721', N'Pete', N'c:\123456.mp3', CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (2, N'123', N'Pete', N'c:\223456.mp3', CAST(0x0000A58000000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (3, N'456', N'Pete', N'c:\323456.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (4, N'789', N'Pete', N'c:\423456.mp3', CAST(0x0000A58000000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (5, N'1011', N'Pete', N'c:\523456.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (6, N'1112', N'Jack', N'c:\623456.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (7, N'1312', N'Jack', N'c:\723456.mp3', CAST(0x0000A56C00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (8, N'1412', N'Jack', N'c:\823456.mp3', CAST(0x0000A54300000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (9, N'1516', N'Jack', N'c:\923456.mp3', CAST(0x0000A58000000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (10, N'1817', N'Jack', N'c:\133456.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (11, N'1819', N'Sherry', N'c:\143456.mp3', CAST(0x0000A52400000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (12, N'2021', N'Sherry', N'c:\153456.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (13, N'2122', N'Sherry', N'c:\163456.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (14, N'2123', N'Pete', N'c:\173456.mp3', CAST(0x0000A52400000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (15, N'2124', N'Pete', N'c:\183456.mp3', CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (16, N'2125', N'Sherry', N'c:\193456.mp3', CAST(0x0000A50600000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (17, N'2126', N'Jack', N'c:\124456.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (18, N'2127', N'Pete', N'c:\125456.mp3', CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (19, N'2128', N'Sherry', N'c:\aaaa1.mp3', CAST(0x0000A4E700000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (20, N'2129', N'Jack', N'c:\aaaa2.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (21, N'3121', N'Jack', N'c:\aaaa3.mp3', CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (22, N'3122', N'Sherry', N'c:\aaaa4.mp3', CAST(0x0000A54300000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (23, N'3123', N'Jack', N'c:\aaaa5.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (24, N'3124', N'Jack', N'c:\aaaa6.mp3', CAST(0x0000A13900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (25, N'3125', N'Sherry', N'c:\aaaa7.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (26, N'3126', N'Jack', N'c:\aaaa8.mp3', CAST(0x0000A56C00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (27, N'3127', N'Pete', N'c:\aaaa9.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (28, N'3128', N'Pete', N'c:\bbbbb1.mp3', CAST(0x0000A54300000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (29, N'3129', N'Sherry', N'c:\bbbbb2.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (30, N'4121', N'Jack', N'c:\bbbbb3.mp3', CAST(0x0000A52400000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (31, N'4122', N'Pete', N'c:\bbbbb4.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (32, N'4123', N'Jack', N'c:\bbbbb5.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (33, N'4124', N'Jack', N'c:\bbbbb6.mp3', CAST(0x0000A50600000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (34, N'4125', N'Sherry', N'c:\bbbbb7.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (35, N'4126', N'Jack', N'c:\bbbbb8.mp3', CAST(0x0000A56C00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (36, N'4127', N'Jack', N'c:\bbbbb9.mp3', CAST(0x0000A4E700000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (37, N'4128', N'Jack', N'c:\cccccc1.mp3', CAST(0x0000A54300000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (38, N'4129', N'Pete', N'c:\cccccc2.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (39, N'5121', N'Sherry', N'c:\cccccc3.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (40, N'5122', N'Jack', N'c:\cccccc4.mp3', CAST(0x0000A56C00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (41, N'5123', N'Jack', N'c:\cccccc5.mp3', CAST(0x0000A54300000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (42, N'5124', N'Sherry', N'c:\cccccc6.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (43, N'5125', N'Pete', N'c:\cccccc7.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (44, N'5126', N'Jack', N'c:\cccccc8.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (45, N'5127', N'Pete', N'c:\cccccc9.mp3', CAST(0x0000A4E700000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (46, N'5128', N'Pete', N'c:\ddddd1.mp3', CAST(0x0000A54300000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (47, N'5129', N'Pete', N'c:\ddddd2.mp3', CAST(0x0000A4E700000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (48, N'6121', N'Pete', N'c:\ddddd3.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (49, N'6122', N'Pete', N'c:\ddddd4.mp3', CAST(0x0000A50600000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (50, N'6123', N'Pete', N'c:\ddddd5.mp3', CAST(0x0000A5BC00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (51, N'6124', N'Pete', N'c:\ddddd6.mp3', CAST(0x0000A52400000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (52, N'6125', N'Pete', N'c:\ddddd7.mp3', CAST(0x0000A5F900000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (53, N'6126', N'Pete', N'c:\ddddd8.mp3', CAST(0x0000A50600000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (54, N'6127', N'Pete', N'c:\ddddd9.mp3', CAST(0x0000A5DB00000000 AS DateTime))
INSERT [dbo].[ProximitySelectTable] ([ID], [TextIdentifier], [UserName], [FileName], [DateCreated]) VALUES (55, N'6128', N'Jack', N'c:\ddddd0.mp3', CAST(0x0000A5F900000000 AS DateTime))
SET IDENTITY_INSERT [dbo].[ProximitySelectTable] OFF
答案 0 :(得分:2)
您当前的查询不正确(缺少ORDER BY
且不需要UNION
而不是UNION ALL
),但使用正确的索引时,其固定版本可能效率最高。
我的row_number
答案首先是
WITH CTE1
AS (SELECT *,
ROW_NUMBER() OVER (ORDER BY DateCreated) AS RN
FROM [ProximitySelectTable]
WHERE UserName = @UserName),
CTE2
AS (SELECT *,
MAX(CASE
WHEN @TextIdentifier = TextIdentifier
THEN RN
END) OVER () AS FocusRN
FROM CTE1)
SELECT ID,
TextIdentifier,
UserName,
FileName,
DateCreated
FROM CTE2
WHERE RN BETWEEN FocusRN - 10 AND FocusRN + 10
ORDER BY DateCreated
当前查询的固定版本
SELECT * FROM (
SELECT TOP 10 *
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND [DateCreated] < @ProximityDate
ORDER BY [DateCreated] DESC
UNION ALL
SELECT *
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND TextIdentifier = @TextIdentifier
UNION ALL
SELECT TOP 10 *
FROM dbo.ProximitySelectTable
WHERE UserName = @UserName
AND [DateCreated] > @ProximityDate
ORDER BY [DateCreated] ASC
) x
ORDER BY x.DateCreated
这需要UserName, TextIdentifier
的索引用于初始搜索,而UserName, DateCreated
则需要TOP 10
的搜索,而不是排序。
答案 1 :(得分:1)
没有一个运行SQL Server 2008的方便,但我检查了以下所有功能在2008年可用:
DECLARE @UserName NVARCHAR(255) = 'Pete'
DECLARE @TextIdentifier NVARCHAR(255) = '456'
;WITH
cte1 AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY DateCreated) AS RowNumber
FROM ProximitySelectTable
),
cte2 AS
(
SELECT TOP 1
RowNumber
FROM cte1
WHERE UserName = @UserName AND TextIdentifier = @TextIdentifier
ORDER BY RowNumber
)
SELECT *
FROM cte1, cte2
WHERE cte1.RowNumber BETWEEN (cte2.RowNumber - 10) AND (cte2.RowNumber + 10)
cte1
对从1到n的所有行进行编号。 cte2
找到目标行的行号。最终SELECT
只扫描过去10行和接下来的10行。
答案 2 :(得分:1)
以下是ROW_NUMBER解决方案的伪代码:
WITH cte1 AS (
SELECT desiredcolumns, ROW_NUMBER() OVER (ORDER BY DateCol) AS rn
FROM MyTable
),
cte2 AS (
SELECT ID, rn
FROM cte1
WHERE TextIdentifier = @TextIdentifier
)
SELECT desiredcolumns
FROM cte1
CROSS JOIN cte2
WHERE cte1.rn > cte2.rn - 10
AND cte1.rn < cte2.rn + 9