此查询有一个小问题。它使我返回约13k行,大约需要12秒或更长时间。
SELECT
hla.Id,
hc.Nome AS Colaborador,
hla.UAP,
hra.Referencia,
hra.QtdAbastecimento,
hra.QtdPecasPorCaixa,
hra.QtdCaixas,
A.Etiqueta,
A.Localizacao
FROM
hListasAbastecimento hla
INNER JOIN hColaboradores hc
ON hc.Id = hla.ColaboradorId
INNER JOIN
hReferenciasAbastecimento hra
ON hla.Id = hra.ListaAbastecimentoId
LEFT JOIN OPENQUERY(MACPAC,
'SELECT
FET001.ET0102 AS Referencia,
FET001.ET0101 AS Etiqueta,
FET001.ET0109 AS Localizacao
FROM
AUTO.D805DATPOR.FET001 FET001
WHERE FET001.ET0104 = ''POE''
AND FET001.ET0105 = ''DIS''
ORDER BY FET001.ET0101 ASC ') A
ON A.Referencia = hra.Referencia
WHERE hla.Id = @Id
我不确定该如何提高性能。我已经在使用唯一的主键来链接两个表。我也无法控制链接服务器,因此无法创建新索引
更新
我通过一个同事发现,链接服务器上有一张表可以帮助我通过“ UAP”获得结果,仅用4秒即可获得大约1700行,这正是我所需要的
ALTER PROCEDURE GenerateListaAbastecimento
@Id INT,
@UAP NVARCHAR(20)
AS
BEGIN
CREATE TABLE #tempTable
(
Id INT PRIMARY KEY,
Referencia NVARCHAR(15),
Etiqueta INT,
Localizacao NVARCHAR(20)
UNIQUE(Id, Etiqueta)
)
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL ='INSERT INTO #tempTable
SELECT
Id,
Referencia,
Etiqueta,
Localizacao
FROM OPENQUERY(MACPAC,
''SELECT
ROW_NUMBER() OVER(ORDER BY FET001.ET0101 ASC) AS Id,
A.RH6001 as Referencia,
FET001.ET0101 AS Etiqueta,
FET001.ET0109 AS Localizacao
FROM AUTO.D805DATPOR.TRP060H AS A
LEFT JOIN AUTO.D805DATPOR.FET001 FET001
ON FET001.ET0102 = A.RH6001
AND FET001.ET0104 = ''''POE''''
AND FET001.ET0105 = ''''DIS''''
AND A.RH6002 = N''' + QUOTENAME(@UAP,N'''') + N''' '')'
EXEC sp_executesql @SQL
SELECT
hla.Id,
hc.Nome AS Colaborador,
hla.UAP,
hra.Referencia,
hra.QtdAbastecimento,
hra.QtdPecasPorCaixa,
hra.QtdCaixas,
A.Etiqueta,
A.Localizacao
FROM
hListasAbastecimento hla
INNER JOIN hColaboradores hc
ON hc.Id = hla.ColaboradorId
INNER JOIN
hReferenciasAbastecimento hra
ON hla.Id = hra.ListaAbastecimentoId
LEFT JOIN #tempTable A
ON A.Referencia = hra.Referencia
WHERE hla.Id = @Id
ORDER BY A.Etiqueta ASC
DROP TABLE #tempTable
END