Query optimization

Reply

Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

Query optimization

 
0
  #1
31 Days Ago
Hi all.
I am writing a utility to manage virtual users on a mailserver w/ mysql backed. Unfortunately I am having performance issues when querying for the user's last access date/time.

DB has this structure

table users
  1. +------------------+-------------+------+-----+---------+-------+
  2. | FIELD | Type | NULL | Key | DEFAULT | Extra |
  3. +------------------+-------------+------+-----+---------+-------+
  4. | email | VARCHAR(80) | NO | PRI | NULL | |
  5. | force_change_pwd | INT(1) | NO | | 0 | |
  6. | password | MD5(20) | NO | | NULL | |
  7. | creation_date | DATETIME | YES | | NULL | |
  8. +------------------+-------------+------+-----+---------+-------+

table user_activity

  1. +----------------+--------------+------+-----+---------+----------------+
  2. | FIELD | Type | NULL | Key | DEFAULT | Extra |
  3. +----------------+--------------+------+-----+---------+----------------+
  4. | id | INT(11) | NO | PRI | NULL | AUTO_INCREMENT |
  5. | event | VARCHAR(30) | NO | MUL | NULL | |
  6. | username | VARCHAR(128) | NO | | NULL | |
  7. | domain | VARCHAR(128) | NO | MUL | NULL | |
  8. | remote_address | VARCHAR(255) | NO | | NULL | |
  9. | DATE | DATETIME | NO | MUL | NULL | |
  10. | comments | VARCHAR(255) | YES | | | |
  11. +----------------+--------------+------+-----+---------+----------------+


In my PHP page, I have the following code

  1. $mbox_users = 'SELECT email, creation_date FROM users ORDER BY email ASC';
  2. $result = mysql_query($mbox_users) OR die ("There was a problem with the SQL query: " . mysql_error());
  3. while($row = mysql_fetch_assoc($result))
  4. {
  5.  
  6. $email=$row['email'];
  7. $date=$row['creation_date'];
  8. if (is_null($date)) {
  9. $date = "N/A";
  10. }
  11. $mbox_access="SELECT date FROM user_activity WHERE username = '".$row['email']."' ORDER BY date DESC LIMIT 1";
  12. $result_access = mysql_query($mbox_access) OR die ("There was a problem with the SQL query: " . mysql_error());
  13. $last = mysql_fetch_assoc($result_access);
  14. echo '<li>'.$email.' (<b>Created: </b>'.$date.' - <b>Last Access: </b>'.$last['date'].')</li>';


Unfortunately with +20k users and over 100k records in the user_activity table, the queries are awfully slow.
Am I missing some obvious method of making the query faster?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 190
Reputation: mwasif is an unknown quantity at this point 
Solved Threads: 25
mwasif mwasif is offline Offline
Junior Poster
 
0
  #2
31 Days Ago
Do you have index on username and date columns?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster
 
0
  #3
31 Days Ago
No. Would that help significantly?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 190
Reputation: mwasif is an unknown quantity at this point 
Solved Threads: 25
mwasif mwasif is offline Offline
Junior Poster
 
0
  #4
31 Days Ago
Yes, it will increase the performance dramatically.
Check this article for more info http://articles.sitepoint.com/articl...ql-application
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster
 
0
  #5
31 Days Ago
Originally Posted by mwasif View Post
Yes, it will increase the performance dramatically.
Check this article for more info http://articles.sitepoint.com/articl...ql-application
Ok. I'll set up indexes on those two fields.
I was also wondering whether a JOIN query might improve the performance. Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 190
Reputation: mwasif is an unknown quantity at this point 
Solved Threads: 25
mwasif mwasif is offline Offline
Junior Poster
 
0
  #6
30 Days Ago
I am not sure about this. You have to test it yourself by implementing both approaches.

Don't forget to index email and username in tables before uisng JOIN.
Last edited by mwasif; 30 Days Ago at 5:51 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC