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
LVL 1
Jason JonesGIS/APP DevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
Are there indexes on your date fields?  Also, you're joining on a date field returned from a subquery...which cannot be fast.
0
Jason JonesGIS/APP DevAuthor Commented:
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
0
Jason JonesGIS/APP DevAuthor Commented:
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
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

chapmandewCommented:
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)
0
Cedric_DCommented:
This should help:

execute:

create index ix1 on GWLMCD (DATE, UNID)

and rewrite query as:

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], DATE
                            FROM          GWLMCD) AS S ON S.UNID LIKE 'BUT00014' AND S.DATE = CONVERT(datetime, MakeDates_2.thedate)
GROUP BY MakeDates_2.thedate
ORDER BY MakeDates_2.thedate


on syntax error, you might need to enclode date column into brackets:  [date].

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason JonesGIS/APP DevAuthor Commented:
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
0
chapmandewCommented:
try this:

SELECT     UNID, TEMP, [Depth Below Measuring Point], DATE INTO #temp
FROM          GWLMCD
WHERE UNID = 'BUT00014'

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     * FROM #temp ) AS S ON S.DATE = CONVERT(datetime, MakeDates_2.thedate)
GROUP BY MakeDates_2.thedate
ORDER BY MakeDates_2.thedate
0
Jason JonesGIS/APP DevAuthor Commented:
Gee guys both run 100 times faster...  which one should I use and award points to?

THanks!
0
chapmandewCommented:
you can split them
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.