Microsoft
Software
Hardware
Network
Question : How do I optimize these MySQL Queries
I use PHP & MySQL in my web application:
1. What would be a free tool to help me find out bad queries
2. My main question: Here are the queries I use and I want to know how to optimize them:
1. SELECT od.id AS orgdetailsid,od.department
id,od.team
leaduserid
,
(SELECT u.ufname AS orgfname FROM users u WHERE u.uid=od.orgid) AS orgfname,
(SELECT u.ulname AS orglname FROM users u WHERE u.uid=od.orgid) AS orglname,
u.ufname AS teamleadfname,u.ulname AS teamleadlname,u.uemail,dd.
department
_name
FROM organization_details od, department_details dd, users u WHERE od.status='Y'
AND dd.status='Y' AND od.teamleaduserid=u.uid AND dd.id=od.departmentid ORDER BY department_name;
2. SELECT profile_of,(SELECT department_name FROM department_details WHERE id=profile_of) AS Department_Name,
COUNT(*) ViewCount FROM view_profile_count WHERE STATUS='Y' AND profile_type="dept_profile
"
GROUP BY profile_of ORDER BY ViewCount DESC;
3. SELECT u.ufname AS ufname, u.ulname AS ulname, u.uid, u.ualias, u.uemail, u.upPhone, u.umobileno, u.location, DATE_FORMAT (STR_TO_DATE(u.GoLive, '%m/%d/%Y'), '%M %D' ) AS GoLive, DATE_FORMAT (STR_TO_DATE(u.GoLive, '%m/%d/%Y'), '%m/%d' ) AS Go_Live_Format, dd.id AS department_id, dd.department_name, dd.department_email, od.teamleaduserid, tl.ufname AS teamlead_fname, tl.ulname AS teamlead_lname, um.organization_details_id
, um.id AS users_master_id, um.user_role AS users_master_role, um.backup_user1, um.backup_user2, um.backup_user3, um.backup_user4, um.backup_user5, um.backup_area1,um.backup_
area2,um.b
ackup_area
3,um.backu
p_area4,um
.backup_ar
ea5 FROM department_details dd, users tl, users u, organization_details od, users_master um WHERE um.organization_details_id
= od.id AND od.departmentid = dd.id AND um.userid = u.uid AND tl.uid = od.teamleaduserid AND u.uid = $emp_id";
4. SELECT u.ufname AS ufname, u.ulname AS ulname, u.ualias, u.uid, u.location, DATE_FORMAT (STR_TO_DATE(u.GoLive, '%m/%d/%Y'), '%m' ) AS Birth_Month, DATE_FORMAT (STR_TO_DATE(u.GoLive, '%m/%d/%Y'), '%d' ) AS Birth_Day FROM users u WHERE u.uid = $emp_id LIMIT 1;
5. SELECT uid,ufname,ulname,uemail, DATE_FORMAT(STR_TO_DATE(Go
Live, '%m/%d/%Y'),'%b %d') AS ptDob, DATE_ADD(CURDATE(), INTERVAL 10 DAY), (DAYOFYEAR(DATE_ADD(CURDAT
E(), INTERVAL 10 DAY))-DAYOFYEAR(STR_TO_DAT
E(GoLive, '%m/%d/%Y'))) FROM users WHERE delFlag='0' AND UserType IN (2) AND (DAYOFYEAR(DATE_ADD(CURDAT
E(), INTERVAL 10 DAY))-DAYOFYEAR(STR_TO_DAT
E(GoLive, '%m/%d/%Y'))) BETWEEN 1 AND 10 ORDER BY (DAYOFYEAR(DATE_ADD(CURDAT
E(), INTERVAL 10 DAY))-DAYOFYEAR(STR_TO_DAT
E(GoLive, '%m/%d/%Y'))) DESC
6. SELECT u.uid AS EmployeeId, u.ufname AS FirstName, u.ulname AS LastName, u.upPhone AS DeskPhone, u.umobileno AS MobilePhone, u.uemail AS Email, u.location AS Location, ud.communication_mode AS CommunicationMode, ud.sparkid AS SparkId, ud.secondary_email AS SecondaryEmail, dd.department_name AS DepartmentName, um.user_role AS UserRole FROM users_master um, department_details dd, organization_details od, users u LEFT JOIN users_details ud ON ud.userid=u.uid WHERE u.delFlag='0' AND u.UserType ='2' AND u.status=0 AND u.uid=um.userid AND um.organization_details_id
=od.id AND dd.id=od.departmentid ORDER BY u.ufname;
Answer : How do I optimize these MySQL Queries
You can use explain to get the execution plan for your query.
Do you use phpmyadmin, it might be helpful in retrieving the status history as well as several parameters from the mysql server and provides some suggestions on how to improve performance by adjusting some parameters?
In query 1 and 2 a join might be better.
i.e. select u.ulname AS orglname , u.ufname as orgfname, od.id as detailsid ... from .. join user u on (u.uid=od.orgid)
Without the definition of the table i.e. what indexes you have for each table, etc. It is hard to analyze a query to suggest improvements.
i.e. if your tables have no primary keys nor indexes, adding indicies would speed up the process.
Using the unixtime stamp for the date fields versus the string may also speed up queries/processing i.e. you would not need to convert string to date and then process/calculate.
10 days are 864000 seconds. such that adding 864000 to a unixdate column and then converting the output to string in the php code during the display will be a better use of resources as compared to having your display alterations done on the my sql server given that you will still be doing some processing on the php side during the display i.e. laying out the data from the query in a particular format as compared to just dumping out the results as plain text.
Random Solutions
Command:Click:Command is working fine for Button but its not working for Hyperlink
how to build a query to display -- to a logged in user -- a list of other users with UserRoleID = 5 -- who ALSO live in the same state as the logged in user?
Remote access VPN via Thompson TG585 v7
Autocomplete
Can't Install Microsoft Entourage 2008 for Mac, Web Services Edition
"Invalid Partition Table" - What are my options?
FileInputStream check if a file is in use
VB.Net get Excel columns itto an array
Can't access variable in code behind page
Outlook/Exchange Issue with Emails not showing