If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to speed up AVG(), MIN(), MAX() functions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-17-10, 07:19
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
How to speed up AVG(), MIN(), MAX() functions

Hi!

I have a table with about 400k rows.
When I query this:
Code:
SELECT MIN(some_metrics) FROM big_table
everything is OK, execution time is about 0.02 sec.

But when I add a WHERE clause
Code:
SELECT MIN(some_metrics) FROM big_table WHERE parent_id = 10
this query executes in about 2 minutes. Indexes on parent_id and some_metrics columns are created.

The question is - how to speed up this query?
Reply With Quote
  #2 (permalink)  
Old 12-17-10, 07:39
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Just read some more manuals.. I guess I should create a composite index (parent_id, some_metrics), right?
Reply With Quote
  #3 (permalink)  
Old 12-17-10, 08:40
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by tedd View Post
I guess I should create a composite index (parent_id, some_metrics), right?
right

for that particular query, that compound index will be a covering index

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 12-17-10, 10:09
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Thanks for the answer!

I'm still confused with all this index stuff Would you suggest something to read?
Reply With Quote
  #5 (permalink)  
Old 12-17-10, 10:41
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by tedd View Post
Would you suggest something to read?
start here: Optimizing your MySQL Application
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-20-10, 07:39
tedd tedd is offline
Registered User
 
Join Date: Nov 2010
Posts: 24
Thanks, nice article! Got some questions answered.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On