Delphi - SQL Server query takes a long time to run

124 Views Asked by At

Delhi XE3 Enterprise, SQL Server 2008

When I run the following the query it takes 4 seconds to run in the Delphi program. When I run it from SQL Server management studio it only takes .5 seconds to run.

SELECT
      SUM(ISNULL(soi.ExtendedUnitPrice, 0)) AS TotalExtendedUnitPrice,
      SUM(ISNULL(soi.ExtendedUnitCost, 0))  AS TotalExtendedUnitCost,
      SUM(ISNULL(soi.OriginalTotal, 0))     AS TotalOriginalTotal,
      SUM(ISNULL(soi.LineWeight, 0))        AS TotalLineWeight
FROM SalesOrderItems soi
WHERE (soi.SalesOrderID = :SalesOrderID)
1

There are 1 best solutions below

0
Donald Adams On

To fix it I added the soi.SalesOrderID to the field list and added a GROUP BY SalesOrderID. Very quick now.