SELECT *
FROM members
WHERE memberid IN (SELECT follows.followingid
FROM follows
WHERE follows.memberid = '$memberid'
AND follows.followingid NOT IN (SELECT memberid
FROM userblock))
AND memberid NOT IN (SELECT blockmemberid
FROM userblock
WHERE memberid = '$memberid')
The query above is taking nearly 4 seconds to execute in MySQL and I want to know if anyone has any suggestions on how I might improve/optimize it to achieve a faster execution time?
Replace the in clauses with joins. I think the following captures the logic. Note that the not in turns into a left join with a condition in the where clause finding a non-match
SELECT m.*
FROM members m
follow f
on m.memberid = f.followingid and
f.memberid = $memberid left join
userblock ubf
on follows.followingid = ubf.memberid left join
userblock ub
on m.memberid = ub.blockmemberid and
ub.memberid = '$memberid'
where ub.blockmemberid is null and
ubf.memberid is null;
This looks similar but you have less nested queries.
SELECT *
FROM members m
WHERE EXIST (SELECT f.followingid
FROM follows f
WHERE f.memberid = '$memberid'
AND f.followingid = m.memberid)
AND NOT EXIST (SELECT u.blockmemberid
FROM userblock u
WHERE (m.memberid = '$memberid'
AND u.blockmemberid = m.memberid)
OR
(u.blockmemberid = m.memberid
AND u.memberid = m.memberid) )
This is the logic I reversed-engineered from your code without seeing tables.
SELECT m.*
FROM members m
INNER JOIN follows f ON f.followingid = m.memberid AND
f.memberid = '$memberid'
LEFT OUTER JOIN userblock ub1 ON f.followingid = ub1.memberid
LEFT OUTER JOIN userblock ub2 ON m.memberid = ub2.blockmemberid AND
ub2.memberid = '$memberid'
WHERE ub1.memberid IS NULL AND ub2.blockmemberid IS NULL
Related
I have a list of clients. Each client can have several activities (0..*). Each activity contains a status `is_completed` which is a Boolean (True/False).
I need to retrieve the list of clients that have all activities completed:
if a client has all its activities completed, I keep him.
if a client has not all its activities completes, I ignore him.
I wrote an SQL query that does the job but I am not convinced that it is optimized:
SELECT DISTINCT cc.client_id
FROM clients_clientactivity AS cc
LEFT JOIN clients_client AS c ON (c.id = cc.client_id)
WHERE c.client_type_id = 2
AND (
SELECT COUNT(cc1.id) FROM clients_clientactivity AS cc1 WHERE cc1.client_id = cc.client_id
) = (
SELECT COUNT(cc2.id) FROM clients_clientactivity AS cc2 WHERE cc2.is_completed = True AND cc2.client_id = cc.client_id
);
How can I improve it ?
Thank you for your help.
You could use a not in select for the not true
SELECT DISTINCT cc.client_id
FROM clients_clientactivity AS cc
LEFT JOIN clients_client AS c ON (c.id = cc.client_id)
WHERE c.client_type_id = 2
AND cc.client_id NOT IN (
SELECT cc2.client_id
FROM clients_clientactivity AS cc2
WHERE cc2.is_completed != True
)
I would use aggregation and having:
SELECT c.id
FROM clients_clientactivity ca JOIN
clients_client c
ON c.id = ca.client_id
WHERE c.client_type_id = 2
GROUP BY c.id
HAVING COUNT(*) = SUM(ca.iscompleted)
Your WHERE clause converts the LEFT JOIN to an INNER JOIN, so I removed the LEFT JOIN.
Let's simplify even further:
SELECT client_id
FROM clients_clientactivity
WHERE MIN(is_completed) = TRUE
GROUP BY client_id
(TRUE==1, FALSE==0)
Subqueries are often slow. NOT IN ( SELECT ... ) is really bad (unless the optimizer has magically gotten smarter).
You did not explain how client_type_id = 2, but maybe something like:
clients_client
SELECT a.client_id
FROM clients_client AS c
JOIN clients_clientactivity AS a ON (c.id = a.client_id)
WHERE MIN(a.is_completed) = TRUE
AND c.client_type_id = 2
GROUP BY a.client_id
If performance is a problem, then:
c needs INDEX(client_type_id, id)
a needs INDEX(client_id, is_completed)
Why the sql code below ignores
WHERE r.tt_schedules_id = '105'
in
SELECT DISTINCT r.load_date, u.url
FROM tt_results r
INNER JOIN url_lp u
ON u.lp_id <> r.lp_id
INNER JOIN tt_schedules s
ON s.tt_schedules_id = r.tt_schedules_id
WHERE r.tt_schedules_id = '105'
AND (DATE(NOW()) - DATE(r.load_date) <= 7)
?
Basically, it returns everything with any
tt_schedules_id
There are 5 tables:
url_lp, tt_results (table having foreign keys in url_lp, tt_schedules and
tt_tags), tt_schedules, tt_tags(not used) and tt_schedules_url_lp_hub
If you draw the scheme on the paper you should be clear.
I fixed it:
SELECT u.url FROM url_lp u
INNER JOIN tt_schedules_url_lp_hub hub
ON hub.lp_id = u.lp_id
INNER JOIN tt_schedules s
ON hub.tt_schedules_id = s.tt_schedules_id
WHERE s.tt_schedules_id = '105'
AND u.lp_id NOT IN
(SELECT r.lp_id FROM tt_results r WHERE r.tt_schedules_id = '105')
Cheers.
I have the following query and would like to convert it to using a left outer join instead of a not in to see if it would run faster that way. It's currently taking this query about 40 seconds to run on our database. I'm not familiar enough with using outer joins for this type of thing to convert it myself.
select
c.contact_id as contact_id,
c.orgid as organization_id,
c.first_name as first_name,
c.last_name as last_name,
a.address_state as state
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
where a.address_state = 'OH'
and (c.orgid = 45 or c.orgid = 55)
and c.contact_id NOT IN (
select pc.contact_id
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
inner join cnx_contact_group_participant as gp on c.contact_id = gp.contact_id
inner join cnx_contact_participant_role as cr on gp.participant_role_uid = cr.participant_role_uid
inner join cnx_contact_group as cg on gp.group_uid = cg.group_uid
inner join cnx_contact_group_participant as pgp on cg.primary_participant_uid = pgp.participant_uid
inner join cnx_contact as pc on pgp.contact_id = pc.contact_id
where (c.orgid = 45 or c.orgid = 55)
and cr.name = 'Applicant'
);
select
c.columns
from cnx_contact as c
inner join cnx_address as a on c.home_address_uid = a.address_uid
LEFT JOIN
(Subquery goes here) x
ON x.contact _id = c.contact_id
where a.participant_state = 'OH'
and c.orgid IN(45,55)
and x.contact_id IS NULL;
I have the SQL to display ALL the activities and relative Admin permissions (if any) for that activity.
Current SQL Code:
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
WHERE `activities`.`active` = 1
Returning:
id | name | description | active | admin_role_id (or null)
I then need to detect whether they are an active member within that Activity.
I have the following SQL code:
SELECT DISTINCT `products`.`activity_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `activities` ON `activities`.`id` = `products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Is there any way to merge this into one SQL query. I can't figure out how to use the correct JOIN queries, because of the complexity of the JOINs.
Help please, thanks! :)
Try like this
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
JOIN (`transactions_items`
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`)
ON `activities`.`id`=`products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Seems to me that a query like this would be marginally more comprehensible and (I think) adhere more closely to the spec...
SELECT c.*
, d.admin_role_id
FROM activities c
LEFT
JOIN admins d
ON d.activity_id = c.id
AND d.member_id = 27500
LEFT
JOIN products p
ON p.activity_ID = c.id
LEFT
JOIN transactions_items ti
ON ti.product_id = p.id
LEFT
JOIN transactions t
ON t.id = ti.id
AND t.member_id = 27500
WHERE c.active = 1
I've got the query below that's pulling data from a number of tables to create an update:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
It works fine but when there are plenty of records in papr_down then it takes ages to process. Any ideas about how it can be optimized?
What I think is the join between the emailAddress is the issue here, you can try out with the join with the Id's.
If you provide us the screen shot of the below query ::
EXPLAIN Select * from
en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down pd1
INNER JOIN email.papr_data pd2 on pd1.paper_id = pd2.id
INNER JOIN email.papr_subj ps on ps.id = pd2.subject
INNER JOIN email.papr_exam pe on pe.id = pd2.exam
INNER JOIN email.papr_levl pl on pl.id = pd2.level
WHERE pd2.exam = 1
and pd2.level = 4
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
WHERE sd.fieldid = 33
As I know we should use joins only for the columns which are preset in SELECT clause and for other joins we should implement using WHERE clause
Please try following query:
UPDATE en_inter.subscribers_data AS sd
inner join en_inter.list_subscribers AS ls
on sd.subscriberid = ls.subscriberid
LEFT JOIN (
SELECT pd1.email_address,COUNT(pd1.email_address) AS NumDowns
FROM email.papr_down AS pd1
INNER JOIN email.papr_data AS pd2 on pd1.paper_id = pd2.id
WHERE
email.papr_exam.id in (select exam from email.papr_data where exam = 1)
AND
email.papr_levl.id in (select level from email.papr_data where level = 4 )
AND
email.papr_subj.id in (select subject from email.papr_data)
GROUP BY email_address
) AS downs ON downs.email_address = ls.emailaddress
SET sd.data = ifnull(downs.NumDowns,1)
WHERE sd.fieldid = 33;
I can not execute this at my machine since i don't have the schema