通过修改查询或影响优化器的策略,是否可以通过一次查找或扫描检索与以下相同的数据?
与此类似的代码和架构目前在SQL Server 2014中。
复制脚本。设定:
USE tempdb;
GO
IF OBJECT_ID('dbo.TestUpload', 'U') IS NOT NULL
DROP TABLE dbo.TestUpload;
CREATE TABLE dbo.TestUpload(
JobRunId bigint NOT NULL,
ThingAName nvarchar(255) NOT NULL,
ThingAType nvarchar(255) NOT NULL,
ThingAGranularity nvarchar(255) NOT NULL,
ThingBName nvarchar(255) NOT NULL,
ThingBType nvarchar(255) NOT NULL,
ThingBGranularity nvarchar(255) NOT NULL
);
CREATE CLUSTERED INDEX IX_JobRunId ON dbo.TestUpload (JobRunId);
GO
INSERT INTO dbo.TestUpload (JobRunId, ThingAName, ThingAType, ThingAGranularity, ThingBName, ThingBType, ThingBGranularity)
VALUES (1, 'A', 'B', 'C', 'D', 'E', 'F');
GO 10
INSERT INTO dbo.TestUpload (JobRunId, ThingAName, ThingAType, ThingAGranularity, ThingBName, ThingBType, ThingBGranularity)
VALUES (1, 'D', 'E', 'F', 'A', 'B', 'C');
GO 10
查询:
DECLARE @JobRunID bigint = 1;
SELECT JobRunId,
ThingAName AS Name,
ThingAType AS [Type],
ThingAGranularity AS Granularity
FROM dbo.TestUpload
WHERE JobRunId = @JobRunID
UNION
SELECT JobRunId,
ThingBName AS Name,
ThingBType AS [Type],
ThingBGranularity AS Granularity
FROM dbo.TestUpload
WHERE JobRunId = @JobRunID;
拆除:
IF OBJECT_ID('dbo.TestUpload', 'U') IS NOT NULL
DROP TABLE dbo.TestUpload;
我认为这可能不是理想的模型。我正在尝试从开发人员那里获取有关如何选择架构的更多信息,但很好奇我是否忽略了TSQL技巧,因为更改查询比架构更容易。
UNION
因为必须删除重复项。