| | |
Query optimization
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
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
table user_activity
In my PHP page, I have the following code
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?
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)
+------------------+-------------+------+-----+---------+-------+ | FIELD | Type | NULL | Key | DEFAULT | Extra | +------------------+-------------+------+-----+---------+-------+ | email | VARCHAR(80) | NO | PRI | NULL | | | force_change_pwd | INT(1) | NO | | 0 | | | password | MD5(20) | NO | | NULL | | | creation_date | DATETIME | YES | | NULL | | +------------------+-------------+------+-----+---------+-------+
table user_activity
MySQL Syntax (Toggle Plain Text)
+----------------+--------------+------+-----+---------+----------------+ | FIELD | Type | NULL | Key | DEFAULT | Extra | +----------------+--------------+------+-----+---------+----------------+ | id | INT(11) | NO | PRI | NULL | AUTO_INCREMENT | | event | VARCHAR(30) | NO | MUL | NULL | | | username | VARCHAR(128) | NO | | NULL | | | domain | VARCHAR(128) | NO | MUL | NULL | | | remote_address | VARCHAR(255) | NO | | NULL | | | DATE | DATETIME | NO | MUL | NULL | | | comments | VARCHAR(255) | YES | | | | +----------------+--------------+------+-----+---------+----------------+
In my PHP page, I have the following code
MySQL Syntax (Toggle Plain Text)
$mbox_users = 'SELECT email, creation_date FROM users ORDER BY email ASC'; $result = mysql_query($mbox_users) OR die ("There was a problem with the SQL query: " . mysql_error()); while($row = mysql_fetch_assoc($result)) { $email=$row['email']; $date=$row['creation_date']; if (is_null($date)) { $date = "N/A"; } $mbox_access="SELECT date FROM user_activity WHERE username = '".$row['email']."' ORDER BY date DESC LIMIT 1"; $result_access = mysql_query($mbox_access) OR die ("There was a problem with the SQL query: " . mysql_error()); $last = mysql_fetch_assoc($result_access); 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?
•
•
Join Date: Dec 2007
Posts: 195
Reputation:
Solved Threads: 27
0
#4 Oct 24th, 2009
Yes, it will increase the performance dramatically.
Check this article for more info http://articles.sitepoint.com/articl...ql-application
Check this article for more info http://articles.sitepoint.com/articl...ql-application
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
0
#5 Oct 24th, 2009
•
•
•
•
Yes, it will increase the performance dramatically.
Check this article for more info http://articles.sitepoint.com/articl...ql-application
I was also wondering whether a JOIN query might improve the performance. Any ideas?
![]() |
Similar Threads
- query optimization in sql (C#)
- Indexing and Query optimisation. (MySQL)
- Query on a large table too slow (MySQL)
- Query optimization (PHP)
- query optimization (PHP)
- Infoscripts.com Review (Website Reviews)
Other Threads in the MySQL Forum
- Previous Thread: is mysql better than dbase 4
- Next Thread: My data not appear
Views: 402 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database distinct drupal dui ec2 eliminate email enter enterprise error eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law license licensing linux maintenance managing mariadb micromanage microsoft microsoftexchange mindtouch montywidenius mozilla music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlquery news open-xchange opendatabasealliance opensource operand oracle pdf php priceupdating query referencedesign remove reorderingcolumns results resultset saas search select sharepoint simpledb sourcecode sql statement sugarcrm syntax thunderbird transparency update





