hsbsitez
01-03-2009, 06:52 PM
The following query takes 30+ seconds to return the result:
$query = "SELECT lo.logID, lo.log_type, lo.log, lo.log_time, lo.user_name, lo.user_id, lo.link_id, lo.cat_id, lo.log_status,
files_links.name as link_name, files_list.name as cat_name
FROM `files_log` lo
LEFT JOIN files_links ON lo.link_id = files_links.autoID
LEFT JOIN files_list ON lo.cat_id = files_list.cat_id
WHERE lo.log_type = 'added' ORDER BY lo.log_time DESC LIMIT 25";
I changed it to following and time was reduced to 13 seconds
$query = "SELECT lo.logID, lo.log_type, lo.log, lo.log_time, lo.user_name, lo.user_id, lo.link_id, lo.cat_id, lo.log_status,
files_links.name as link_name, files_list.name as cat_name,
FROM `files_log` lo, `files_links`, `files_list`
WHERE lo.log_type = 'added' AND
lo.link_id = files_links.autoID AND
lo.cat_id = files_list.cat_id
ORDER BY lo.log_time DESC LIMIT 25";
From the table files_links I get the name of the url for which the log is about, I don't store the name in the log.
From the table files_list I get the name of the category in which the url resides.
The query is meant to get the log rows from the files_log, and use the other table to get the needed information.
My Questions:
1. How come Left Join takes longer?
2. How can I further optimize the second query?
3. Is there something I can do to the tables to improve the query time?
Update:
I was reading Improve Your PHP Programming (http://www.webhostingtalk.com/showthread.php?t=299134) and found my solution. I saw that the person made the fields to be index, and I had not. So I made lo.cat_id and lo.link_id into index, and now my query is running under 1 second. I think that's my answer to question 2 and 3.
I would still like to hear thoughts as to why Left Join was taking longer than using WHERE.
$query = "SELECT lo.logID, lo.log_type, lo.log, lo.log_time, lo.user_name, lo.user_id, lo.link_id, lo.cat_id, lo.log_status,
files_links.name as link_name, files_list.name as cat_name
FROM `files_log` lo
LEFT JOIN files_links ON lo.link_id = files_links.autoID
LEFT JOIN files_list ON lo.cat_id = files_list.cat_id
WHERE lo.log_type = 'added' ORDER BY lo.log_time DESC LIMIT 25";
I changed it to following and time was reduced to 13 seconds
$query = "SELECT lo.logID, lo.log_type, lo.log, lo.log_time, lo.user_name, lo.user_id, lo.link_id, lo.cat_id, lo.log_status,
files_links.name as link_name, files_list.name as cat_name,
FROM `files_log` lo, `files_links`, `files_list`
WHERE lo.log_type = 'added' AND
lo.link_id = files_links.autoID AND
lo.cat_id = files_list.cat_id
ORDER BY lo.log_time DESC LIMIT 25";
From the table files_links I get the name of the url for which the log is about, I don't store the name in the log.
From the table files_list I get the name of the category in which the url resides.
The query is meant to get the log rows from the files_log, and use the other table to get the needed information.
My Questions:
1. How come Left Join takes longer?
2. How can I further optimize the second query?
3. Is there something I can do to the tables to improve the query time?
Update:
I was reading Improve Your PHP Programming (http://www.webhostingtalk.com/showthread.php?t=299134) and found my solution. I saw that the person made the fields to be index, and I had not. So I made lo.cat_id and lo.link_id into index, and now my query is running under 1 second. I think that's my answer to question 2 and 3.
I would still like to hear thoughts as to why Left Join was taking longer than using WHERE.
