Optimizing a complex mysql query - php

The following query is taking 0.1313 seconds on phpmyadmin. Any way to optimize this to make things faster (say like to get it in 0.00XX seconds)? Index already added at columns that are doing the joinings.
SELECT m.id, m.civ, m.prenom, m.nom, m.sexe, m.depart, m.date_entree, m.date_sortie, m.login_userid, m.login_passwd, a.rank_id, r.rank_m, r.rank_f, d.user_id AS depID, c.nom AS cordo, z.rank
FROM `0_member` AS m
LEFT JOIN `0_area` AS a ON ( m.id = a.user_id
AND a.sec_id =2 )
LEFT JOIN `0_rank` AS r ON r.id = a.rank_id
LEFT JOIN `0_depart` AS d ON ( m.depart = d.depart
AND d.user_sec =2 )
LEFT JOIN `0_area` AS z ON ( d.user_id = z.user_id
AND z.sec_id =2 )
LEFT JOIN `0_member` AS c ON d.user_id = c.id
WHERE z.rank = 'mod'
ORDER BY nom

Your query has a final "WHERE" clause on the value being FOUND in the "Z" alias table with a rank of 'mod', yet your query is all LEFT JOINs indicating you want all members regardless of a possible match on the right side table you are joining to.
Additionally, you are joining downstream to the "z" table by depart and depart to a user ID, then re-joining directly to the '0_area' as A table directly on the user's ID which APPEARS it would be the same as found from the linking to the depart table to the 'z' table anyhow.
That said, and your member joins to depart and then to area...
My SUGGESTION (and I can rewrite the query as such) is to reverse the order of the query putting your Area table FIRST with an index on the "sec_id, rank" being available... I would have the key order based on whichever category had the smaller subset column first... so either SEC_ID, RANK or RANK, SEC_ID.
Then doing simple JOIN (not LEFT JOIN) to the other tables... At a minimum from:
SELECT STRAIGHT_JOIN
m.id,
m.civ,
m.prenom,
m.nom,
m.sexe,
m.depart,
m.date_entree,
m.date_sortie,
m.login_userid,
m.login_passwd,
a.rank_id,
r.rank_m,
r.rank_f,
d.user_id AS depID,
c.nom AS cordo,
z.rank
FROM
`0_area` AS z
JOIN `0_depart` AS d
on z.user_id = d.user_id
and d.user_sec = 2
JOIN `0_member` AS m
on d.depart = m.depart
AND z.user_id = m.id
LEFT JOIN `0_rank` AS r
on z.rank_id = .rid
WHERE
z.sec_id = 2
AND z.rank = 'mod'
ORDER BY
nom
In your original query, you had a join from
member
Links to Area (on member's user ID just to ensure the "sec_id = 2")
Since the new query is exclusively STARTING with the "area" table as "Z" alias, and THAT where clause is explicitly "sec_id = 2" value, you'll never need to backlink again...
Area (only SECID = 2 and rank = mod)
Links to Depart (on the User's ID)
Links to Members by (on the depart ID)

Try moving this statements " a.sec_id =2 ", "d.user_sec =2 ", " z.sec_id =2 " from ON sections to WHERE section like you already did with "z.rank = 'mod'". Like this:
SELECT m.id, m.civ, m.prenom, m.nom, m.sexe, m.depart, m.date_entree, m.date_sortie, m.login_userid, m.login_passwd, a.rank_id, r.rank_m, r.rank_f, d.user_id AS depID, c.nom AS cordo, z.rank
FROM `0_member` AS m
LEFT JOIN `0_area` AS a ON m.id = a.user_id
LEFT JOIN `0_rank` AS r ON r.id = a.rank_id
LEFT JOIN `0_depart` AS d ON m.depart = d.depart
LEFT JOIN `0_area` AS z ON d.user_id = z.user_id
LEFT JOIN `0_member` AS c ON d.user_id = c.id
WHERE z.rank = 'mod'
AND a.sec_id =2
AND d.user_sec =2
AND z.sec_id =2
ORDER BY nom

Related

Left Join with subquery returns Null

I am applying Left Join with Sub query & where clause
It seems fine no syntax error but the columns I am selecting from that sub query always returns me Null. I have executed the same part in my SQL section, it gives me record. Kindly have a look on the query and let me know if any thing is possible or if question is not clear.
SELECT alt.userId, u.name, t.name AS teamName, alt.startDateTime, v.name AS villageName, c.name AS clusterName, startLat, startLong, latlng.lat, latlng.long
FROM activity_log_tim AS alt
JOIN user AS u ON u.userId = alt.userId
JOIN team_members AS tm ON tm.memberId = u.userId
JOIN team AS t ON t.teamId = tm.teamId
JOIN village AS v ON v.villageId = alt.villageId
JOIN cluster_villages AS cv ON cv.villageId = v.villageId
JOIN cluster AS c ON c.clusterId = cv.clusterId
LEFT JOIN (SELECT lat, long,dateTime, scheduleId FROM activity_log_gps LIMIT 1) AS latlng ON latlng.scheduleId = alt.scheduleId
WHERE DATE(alt.startDateTime) = '2015-09-05' AND DATE(alt.endDateTime) = '0000-00-00' GROUP BY alt.userId ORDER BY latlng.dateTime DESC
Well it's supposed to since you are performing a LEFT JOIN which produces NULL if no match occurs. So either you can perform a INNER JOIN or use a COALESCE function like below
SELECT alt.userId, u.name,
t.name AS teamName,
alt.startDateTime,
v.name AS
villageName,
c.name AS clusterName,
startLat,
startLong,
COALESCE(latlng.lat, 23), //Notice the use of COALESCE. If null will return 23 as default
COALESCE(latlng.long, 32)

Attempting to add count function to a complex query PHP MYSQL

I have a complex query consisting of lots of Inner and Left Joins and i am attempting to get the number of record back in a certain table using the COUNT Function of SQL.
I am attempting to get the number of records in the 'held_proposals' table for each proposal using the proposal_id since I'll be outputting the data into a table using a foreach loop.
In essence I would like to show how many students have 'held' a proposal against each proposal.
This was my attempt:
SELECT p.proposal_id, p.proposal_title, p.description, u.user_record_id, u.forename, u.surname, c.course_title, h.*,
GROUP_CONCAT(DISTINCT t.tag_title) AS tags FROM proposal p
LEFT JOIN user u on u.user_record_id = p.user_record_id
LEFT JOIN course_details c on c.course_code = p.course_code
LEFT JOIN record r on r.proposal_id = p.proposal_id
LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
LEFT JOIN tag_details t on t.tag_code = pt.tag_code
LEFT JOIN (
SELECT h.student_record_id, COUNT(*) AS Held
FROM held_proposals h
) H on h.proposal_id = p.proposal_id
WHERE p.source = "Supervisor"
AND (r.status_code not in (3,8) OR r.status_code IS NULL)
GROUP BY p.proposal_id;
the table currently looks like this:
I would like to add the value returned from the 'held_proposals' table for each proposal at the end of the table as another column.
Could any please provide me with some guidance as to how I can achieve this in the SQL query. Thank you in advance.
You need to group the count inside the query which you are joining something as
SELECT
p.proposal_id,
p.proposal_title,
p.description,
u.user_record_id,
u.forename,
u.surname,
c.course_title, coalesce(h.Held,0) as `Held`,
GROUP_CONCAT(DISTINCT t.tag_title) AS tags
FROM proposal p
LEFT JOIN user u on u.user_record_id = p.user_record_id
LEFT JOIN course_details c on c.course_code = p.course_code
LEFT JOIN record r on r.proposal_id = p.proposal_id
LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
LEFT JOIN tag_details t on t.tag_code = pt.tag_code
LEFT JOIN (
SELECT proposal_id, COUNT(*) AS Held
FROM held_proposals group by proposal_id
) h on h.proposal_id = p.proposal_id
WHERE p.source = "Supervisor"
AND (r.status_code not in (3,8) OR r.status_code IS NULL)
GROUP BY p.proposal_id;

Multitable query mysql

I am in process of creating multi variable & multi table search query for Job Portal. Got some success in creating following long query which gives results as required. I need to replace above highlighted red circle delimited values (which i created using GROUP_CONCAT function in following query) with follwoing table description values.
Note:- Delimited values are not in database field. I'am creating these using following query.
My other table structure is as follows:
+---------------------------------------------+
ID Description
+---------------------------------------------+
1 Delhi
2 Mumbai
3 Chennai
4 Kolkata
+----------------------------------------------+
Please help me to guide a way forward for one, rest i'll do it myself.
Select
a.user_id, g.username, a.mobile_no, a.location, a.DOB, a.current_CTC,
a.expected_CTC, a.work_exp, a.job_type, a.designation, a.Company_name,
a.keyskills, a.profile_headline, a.resume_path, a.Notice_period,
DATEDIFF(CURDATE(),Max(b.Login_date_time)) as Last_Login_days,
Max(b.Login_date_time) as Last_Available_on_site,
(Select GROUP_CONCAT(DISTINCT c.Education_ID ORDER BY c.Education_ID
SEPARATOR ', ') user_education from user_education c where a.user_id =
c.User_ID) as Education_ID,
(Select GROUP_CONCAT(DISTINCT d.FA_ID ORDER BY d.FA_ID SEPARATOR ', ') FA
from user_fa d where a.user_id = d.User_ID) as FA_ID,
(Select GROUP_CONCAT(DISTINCT e.Industry_ID ORDER BY e.Industry_ID
SEPARATOR ', ') Industry_ID from user_industry e where a.user_id =
e.User_ID) as Industry_ID,
(Select GROUP_CONCAT(DISTINCT f.Location_ID ORDER BY f.Location_ID
SEPARATOR ', ') Location_ID from user_preferred_locations f where
a.user_id = f.User_ID) as Preferred_Location_ID
from user_details a
left join login_history b on a.user_id = b.user_id
left join users g on a.user_id = g.id
left join user_education c on a.user_id = c.User_ID
left join user_fa d on a.user_id = d.User_ID
left join user_industry e on a.user_id = e.User_ID
left join user_preferred_locations f on a.user_id = f.User_ID
INNER JOIN locations h ON FIND_IN_SET(h.location_id, f.Location_ID)
WHERE MATCH(keyskills) AGAINST('MIS, Analysis' IN BOOLEAN MODE)
And YEAR(CURDATE()) - YEAR(DOB)>= 18 And YEAR(CURDATE()) - YEAR(DOB)<= 45
And work_exp >= 2 And work_exp <= 15
And a.current_CTC>=102 And a.current_CTC<=115
And a.industry IN (10001,10002,10004)
And a.functional_area IN(1001,1002,1003)
And a.location_preferred IN (1,2,3,4,6,7)
And Education_ID IN (1,5,7,8)
And a.job_type Like '%FTP%'
And Notice_period >= 15
And b.Login_date_time>=0
And a.mobile_no is not null
And resume_path is not null
group by a.user_id

mysql join return 0 for one column with other columns intact

I wish to join multiple tables like- Categories, menus, restaurants, reviews, etc.
to return the restaurants that provide the inserted food with their prices.
Everything works except numberOfReviews in reviews table.
If a restaurant has no reviews then output should be 0 for numOfReviews column but other column values should be retrieved i.e. price, name, etc.
With following query I get all fields as null and count(numReviews) as 0:
select r.id
,r.`Name`
,r.`Address`
,r.city
,r.`Rating`
,r.`Latitude`
,a.`AreaName`
,m.`Price`
,count(rv.id)
from `categories` c, `menus` m, `restaurants` r, areas a, reviews rv
where m.`ItemName`="tiramisu"
and c.`restaurant_id`=r.`id`
and m.`category_id`=c.id
and r.`AreaId`=a.`AreaId`
and if I can't match rv.restaurant_id=r.id in where clause(obviously).
Where am I getting wrong? How do I solve this?
edited
select r.id,
r.`Name`,
r.`Address`,
r.city,
r.`Rating`,
r.`Latitude`,
a.`AreaName`,
m.`Price`,
r.`Longitude`,
r.Veg_NonVeg,
count(rv.id)
from restaurants r LEFT JOIN `reviews` rv on rv.`restaurant_id`=r.`id`
inner join `categories` c on c.`restaurant_id` = r.id
inner join `menus` m on m.`category_id` = c.id
inner join `areas` a on a.`AreaId` = r.`AreaId`
where m.`ItemName`="tiramisu"
First of all, don't use this old school syntax for the jointures.
Here is a query that may solve your problem:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,COUNT(RV.id) AS numOfReviews
FROM restaurants R
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
LEFT JOIN reviews RV ON RV.restaurant_id = R.id
WHERE M.ItemName = 'tiramisu'
GROUP BY R.id, R.Name, R.Address, R.city, R.Rating, R.Latitude, R.Longitude, A.AreaName, M.Price, R.Veg_NonVeg
I used explicit INNER JOIN syntax instead of your old school syntax and I modified the jointure with table reviews in order to get the expected result. The GROUP BY clause is required to use the aggregate function COUNT, every rows will be grouped by the enumerated columns (every column except the one used by the function).
Here is another solution that simplify the GROUP BY clause and allow the modification of SELECT statement without having to worry about the fact that every columns need to be part of the GROUP BY clause:
SELECT R.id
,R.Name
,R.Address
,R.city
,R.Rating
,R.Latitude
,R.Longitude
,A.AreaName
,M.Price
,R.Veg_NonVeg
,NR.numOfReviews
FROM restaurants R
INNER JOIN (SELECT R2.id
,COUNT(RV.id) AS numOfReviews
FROM restaurants R2
LEFT OUTER JOIN reviews RV ON RV.restaurant_id = R2.id
GROUP BY R2.id) NR ON NR.id = R.id
INNER JOIN categories C ON C.restaurant_id = R.id
INNER JOIN menus M ON M.category_id = C.id
INNER JOIN areas A ON A.AreaId = R.AreaId
WHERE M.ItemName = 'tiramisu'
As you can see here I added a new jointure on a simple subquery that does the aggregation job in order to provide me the expected number of reviews for each restaurant.
Hope this will help you.

Is there an alternative for MYSQL HAVING

I'm trying to find some records based on the tags they have. In order to find out which tags a record has i add them to the result using a subquery. To find out which results should be returned i added a having statement on the end of the query. But something tells me this is not the best way.
SELECT e.id, e.title, e.text, e.introduction,
UNIX_TIMESTAMP(e.starts_on) AS starts_on,
UNIX_TIMESTAMP(e.ends_on) AS ends_on,
m.id AS meta_id,
m.url,
cm.title AS category_title,
cm.url AS category_url,
CONCAT(
",",
(
SELECT GROUP_CONCAT(t.id)
FROM modules_tags AS mt
JOIN tags AS t ON t.id = mt.tag_id
WHERE mt.other_id = e.id
),","
) AS tags_search
FROM event_posts AS e
INNER JOIN meta AS m ON e.meta_id = m.id
LEFT JOIN event_categories AS c ON e.category_id = c.id
LEFT JOIN meta AS cm ON c.meta_id = cm.id
LEFT JOIN modules_tags AS mt ON mt.other_id = e.id
LEFT JOIN tags AS t ON t.id = mt.tag_id
WHERE 1 HAVING tags_search LIKE '%,5,%' AND tags_search LIKE '%,6,%'
FIND_IN_SET() is the best way to find values in comma separated values in MySQL.

Categories