Link to home
Start Free TrialLog in
Avatar of Kristen Jones
Kristen JonesFlag for United States of America

asked on

SQL Query Help - TOO Slow

I have the following SQL statement that takes nearly 1 minute to execute on a Windows 2000 SQL server. The database have over a million records, but  this query only pulls about 630 of them, but takes way too long.  IT basically takes returns a set of daily records temperature measurements  and a depth of a particular UNID.  One day could have several readings hence the AVG and some days may be NULL where a measurement wasn't done.. so it makes the dates, joins them and displays the records..  


SELECT     CONVERT(varchar(6), MakeDates_2.thedate, 101) + CONVERT(varchar(4), MakeDates_2.thedate, 120) AS DateTime, 1.8 * CAST(AVG(S.TEMP) + 32 AS int) AS TEMP,
                      ROUND(AVG(S.[Depth Below Measuring Point]), 3) AS [Depth Below Measuring Point]
FROM         dbo.MakeDates('2/2/2007', '10/19/2008') AS MakeDates_2 LEFT OUTER JOIN
                          (SELECT     UNID, TEMP, [Depth Below Measuring Point], CAST(CONVERT(varchar(10), DATE, 120) AS datetime) AS DATETIME2
                            FROM          GWLMCD) AS S ON S.UNID LIKE 'BUT00014' AND S.DATETIME2 = MakeDates_2.thedate
GROUP BY MakeDates_2.thedate
ORDER BY MakeDates_2.thedate
Avatar of chapmandew
chapmandew
Flag of United States of America image

Are there indexes on your date fields?  Also, you're joining on a date field returned from a subquery...which cannot be fast.
Avatar of Kristen Jones

ASKER

There is an index for the UNID and another for Date...  This same query runs on another database with about 30K records and is very fast,  I just modified it to pull the same data type from another database...  I am open to any suggestions as I gotta get this down to a few seconds at most.

Thanks
Jason
Here is the Table info

CREATE TABLE [dbo].[GWLMCD](
      [UNID] [nvarchar](20) NULL,
      [WELL ID] [nvarchar](255) NULL,
      [DATE] [smalldatetime] NULL,
      [Depth Below Measuring Point] [float] NULL,
      [GroundWater Elevation] [float] NULL,
      [ID] [int] IDENTITY(1,1) NOT NULL,
      [TIME] [nvarchar](20) NULL,
      [Depth Below Ground Surface] [float] NULL,
      [Reference Elevation] [nvarchar](50) NULL,
      [TEMP] [float] NULL
GWLMCD Is the table w/ the million records?

it is this statement that is absolutely killing you:

LEFT OUTER JOIN
                          (SELECT     UNID, TEMP, [Depth Below Measuring Point], CAST(CONVERT(varchar(10), DATE, 120) AS datetime) AS DATETIME2
                            FROM          GWLMCD)
ASKER CERTIFIED SOLUTION
Avatar of Cedric_D
Cedric_D

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ye it is...

I am not a SQL Guru, I just took a query that I knew was working and changed it for this table..  if you could just copy and paste a solution that would really help me I have no idea where to start with this....

Thanks
Jason
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gee guys both run 100 times faster...  which one should I use and award points to?

THanks!
you can split them