<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>DaniWeb IT Discussion Community</title>
		<link>http://www.daniweb.com/forums/</link>
		<description>Tech support, programming, web development, and internet marketing community. Forums to get free computer help and support.</description>
		<language>en-US</language>
		<lastBuildDate>Mon, 30 Nov 2009 15:11:56 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.daniweb.com/alphaimages/misc/rss.jpg</url>
			<title>DaniWeb IT Discussion Community</title>
			<link>http://www.daniweb.com/forums/</link>
		</image>
		<item>
			<title>Query optimization</title>
			<link>http://www.daniweb.com/forums/thread232856.html</link>
			<pubDate>Sat, 24 Oct 2009 10:04:45 GMT</pubDate>
			<description><![CDATA[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* 
  <div class="codeblock"> <div class="spaced"> <div...]]></description>
			<content:encoded><![CDATA[<div>Hi all.<br />
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.<br />
<br />
DB has this structure<br />
<br />
table <span style="font-weight:bold">users</span><br />
 <pre style="margin:20px; line-height:13px">+------------------+-------------+------+-----+---------+-------+<br />
| Field&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Type&nbsp; &nbsp; &nbsp; &nbsp; | Null | Key | Default | Extra |<br />
+------------------+-------------+------+-----+---------+-------+<br />
| email&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | varchar(80) | NO&nbsp;  | PRI | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp;  |<br />
| force_change_pwd | int(1)&nbsp; &nbsp; &nbsp; | NO&nbsp;  |&nbsp; &nbsp;  | 0&nbsp; &nbsp; &nbsp;  |&nbsp; &nbsp; &nbsp;  |<br />
| password&nbsp; &nbsp; &nbsp; &nbsp;  | md5(20)&nbsp; &nbsp;  | NO&nbsp;  |&nbsp; &nbsp;  | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp;  |<br />
| creation_date&nbsp; &nbsp; | datetime&nbsp; &nbsp; | YES&nbsp; |&nbsp; &nbsp;  | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp;  |<br />
+------------------+-------------+------+-----+---------+-------+</pre><br />
table <span style="font-weight:bold">user_activity</span><br />
<br />
 <pre style="margin:20px; line-height:13px">+----------------+--------------+------+-----+---------+----------------+<br />
| Field&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Type&nbsp; &nbsp; &nbsp; &nbsp;  | Null | Key | Default | Extra&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+----------------+--------------+------+-----+---------+----------------+<br />
| id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | int(11)&nbsp; &nbsp; &nbsp; | NO&nbsp;  | PRI | NULL&nbsp; &nbsp; | auto_increment |<br />
| event&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | varchar(30)&nbsp; | NO&nbsp;  | MUL | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| username&nbsp; &nbsp; &nbsp;  | varchar(128) | NO&nbsp;  |&nbsp; &nbsp;  | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| domain&nbsp; &nbsp; &nbsp; &nbsp;  | varchar(128) | NO&nbsp;  | MUL | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| remote_address | varchar(255) | NO&nbsp;  |&nbsp; &nbsp;  | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| date&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  | datetime&nbsp; &nbsp;  | NO&nbsp;  | MUL | NULL&nbsp; &nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
| comments&nbsp; &nbsp; &nbsp;  | varchar(255) | YES&nbsp; |&nbsp; &nbsp;  |&nbsp; &nbsp; &nbsp; &nbsp;  |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+----------------+--------------+------+-----+---------+----------------+</pre><br />
<br />
In my PHP page, I have the following code<br />
<br />
 <pre style="margin:20px; line-height:13px">$mbox_users = 'SELECT email, creation_date FROM users ORDER BY email ASC';<br />
$result = mysql_query($mbox_users) or die (&quot;There was a problem with the SQL query: &quot; . mysql_error());<br />
while($row = mysql_fetch_assoc($result))<br />
{<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; $email=$row['email'];<br />
&nbsp; &nbsp; &nbsp; &nbsp; $date=$row['creation_date'];<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (is_null($date)) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $date = &quot;N/A&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; $mbox_access=&quot;SELECT date FROM user_activity&nbsp; WHERE username = '&quot;.$row['email'].&quot;' ORDER BY date DESC LIMIT 1&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $result_access = mysql_query($mbox_access) or die (&quot;There was a problem with the SQL query: &quot; . mysql_error());<br />
&nbsp; &nbsp; &nbsp; &nbsp; $last = mysql_fetch_assoc($result_access);<br />
&nbsp; &nbsp; &nbsp; &nbsp; echo '&lt;li&gt;'.$email.' (&lt;b&gt;Created: &lt;/b&gt;'.$date.' - &lt;b&gt;Last Access: &lt;/b&gt;'.$last['date'].')&lt;/li&gt;';</pre><br />
<br />
Unfortunately with +20k users and over 100k records in the <span style="font-weight:bold">user_activity</span> table, the queries are awfully slow.<br />
Am I missing some obvious method of making the query faster?</div> ]]></content:encoded>
			<category domain="http://www.daniweb.com/forums/forum126.html">MySQL</category>
			<dc:creator>trashed</dc:creator>
			<guid isPermaLink="true">http://www.daniweb.com/forums/thread232856.html</guid>
		</item>
	</channel>
</rss>
