Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 18th, 2005, 09:36 PM
phil99 phil99 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 phil99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 41 sec
Reputation Power: 0
Timeout expired error on SQL Server query with subquery

hi,
i have an ASP page that is querying an SQL database, and a SQL timeout expired '80040e31' error occurs on the recordset.open line.

The query is resonably standard, except there is a correlated subquery to do a COUNT of 'other users' who registered lately against the same school. Is there a way to make the query more efficient, (uncorrelate the subquery or something) so that the timeout error does not occur?

I could take out the subquery, and then do a seperate query on each userID to see if there are 'other users' for that user, but i think that might be less efficient than using the subquery (only about 1 in 10 users has 'other users').

The users table has about 40000 records, and no field is very large, varchar 300 would be about the biggest.


Thanks for your help, hopefully ive given enough information! :-)

Heres the query:
SELECT Users.UserID, Users.FirstName, Users.LastName1, Users.Email, User_school_link.StartYear, User_school_link.FinishYear, Schools.SchoolID,
Schools.SchoolName,
(SELECT COUNT(U.UserID) AS myoutput
FROM User_school_link L INNER JOIN
Users U ON L.UserID = U.UserID
WHERE (L.FinishYear >= User_school_link.StartYear) AND (L.StartYear <= User_school_link.FinishYear) AND (L.AddDateTime >= '1 Dec 2004') AND (L.AddDateTime < '1 Jan 2005') AND (L.SchoolID = Schools.SchoolID) AND (L.UserID <> Users.UserID))
AS NewRegoInfo_COUNT
FROM User_school_link INNER JOIN
Schools ON User_school_link.SchoolID = Schools.SchoolID INNER JOIN
Users ON User_school_link.UserID = Users.UserID
WHERE (User_school_link.UserNotify = 1)
ORDER BY Users.UserID, User_school_link.StartYear

Reply With Quote
  #2  
Old January 31st, 2005, 04:22 PM
phil99 phil99 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 phil99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 41 sec
Reputation Power: 0
ok, there is not much i could do to the query to improve the efficiency - i didnt need to link on the users table in the subquery, so i took that out and it ran a few seconds faster in query analyser.

But I did a bit of research and i think the 'missing link' was indexes. The only indexes i had on these tables were the primary key indexes that SQL generates automatically when the tables are created. So i added indexes on the other columns in the user_school_link table that were specified in the WHERE clause (user_id, school_id, startyear, finishyear, adddatetime) and now the query runs with no timeouts! i dont know if the changes are completely optimal (not a dba) but it did seem to improve the query performance - only one record at a time is inserted or updated on the website, so the extra work SQL Server needs to perform because of the indexes seems to be minimal compared to performance gains the indexes give to the SELECT queries.

Reply With Quote
  #3  
Old January 31st, 2005, 04:30 PM
phil99 phil99 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 phil99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 41 sec
Reputation Power: 0
maybe someone who is a dba can offer advice on the best columns to index? (for future reference)

The design of the user_school_link table is as follows:
Name datatype length allow_nulls
LinkID int 4 0
UserID int 4 1
SchoolID int 4 1
FriendID int 4 1
UserNotify bit 1 0
StartYear int 4 1
FinishYear int 4 1
PrimarySchool bit 1 0
AddDateTime datetime 8 1
ModifyDateTime datetime 8 1

Reply With Quote
  #4  
Old February 2nd, 2005, 04:29 PM
kwenzel kwenzel is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 4 kwenzel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 12 sec
Reputation Power: 0
In general you should index any primary or foreign keys. Off the top of my head I would think you would want to index

UserID in the "User Table"
SchoolID in the "School" table
FriendID in the "Friends" table

and so on...

Reply With Quote
  #5  
Old February 2nd, 2005, 04:31 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Plus any field you maybe using in a WHERE clause.

Reply With Quote
  #6  
Old February 8th, 2005, 04:12 AM
Tonny-Soeroso Tonny-Soeroso is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 36 Tonny-Soeroso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 56 m 27 sec
Reputation Power: 0
what about using index on temp table?

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Timeout expired error on SQL Server query with subquery


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT