I am just trying to figure out on how to optimize the below sequence of queries into a single query.
To be specific, below queries are just like alerts to be displayed to user when he logins into his account.
$sq = "SELECT COUNT(*) as totm FROM login as l
JOIN msgs m on m.id = l.id
WHERE m.tstamp > l.mtstamp AND l.id = $id;";
$sq .= "SELECT COUNT(*) as totp FROM info as u
JOIN pst as p on p.cid = u.cid
JOIN login as l on l.id = u.id
WHERE p.tstamp > l.ptstamp AND p.id <> u.id
AND p.type = 0 AND u.id = $id;";
$sq .= "SELECT COUNT(*) as totq FROM info as u
JOIN pst as p on p.cid = u.cid
JOIN login as l on l.id = u.id
WHERE p.tstamp > l.ptstamp AND p.id <> u.id
AND p.type = 1 AND u.id = $id";
Right now Iam using mysqli_multi_query() to run this multiple queries.
However I have managed to cut it down into a single query
$sq = "SELECT m.totm,p.totp FROM login as l
JOIN info as u on u.id = l.id
LEFT JOIN (SELECT tstamp,id,COUNT(*) as 'totm' FROM msgs GROUP BY id) m
ON m.id = l.id AND m.tstamp > l.mtstamp
LEFT JOIN (SELECT tstamp,cid,type,id,COUNT(*) as 'totp' FROM pst
GROUP BY id) p
ON p.cid = u.cid AND p.tstamp > l.ptstamp AND p.type = 0
AND p.id <> l.id WHERE l.id = $id
LEFT JOIN (SELECT tstamp,cid,type,id,COUNT(*) as 'totp' FROM pst
GROUP BY id) p
ON p.cid = u.cid AND p.tstamp > l.ptstamp AND p.type = 1
AND p.id <> l.id WHERE l.id = $id LIMIT 1";
When I have tried the single query with EXPLAIN STATEMENT the query ouput is too bad, many rows are affected.
When I have performed the same on three queries individually, the result was good. I am not sure should I run multiple queries or try single query.
After googling I have found that groupBY causes severe overhead in joins and for many rows. kindly anyone let me know what would be the better way to approach this. Can anyone me help me for writing a more optimized query. Thank you. Any help is greatly appreciated.
If you're trying to retrieve the count for a specific id, then I recommend combining your queries as subqueries in the select. There's no sense in using GROUP BY since you're looking for the count of a specific id.
SELECT
(SELECT COUNT(*) FROM login as l
JOIN msgs m on m.id = l.id
WHERE m.tstamp > l.mtstamp AND l.id = $id) totm,
(SELECT COUNT(*) FROM info as u
JOIN pst as p on p.cid = u.cid
JOIN login as l on l.id = u.id
WHERE p.tstamp > l.ptstamp AND p.id <> u.id
AND p.type = 0 AND u.id = $id) totp,
(SELECT COUNT(*) FROM info as u
JOIN pst as p on p.cid = u.cid
JOIN login as l on l.id = u.id
WHERE p.tstamp > l.ptstamp AND p.id <> u.id
AND p.type = 1 AND u.id = $id) totq
Contrast this to retrieving counts for every id in which case a GROUP BY on id would be useful:
SELECT t1.id,t1.totm,t2.totp,t3.totq
FROM
(SELECT l.id, COUNT(*) as totm FROM login as l
JOIN msgs m on m.id = l.id
WHERE m.tstamp > l.mtstamp
GROUP BY l.id) t1
LEFT JOIN (SELECT u.id, COUNT(*) as totp FROM info as u
JOIN pst as p on p.cid = u.cid
JOIN login as l on l.id = u.id
WHERE p.tstamp > l.ptstamp AND p.id <> u.id
AND p.type = 0
GROUP BY u.id) t2 ON t1.id = t2.id
LEFT JOIN (SELECT u.id, COUNT(*) as totq FROM info as u
JOIN pst as p on p.cid = u.cid
JOIN login as l on l.id = u.id
WHERE p.tstamp > l.ptstamp AND p.id <> u.id
AND p.type = 1
GROUP BY u.id) t3 ON t2.id = t3.id
Related
I have created one SQL sub-query which was running perfectly.
SELECT o.*, u.*,OD.* FROM orders o
LEFT JOIN order_details OD on o.id = OD.order_id
LEFT JOIN users u ON o.user_id = u.id
WHERE o.order_status = 1 AND o.id NOT IN
(SELECT r.order_id FROM order_rejected_details r WHERE r.courier_id =$courierId)
order by o.id desc
How to write above query with NOT in condition in CakePHP?
You can simply write this query in CakePHP as per below.
In model:
$resultData = $this->query('SELECT o.*,
u.*,
OD.*
FROM orders o
LEFT JOIN order_details OD
ON o.id = OD.order_id
LEFT JOIN users u
ON o.user_id = u.id
WHERE o.order_status = 1
AND o.id NOT IN (SELECT r.order_id
FROM order_rejected_details r
WHERE r.courier_id = $courierid)
ORDER BY o.id DESC');
return $resultData;
I have the following join below and I was wanting to know is there a better way to write it as I am getting Unknown table 'id' in MULTI DELETE and I cannot seem to pin point where.
Join:
$query = $dbConnection->prepare('
DELETE c.id, r.id, s.id,f.id,ip.id,ct.id
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id');
$query->execute(array(':campaign_id' => $campaign_id));
Your DELETE statement is incorrect. You should remove the fields.
DELETE
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id'
So I have two MySQL queries that if I had the knowledge to combine I would but I don't so that's why I turned to "SO", and in that case I haven't tried anything because its out of my scope. I want to combine all in one query and if that's not possible please let me know.
Query one "This selects all of your friends posts including yours":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20"
Query two "This selects all of the people your following posts":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20"
I have combined these but with php alone. I'd like to combine both in one single MySQL query. Thanks in advance
SELECT *
FROM
(SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_friendship e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id LIMIT 20
UNION SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_follower e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id LIMIT 20 ) MainQuery
ORDER BY id DESC
SELECT b.*, c.photo, d.name, e.status, "P"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20
UNION
SELECT b.*, c.photo, d.name, e.status, "F"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20
With the extra column you can see where the row is coming from (P = post, F = following)
Keep in mind that union will remove duplicate rows. If you want to see all rows use UNION ALL
Query:
SELECT l.id, l.userid, l.checktime, l.SN, u.name, u.badgenumber
FROM kio_checkinout l
LEFT JOIN userinfo u ON l.userid = u.userid
GROUP BY l.userid DESC
i need show the data first order by id descending and then group by userid (latest user id) value.
i am trying this sql query but i can not get the data as i need to show.
Use ORDER BY clause:
SELECT l.id, l.userid, l.checktime, l.SN, u.name, u.badgenumber
FROM kio_checkinout l
LEFT JOIN userinfo u ON l.userid = u.userid
GROUP BY l.userid
ORDER BY l.id DESC
Try something like this
SELECT l.userid, GROUP_CONCAT(l.checktime,'-',l.checktype), u.name, u.badgenumber
FROM kio_checkinout l
LEFT JOIN userinfo u ON l.userid = u.userid
GROUP BY l.userid
ORDER BY l.id DESC
I have a select statement like this:
SELECT c.id AS courseid, u.id AS userid
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
I have another piece of data I want that does not have a direct relationship with the current tables. I have been calling this function in the output loop,
function getLastCourseAccessByUser($courseid, $userid){
global $DB;
return
$DB->get_record_sql('
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = '.$courseid.' AND userid = '.$userid.'
ORDER BY time DESC
limit 1
');
}
but would rather get all the data once, and not call back the db.
I am trying this:
SELECT c.id AS courseid, u.id as userid
(
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = c.id AND userid = u.id
) AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
Or, could I call the function directly from the sql like this:
SELECT c.id AS courseid, u.id as userid,
'.getLastCourseAccessByUser(c.id,u.id).' AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
Thank you.
SELECT c.id AS courseid, u.id AS userid, FROM_UNIXTIME(t.time, "%m/%d/%Y") as ftime
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
join mdl_log t on t.course = c.id and t.userid = u.id
You can put AND and OR clauses in a join in mysql so you can join on both your conditionals. Though you may want to make it an outer join so that it won't fail the entire query if the conditionals aren't met
Otherwise your sub select option should also work (this one)
SELECT c.id AS courseid, u.id as userid
(
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = c.id AND userid = u.id
) AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
The more data you process on the MySQL Server side, the less traffic is generated between the MySQL Server and your PHP application, thus speeding up the application. I would say to get all the data from the server at once and not go back and forth too many times.
You can also create the getLastCourseAccessByUser function as a stored function in MySQL if you need to re-use it in other applications:
DELIMITER \\
DROP FUNCTION IF EXISTS getLastCourseAccessByUser\\
CREATE FUNCTION getLastCourseAccessByUser (in_course_id INT, in_user_id VARCHAR(50)
RETURNS STRING
BEGIN
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") AS ftime
FROM mdl_log
WHERE course = in_course_id AND userid = in_user_id
ORDER BY time DESC
LIMIT 1
END\\
DELIMITER ;
Then you could use it in your SELECT statement:
SELECT c.id AS courseid,
u.id as userid,
getLastCourseAccessByUser(c.id,u.id) AS lastaccessdate
FROM mdl_user_enrolments ue
JOIN mdl_enrol e on e.id = ue.enrolid
JOIN mdl_user u on u.id = ue.userid
JOIN mdl_course c on c.id = e.courseid