0

现在我有这个,它检索大约 16K 记录:

DECLARE @LINE_ID INT
SET @LINE_ID = 37
SELECT 
    a.eventType as 'evento', 
    a.tipo as 'tipo', 
    a.nroSoldadura as 'nro soldadura', 
    a.cuentaOdometrica as 'Distancia Absoluta [m]',
    a.x as 'x', 
    a.y as 'y', 
    a.z as 'z', 
    a.zonaGeografica as 'Zona geografica', 
    a.orientacionSoldadura as 'orientacion', 
    a.prof * 100 as 'prof maxima [%]',
    a.longitud as 'long. [mm]',
    a.ancho as 'ancho [mm]',
    a.IntExt as 'INT o EXT',
    a.cluster as 'Cluster',
    a.espesorPared as 'espesor de pared nominal',
    a.longitudEfectiva as 'longitud efectiva',
    a.anchoEfectivo as 'Ancho efectivo',
    a.areaEfectiva as 'Area efectiva',
    a.factorSeguridad as 'Factor de seguridad',
    a.MAOP as 'MAOP (kPa)',
    a.ruptura as 'Ruptura 0.85dL (kPa)',
    a.FER as 'FER',
    NULL as 'Diametro maximo',
    NULL as 'Diametro minimo',
    NULL as 'Ovalizacion',
    NULL as 'Ovalizacion axial',
    a.orientacionSoldadura as 'Orientacion soldadura',
    a.velocidadHerramienta as 'Velc Herr',
    a.comentarios as 'comentarios'
FROM ILI_IMPORT_MFL as a
JOIN ILI_INSPECTION as b ON a.ILI_INSPECTION_ID = b.ILI_INSPECTION_ID
WHERE 
    a.ID IN (
        SELECT TOP 1 c.ID
        FROM ILI_IMPORT_MFL as c 
        JOIN ILI_INSPECTION as d ON c.ILI_INSPECTION_ID = d.ILI_INSPECTION_ID
        WHERE a.nroSoldadura = c.nroSoldadura
            AND d.LINE_ID = @LINE_ID
        ORDER BY d.BEGIN_DATE 
    )
    AND b.LINE_ID = @LINE_ID
ORDER BY a.nroSoldadura, b.BEGIN_DATE 

ILI_IMPORT_MFL 和 ILI_INSPECTION 之间存在连接,因为 ILI_INSPECTION 包含我需要的日期和行 ID。
由于子查询,现在这个查询需要大约 6 分钟来执行。子查询在那里,因为每个 nroSoldadura 可能有不止一行,我只想要其中最旧的一行。

我正在使用 SQL Server 2008。
我该如何解决这个问题?谢谢。

编辑:尝试向 nroSoldadura 和 BEGIN_DATE 列添加索引,并将查询时间缩短到 30 秒。仍然很高兴看到可能的优化以便学习。

4

2 回答 2

1

您可以尝试使用ROW_NUMBER() ... PARTITION BY来替换您的子查询:

DECLARE @LINE_ID INT
SET @LINE_ID = 37
SELECT 
    a.eventType as 'evento', 
    a.tipo as 'tipo', 
    a.nroSoldadura as 'nro soldadura', 
    a.cuentaOdometrica as 'Distancia Absoluta [m]',
    a.x as 'x', 
    a.y as 'y', 
    a.z as 'z', 
    a.zonaGeografica as 'Zona geografica', 
    a.orientacionSoldadura as 'orientacion', 
    a.prof * 100 as 'prof maxima [%]',
    a.longitud as 'long. [mm]',
    a.ancho as 'ancho [mm]',
    a.IntExt as 'INT o EXT',
    a.cluster as 'Cluster',
    a.espesorPared as 'espesor de pared nominal',
    a.longitudEfectiva as 'longitud efectiva',
    a.anchoEfectivo as 'Ancho efectivo',
    a.areaEfectiva as 'Area efectiva',
    a.factorSeguridad as 'Factor de seguridad',
    a.MAOP as 'MAOP (kPa)',
    a.ruptura as 'Ruptura 0.85dL (kPa)',
    a.FER as 'FER',
    NULL as 'Diametro maximo',
    NULL as 'Diametro minimo',
    NULL as 'Ovalizacion',
    NULL as 'Ovalizacion axial',
    a.orientacionSoldadura as 'Orientacion soldadura',
    a.velocidadHerramienta as 'Velc Herr',
    a.comentarios as 'comentarios'
    ROW_NUMBER() OVER (PARTITION BY a.ID ORDER BY d.BEGIN_DATE) As row
FROM ILI_IMPORT_MFL as a
JOIN ILI_INSPECTION as b ON a.ILI_INSPECTION_ID = b.ILI_INSPECTION_ID
WHERE 
    row = 1
    AND b.LINE_ID = @LINE_ID
ORDER BY a.nroSoldadura, b.BEGIN_DATE 
于 2013-01-15T14:31:11.313 回答
0

你想用row_number()这个。您需要将其放在子查询中。我认为这就是你想要的:

select * from (
SELECT 
    a.eventType as 'evento', 
    a.tipo as 'tipo', 
    a.nroSoldadura as 'nro soldadura', 
    a.cuentaOdometrica as 'Distancia Absoluta [m]',
    a.x as 'x', 
    a.y as 'y', 
    a.z as 'z', 
    a.zonaGeografica as 'Zona geografica', 
    a.orientacionSoldadura as 'orientacion', 
    a.prof * 100 as 'prof maxima [%]',
    a.longitud as 'long. [mm]',
    a.ancho as 'ancho [mm]',
    a.IntExt as 'INT o EXT',
    a.cluster as 'Cluster',
    a.espesorPared as 'espesor de pared nominal',
    a.longitudEfectiva as 'longitud efectiva',
    a.anchoEfectivo as 'Ancho efectivo',
    a.areaEfectiva as 'Area efectiva',
    a.factorSeguridad as 'Factor de seguridad',
    a.MAOP as 'MAOP (kPa)',
    a.ruptura as 'Ruptura 0.85dL (kPa)',
    a.FER as 'FER',
    NULL as 'Diametro maximo',
    NULL as 'Diametro minimo',
    NULL as 'Ovalizacion',
    NULL as 'Ovalizacion axial',
    a.orientacionSoldadura as 'Orientacion soldadura',
    a.velocidadHerramienta as 'Velc Herr',
    a.comentarios as 'comentarios',
    ROW_NUMBER() over (partition by a.nroSoldadura order by begin_date desc) as seqnum
FROM ILI_IMPORT_MFL as a
JOIN ILI_INSPECTION as b ON a.ILI_INSPECTION_ID = b.ILI_INSPECTION_ID
where b.line_id = @line_id
)
where seqnum = 1
于 2013-01-15T14:33:04.047 回答