Sql 我不能使用索引。因此,您的查询结构无法降低速度。每次运行查询时,我都会删除缓存,以显示正确的时间。我将检查执行计划并放置一个非聚集索引。不会真正有帮助,子字符串将导致扫描..您可以将子字符串(LinPresup.IdPedido,5,2)存储在另一列中..

Sql 我不能使用索引。因此,您的查询结构无法降低速度。每次运行查询时,我都会删除缓存,以显示正确的时间。我将检查执行计划并放置一个非聚集索引。不会真正有帮助,子字符串将导致扫描..您可以将子字符串(LinPresup.IdPedido,5,2)存储在另一列中..,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,我不能使用索引。因此,您的查询结构无法降低速度。每次运行查询时,我都会删除缓存,以显示正确的时间。我将检查执行计划并放置一个非聚集索引。不会真正有帮助,子字符串将导致扫描..您可以将子字符串(LinPresup.IdPedido,5,2)存储在另一列中..将计算列子字符串(LinPresup.IdPedido,5,2)作为NewCol,并在NewCol上具有索引也会有帮助。Sargable意味着它可以使用索引。您的查询结构不能因此使其变慢。将“LIKE”更改为“=”,但我只是优化了运行时。问题一


我不能使用索引。因此,您的查询结构无法降低速度。每次运行查询时,我都会删除缓存,以显示正确的时间。我将检查执行计划并放置一个非聚集索引。不会真正有帮助,子字符串将导致扫描..您可以将子字符串(LinPresup.IdPedido,5,2)存储在另一列中..将计算列子字符串(LinPresup.IdPedido,5,2)作为NewCol,并在NewCol上具有索引也会有帮助。Sargable意味着它可以使用索引。您的查询结构不能因此使其变慢。将“LIKE”更改为“=”,但我只是优化了运行时。问题一定出在别处。查询的较高成本是:-聚集索引扫描[2011\u Central]。[dbo]。[LinPresup]。[PK\u LinPresup\u Linea\u IdResupUsto\u IdEdido]78%。-聚集索引Seek[2011_Central].[dbo].[Pedidos].[PK_Pedidos_IdPedido]19%听起来不错。我将按照@sqlnemage的建议创建一个计算列,这将允许您充分利用索引。问题一定出在别处。查询的较高成本是:-聚集索引扫描[2011\u Central]。[dbo]。[LinPresup]。[PK\u LinPresup\u Linea\u IdResupUsto\u IdEdido]78%。-聚集索引Seek[2011_Central].[dbo].[Pedidos].[PK_Pedidos_IdPedido]19%听起来不错。我会像@sqlnemage建议的那样创建一个计算列,这将允许您充分利用索引。我证明了我引用的每一件事。精彩的回答。我证明了我引用的每一件事。
Declare @FILTROPAG bigint 
set @FILTROPAG = 1

Declare @FECHATRABAJO DATETIME
set @FECHATRABAJO = getDate()

Select * from(
SELECT distinct Linpresup.IdCliente, Linpresup.IdPedido, Linpresup.FSE, Linpresup.IdArticulo, 
        Linpresup.Des, ((Linpresup.can*linpresup.mca)-(linpresup.srv*linpresup.mca)) as Pendiente,
        Linpresup.IdAlmacen, linpresup.IdPista, articulos.Tip, linpresup.Linea,
        ROW_NUMBER() OVER(ORDER BY CONVERT(Char(19), Linpresup.FSE, 120) +
             Linpresup.IdPedido + CONVERT(char(2), linpresup.Linea) DESC) as NUM_REG
FROM Linpresup INNER JOIN Pedidos on LinPresup.IdPedido = Pedidos.IdPedido
               INNER JOIN Articulos ON Linpresup.IdArticulo = Articulos.IdArticulo
where  pedidos.Cerrado = 'false' and linpresup.IdPedido <> '' and linpresup.can <> linpresup.srv
        and Linpresup.FecAnulacion is null and Linpresup.Fse <= @FECHATRABAJO 
        and LinPresup.IdCliente not in (Select IdCliente from Clientes where Ctd = '4')
        and Substring(LinPresup.IdPedido, 5, 2) LIKE '11' or Substring(LinPresup.IdPedido, 5, 2) LIKE '10' 
) as TablaTemp
WHERE NUM_REG BETWEEN @FILTROPAG AND 1500   
order by NUM_REG ASC
CHECKPOINT;
go
dbcc freeproccache
go
dbcc dropcleanbuffers
go

Declare @FILTROPAG bigint
set @FILTROPAG = 1
Declare @FECHATRABAJO DATETIME
set @FECHATRABAJO = getDate()

SELECT  Linpresup.IdCliente, Linpresup.IdPedido, Linpresup.FSE, Linpresup.IdArticulo, 
        Linpresup.Des, Linpresup.can, linpresup.mca, linpresup.srv,
        Linpresup.IdAlmacen, linpresup.IdPista, linpresup.Linea
into #TEMPREP
FROM Linpresup
where Linpresup.FecAnulacion is null and linpresup.IdPedido <> ''
    and (linpresup.can <> linpresup.srv) and Linpresup.Fse <= @FECHATRABAJO 

Select *, ((can*mca)-(srv*mca)) as Pendiente
From(
    Select tablaTemp.*, ROW_NUMBER() OVER(ORDER BY FSECONVERT + IDPedido + LINCONVERT DESC) as NUM_REG, Articulos.Tip
    From(
            Select #TEMPREP.*, 
                    Substring(#TEMPREP.IdPedido, 5, 2) as NewCol,
                    CONVERT(Char(19), #TEMPREP.FSE, 120) as FSECONVERT, CONVERT(char(2), #TEMPREP.Linea) as LINCONVERT
            from #TEMPREP INNER JOIN Pedidos on #TEMPREP.IdPedido = Pedidos.IdPedido
            where Pedidos.Cerrado = 'false' 
                   and #TEMPREP.IdCliente not in (Select IdCliente from Clientes where Ctd = '4')) as tablaTemp
    inner join Articulos on tablaTemp.IDArticulo = Articulos.IdArticulo
    where (NewCol = '10' or NewCol = '11')) as TablaTemp2
where NUM_REG BETWEEN @FILTROPAG AND 1500   
order by NUM_REG ASC

DROP TABLE #TEMPREP
and Substring(LinPresup.IdPedido, 5, 2) LIKE '11' 
or Substring(LinPresup.IdPedido, 5, 2) LIKE '10'
and Substring(LinPresup.IdPedido, 5, 2) LIKE '11' 
or Substring(LinPresup.IdPedido, 5, 2) LIKE '10'