SunQuest
           Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

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 Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old November 6th, 2003, 08:46 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
Improving a query

I've got two queries that are really slow.

My tables look like this:
Table A:
id (primary key)
Has approx. 140,000 records

Table B:
id (primary key)
a_id (there's an index on this)
created_datetime
Has approx. 1,000,000 records

For the first query, I want to find the number of A's that have a certain number of B's:
Code:
SELECT a_id, COUNT(*) AS cnt FROM b WHERE a_id > 0 GROUP BY a_id HAVING cnt >= 10 AND cnt < 20

The problem is, I really only want the total number of A's, not each one, so after I do the query, I simply look at mysql_num_rows().

The second query is similar, but instead of the number of B's, I want to look at the time difference between the last and the first.
Code:
SELECT a_id, MAX(UNIX_TIMESTAMP(created_datetime))-MIN(UNIX_TIMESTAMP(created_datetime)) AS t FROM b WHERE a_id > 0 GROUP BY a_id HAVING t >= 600 AND cnt < 1200

Again, all I want is the number of rows from this and it seems like I'm wasting a lot of processing by doing it this way.

Any ideas?

Reply With Quote
  #2  
Old November 6th, 2003, 08:51 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 2
RE: Improving a query

Just out of interest, how slow is 'really slow'?

Can you give average times for each?

Reply With Quote
  #3  
Old November 6th, 2003, 08:55 PM
-vertigo- -vertigo- is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Louth, Lincolnshire
Posts: 314 -vertigo- User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 2
RE: Improving a query

Also, on average, how many times do yu run those queries before you modify those tables?

Maybe you can cache the results in a heap table, helps sometimes.

Reply With Quote
  #4  
Old November 6th, 2003, 11:04 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
RE: Improving a query

By slow, I think they were taking about 10 seconds each. I decided cacheing was the way to go. Thanks for the suggestion though.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > Improving a query


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 2 hosted by Hostway