944,070 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 551
  • MySQL RSS
Oct 24th, 2009
0

Query optimization

Expand Post »
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
MySQL Syntax (Toggle Plain Text)
  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

MySQL Syntax (Toggle Plain Text)
  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

MySQL Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 24th, 2009
0
Re: Query optimization
Do you have index on username and date columns?
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007
Oct 24th, 2009
0
Re: Query optimization
No. Would that help significantly?
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 24th, 2009
0
Re: Query optimization
Yes, it will increase the performance dramatically.
Check this article for more info http://articles.sitepoint.com/articl...ql-application
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007
Oct 24th, 2009
0
Re: Query optimization
Click to Expand / Collapse  Quote originally posted by mwasif ...
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?
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 25th, 2009
0
Re: Query optimization
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; Oct 25th, 2009 at 5:51 am.
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: is mysql better than dbase 4
Next Thread in MySQL Forum Timeline: My data not appear





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC