all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

would a Stored Procedure help


would a Stored Procedure help Brian Higgins
2/28/2005 7:21:48 PM
sql server programming:
I have a large and ever growing database with over 15 million records, all
timestamped, and increasing by roughly 30,000 records per day so efficient
queries are essential.

I need to pull a list of records that match certain criteria, that have
timestamps in the last hour. and I need to run it 45 times in a row, and I
need the results in the shortest amount of time as possible.

the only thing I need at this point in the program is a boolean value
telling me that either something has been logged recently, or it has not.

the basic logic of the program is currently as follows:

---------------------------------------------------------------------------
For ServerID = 1 to 45
open Recordset
SELECT ServerInfo.ServerID
FROM ServerInfo INNER JOIN (PInfo INNER JOIN PDetails ON
PInfo.ID = PDetails.ID) ON ServerInfo.ID = PInfo.ID
WHERE (((PDetails.Status)<>2 And (PDetails.Status)<>5) AND
((DateDiff(hh,[TmeStamp],now()))<1))
GROUP BY ServerInfo.ServerID
HAVING (((ServerInfo.ServerID)=%SERVERID 1 thru 45%));

If Recordset.EOF = False Then
'good
DoNothing = True
Else
'Bad
DoNothing = False
End If

Close Recordset

Next ServerID
---------------------------------------------------------------------------

as you can see this is a very very very in-efficient way of doing things
(loop takes over 5 min to run this way, I want it to complete in less then 5
seconds)
in this manor the query is searching through tens of millions of fields each
time it runs


This is how I would like the program logic to flow:
---------------------------------------------------------------------------
Open Recordset1
SELECT PID
FROM PDetails
WHERE (PStatus <> 2) AND (PStatus <> 5) AND (DATEDIFF(hh, TmeStamp,
{ fn NOW() }) < 1)
GROUP BY PDetails.PID;

If Recordset1.EOF = False Then

For ServerID = 1 to 45
open Recordset2 Select * From Recordset1 Where PID = 1 OR PID = 2 or
PID = 3

If Recordset2.EOF = False Then
'Good
DoNothing = True
Else
'Bad
DoNothing = False
End If

Close Recordset2

Next ServerID

Else
'stop there because nothing has happened in the last hour
End If

Close Recordset1
---------------------------------------------------------------------------

In this manor the large brunt of the records are only processed when opening
Recordset1
and the subsequent 45 queries performed by recordset2 are only going to
parse through a few records at most, if any at all

all of this is called from within (vbscript) ASP, what is the best way to
achieve the desired end result (fast page load, near realtime results), with
the least ammount of processing time / bandwidth required (the web server
only has a 100Mb connection to the SQL server, and the database is nearly
1Gb...)?


Thanks in Advance...

Re: would a Stored Procedure help David Browne
2/28/2005 7:44:42 PM

[quoted text, click to view]

You don't need a stored procedure, you just need to change the SQL. You
need to end up with a query of the form

select * from t where DateCol > '2005-03-01 16:00:00'

Yes, you want to hard-code the date in the query. This is one of the
exceptions to the rule of using parameter markers for your queries. This
will give SQL Server the maximum amount of statistical information about how
to process your query.

First, ditch the query builder, and write the SQL by hand. All those
useless parentheses just confuse the issue:

Also ditch the HAVING clause and return all the ServerID's then you can
iterate them on the client side and process all 45 servers with one query.
Just ORDER BY ServerId so you know that if the first row has ServerID=2 then
there was no row with ServerID=1, etc.

SELECT ServerInfo.ServerID
FROM ServerInfo
INNER JOIN PInfo
ON ServerInfo.ID = PInfo.ID
INNER JOIN PDetails
ON PInfo.ID = PDetails.ID
WHERE PDetails.Status<>2
And PDetails.Status)<>5
AND TmeStamp >= '2005-03-01 16:00:00'
GROUP BY ServerInfo.ServerID
ORDER BY ServerInfo.ServerID

With any kind of index on TmeStamp this query should be reasonably cheap.
With a clustered index on TmeStamp it should be downright fast.

David

Re: would a Stored Procedure help Robbe Morris [C# MVP]
2/28/2005 8:32:29 PM
Yes, it would definitely help. See if this sample on table variables sparks
some ideas. See if you can't run a single query that gets the top 45-100
of your records and put the primary key in a table variable. Then run
subsequent queries where the primary key is also found in the table
variable.

My gut says you can do this all in one query but your code sample and
business logic aren't 100% clear. In any event, doing all of this in a
proc with one trip to the db should dramatically improve performance.

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=529

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.learncsharp.net/home/listings.aspx



[quoted text, click to view]

Re: would a Stored Procedure help Brian Higgins
2/28/2005 11:42:21 PM
It's a little more complicated with the tables and queries so I'm not sure
if that will work or not....

The relevant DBStructure is like this,

[ServerInfo] {45 records in DB}
-ServerID
-ServerName

[PInfo] {~125 records}
-PID
-ServerID (one to many from [ServerInfo])

[PDetails] {over 15 million records, updated every 60 seconds}
-DetailID
-PID (one to many from [PInfo])
-PStatus
-TmeStamp

what I need is to get the ServerName/ID for every record in PDetails that
has a PStatus not = x, where the time stamp is within the last hour.

I need to have the info available to me as I go through the list of servers
in the ServerInfo table...

with this bit of info in mind, do you think it's still possible to do this
with one main query? and if so, how do I do that with SQL 2000? (I'm a
systems engineer who just got stuck with this SQL/ASP project, so I'm
stretching a little bit for this)

Thanks.



"David Browne" <davidbaxterbrowne no potted meat@hotmail.com> wrote in
message news:eiYg6FgHFHA.400@TK2MSFTNGP14.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button