Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query running very slowly

Author  Topic 

Gyto
Posting Yak Master

144 Posts

Posted - 2008-05-21 : 09:50:58
Hi there,

I have the following stored procedure that seems to be running much slower than I would expect. It is not dealing with a huge number of rows or anything, around 1000 at most.


CREATE procedure spRegReport

(
@ParkingCode Varchar(255),
@Location Varchar(255),
@Department Varchar(255),
@CostCode Varchar(255),
@CurrentBan Varchar(255)
)

AS


UPDATE dbo.tCarPass SET ReinstatementDate = NULL WHERE (ReinstatementDate < GetDate())


INSERT INTO dbo.tRegReportData (PassNo, Surname, Forename, NonParkingDay, TelNo)
SELECT tCarPass.PassNo, tCarPass.Surname, tCarPass.Forename, tCarPass.NonParkingCode, tCarPass.TelNo
FROM tCarPass
WHERE ((tCarPass.NonParkingCode = @ParkingCode OR @ParkingCode = 'ALL')
AND (tCarPass.Location = @Location OR @Location = 'ALL')
AND (tCarPass.Department = @Department OR @Department = 'ALL')
AND (tCarPass.CostCode = @CostCode OR @CostCode = 'ALL')
AND (tCarPass.ReinstatementDate >= GetDate() OR @CurrentBan = 'ALL'))
GO


Any ideas on why it is slow or how I could speed it up?

Thanks

Matt

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-05-21 : 10:20:56
When you say, "around 1000 at most", is that the total number of rows in tCarPass or how many rows satisfy your criteria?
If you just do the SELECT without the INSERT is the execution time still about the same?

Be One with the Optimizer
TG
Go to Top of Page

Gyto
Posting Yak Master

144 Posts

Posted - 2008-05-21 : 10:58:02
1000 is roughly the total rows in tCarPass, so it will currently never be much more than that....usually much less, in fact.

The query runs almost instantly from query analyser, with or without the insert statement, but sometimes takes up to about 15 seconds when run from within the application, even though there is almost no other code on the page. Also, if you run it once, then again immediately after, the 2nd time is usually much quicker than the first.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-21 : 11:33:22
quote:
Originally posted by Gyto

1000 is roughly the total rows in tCarPass, so it will currently never be much more than that....usually much less, in fact.

The query runs almost instantly from query analyser, with or without the insert statement, but sometimes takes up to about 15 seconds when run from within the application, even though there is almost no other code on the page. Also, if you run it once, then again immediately after, the 2nd time is usually much quicker than the first.


How many indexes do you have on table? Also have a had a look at execution plan to see what is bottleneck statement?
Go to Top of Page

Gyto
Posting Yak Master

144 Posts

Posted - 2008-05-21 : 11:50:32
quote:
Originally posted by visakh16

quote:
Originally posted by Gyto

1000 is roughly the total rows in tCarPass, so it will currently never be much more than that....usually much less, in fact.

The query runs almost instantly from query analyser, with or without the insert statement, but sometimes takes up to about 15 seconds when run from within the application, even though there is almost no other code on the page. Also, if you run it once, then again immediately after, the 2nd time is usually much quicker than the first.


How many indexes do you have on table? Also have a had a look at execution plan to see what is bottleneck statement?



There is just the one default index on the tCarPass table.....I have to confess I am not an expert re. indexing! Also where can I find the execution plan?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-21 : 12:07:03
quote:
Originally posted by Gyto

quote:
Originally posted by visakh16

quote:
Originally posted by Gyto

1000 is roughly the total rows in tCarPass, so it will currently never be much more than that....usually much less, in fact.

The query runs almost instantly from query analyser, with or without the insert statement, but sometimes takes up to about 15 seconds when run from within the application, even though there is almost no other code on the page. Also, if you run it once, then again immediately after, the 2nd time is usually much quicker than the first.


How many indexes do you have on table? Also have a had a look at execution plan to see what is bottleneck statement?



There is just the one default index on the tCarPass table.....I have to confess I am not an expert re. indexing! Also where can I find the execution plan?


Execute the procedure in Query Analyser with shoe execution plan on and you can see execution plan along with your results as a seperate tab. Look for costly steps (greatest % values)
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-05-21 : 12:56:47
Since it runs very quickly from query analyzer (and there is only 1K rows) I don't think indexiing is the issue. You say it only takes a long time when called from your application? I would put some logging in your app to monitor the duration of everying the page is doing to see where the time is spent. Also profile the sql call to make sure that is the call you expect it to be and that there aren't other calls that you aren't aware of happening. At anyrate it sounds like your problem is not in the database.

Be One with the Optimizer
TG
Go to Top of Page

Gyto
Posting Yak Master

144 Posts

Posted - 2008-05-22 : 06:35:58
This is really the only 'functional' code in the page:


objConn.execute("DELETE FROM tRegReportData")

Set CmdStoredProc = Server.CreateObject("ADODB.Command")

CmdStoredProc.ActiveConnection = objConn
CmdStoredProc.CommandText = "spRegReport"
CmdStoredProc.CommandType = adCmdStoredProc

CmdStoredProc.Parameters.Append CmdStoredProc.CreateParameter("@ParkingCode", adVarChar, adParamInput, 255, ParkingCode)
CmdStoredProc.Parameters.Append CmdStoredProc.CreateParameter("@Location", adVarChar, adParamInput, 255, Location)
CmdStoredProc.Parameters.Append CmdStoredProc.CreateParameter("@Department", adVarChar, adParamInput, 255, Department)
CmdStoredProc.Parameters.Append CmdStoredProc.CreateParameter("@CostCode", adVarChar, adParamInput, 255, CostCode)
CmdStoredProc.Parameters.Append CmdStoredProc.CreateParameter("@CurrentBan", adVarChar, adParamInput, 255, CurrentBan)

CmdStoredProc.Execute , , adExecuteNoRecords

Set CmdStoredProc = Nothing


I can't really see what there is to slow it down?

However, it seems it only runs slowly the first time.....if you run it again straight after it is almost instant.....still not sure why it takes so long on the first attempt though?!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-22 : 07:30:52
Have you made a trace on your server to see
"Page Life Expectancy" and "Buffer Cache Hit Ratio"?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -