Use JOINS instead of subquery - mysql

I have a simple query that uses a subquery:
SELECT pictures.*
FROM pictures
WHERE pictures.user_id IN
(SELECT follows.following_id
FROM follows
WHERE follows.follower_id = 9)
ORDER BY created_at DESC LIMIT 5;
I am wondering,
a) How can I remove the sub query and use JOINS instead and b) will there be a performance benefit in using JOINS instead of sub query?
(follows.following_id, follows.follower_id, pictures.user_id are all indexed)
Thanks

SELECT DISTINCT pictures.*
FROM pictures
INNER JOIN follows
ON pictures.user_ID = follows.following_id
WHERE follows.follower_id = 9
ORDER BY pictures.created_at DESC
LIMIT 5
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE
Another way to achieve the same result is by using EXISTS
SELECT *
FROM pictures
WHERE EXISTS
(
SELECT 1
FROM follows
WHERE pictures.user_ID = follows.following_id AND
follows.follower_id = 9
)
ORDER BY pictures.created_at DESC
LIMIT 5

Related

Left join order by

I have property pictures in table with their sort_order starting from 0 to number of pictures.
What I would like to do is select pictures but I would like it to start from 2.
My approach was:
SELECT * FROM property_photos AS pp1
JOIN property_photos AS pp2 ON pp1.p_id = pp2.p_id
where pp2.sort_order =2
and pp2.sort_order <2
and pp1.sort_order >2
and pp1.p_id = 3
So what I am trying to gain here is the sort order would be like 2,0,1,3,4,5,6,7
so I need a self join but my query doesn't work
you don't need a join on this,
SELECT *
FROM property_photos
WHERE p_id = 3
ORDER BY (sort_order = 2) DESC, sort_order

Rewrite MySQL query without using UNION [duplicate]

This question already has answers here:
Get top n records for each group of grouped results
(12 answers)
Get records with highest/smallest <whatever> per group
(2 answers)
Closed 8 years ago.
I have the following query which returns the correct results but I'm sure it's not the best way to get these results...
select * from (
select * from features where feature_area = 0
order by updateStamp desc limit 1
) as feature_1
union all
select * from (
select * from features where feature_area = 1
order by updateStamp desc limit 1
) as feature_2
union all
select * from (
select * from features where feature_area = 2
order by updateStamp desc limit 1
) as feature_3
This returns results which look something like...
id feature_area title updateStamp
--------------------------------------------------------------------
103 0 This is a title  2014-04-15 09:26:14
102 1 Another title 2014-03-27 14:09:49
98 2 More title 2014-01-21 16:00:55
Could this be improved using joins rather than unions and if so could you point me in the right direction please.
EDIT:
Having looked at the other options pointed out by #Ben it would seem I've already got the quickest query (albeit not that attractive) for my particular purpose. Feel free to correct me if you think I'm wrong though. I'm no expert, hence I'm asking for advice.
select f.* from features f
inner join (
select
feature_area
max(updateStamp) as updateStamp
from
features
where feature_are IN (0,1,2)
group by feature_area
) sq on sq.feature_area = f.feature_area
and sq.updateStamp = f.updateStamp
Hope I read your question correctly.
select *
From features f
inner join ( select feature_area, max(updateStamp) as maxUpdateStamp
from features
Group by feature_area
) as minfeatures
ON minfeatures.feature_area = f.feature_area
AND minfeatures.maxUpdateStamp = f.updateStamp
Assuming proper indexes, it's often most performant to solve this with the anti-join:
SELECT f1.*
FROM features f1
LEFT JOIN features f2
ON f2.feature_area = f1.feature_area
AND f2.updateStamp < f1.updateStamp
WHERE f1.feature_area < 3
AND f2.id IS NULL
ORDER BY f1.feature_area
In cases where there are duplicate rows with same feature_area and highest updateStamp, it will return duplicate rows.
For more explanation of this technique:
Get records with highest/smallest <whatever> per group
with MaxFeature
AS
(
select
feature_area AS feature_area
,max(updateStamp) AS MaxUpdateStamp
from
features
group by
feature_area
)
select
Features.*
from
Features
inner join
Maxfeature
on
Features.feature_area = MaxFeature.feature_area
and
Features.updateStamp = MaxFeature.MaxUpdateStamp
order by
Features.feature_area asc

Sql query to combine result of two tables

Currently I am using the following query to display the following result.
SELECT * FROM RouteToGrowthRecord, GradeMaster,MileStoneMaster
WHERE MemberID = 'ALV01L11034A06' AND
RouteToGrowthRecord.GradeID=GradeMaster.GradeID AND
RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
ORDER BY CheckupDate DESC
Now I have another table named RouteToGrowthRecord_st that has same
columns as RouteToGrowthRecord with some additional fields.
I need to display result that are present in both the table. ie . if RouteToGrowthRecord_st has 3 records with the given menberID,then output must contain 3 more records along with the above query result.(fr ex above its 9+3=12 records in total).
You can use Union here to merge the results getting from both queries. Use default values for the unmapped additional fields.
You can write above query in following way
SELECT * FROM RouteToGrowthRecord
INNER JOIN GradeMaster ON RouteToGrowthRecord.GradeID=GradeMaster.GradeID
INNER JOIN MileStoneMaster ON RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
LEFT JOIN RouteToGrowthRecord_st ON RouteToGrowthRecord_st.memberID=RouteToGrowthRecord.memberID
WHERE
RouteToGrowthRecord.MemberID = 'ALV01L11034A06'
order by CheckupDate DESC
this is my answer
SELECT CheckUpDate,AgeInMonths,PresentWeight,Height,Diagnosis,growthstatus,GradeName,MilestoneName,MemberID
FROM RouteToGrowthRecord, GradeMaster,MileStoneMaster WHERE
MemberID = 'ALV01L56107A11 ' and
RouteToGrowthRecord.GradeID=GradeMaster.GradeID and
RouteToGrowthRecord.MileStoneID=MileStoneMaster.MileStoneID
union
SELECT CheckUpDate,AgeInMonths,PresentWeight,Height,Diagnosis,growthstatus,GradeName,MilestoneName,MemberID
FROM RouteToGrowthRecord_st, GradeMaster,MileStoneMaster WHERE
MemberID = 'ALV01L56107A11 ' and
RouteToGrowthRecord_st.GradeID=GradeMaster.GradeID and
RouteToGrowthRecord_st.MileStoneID=MileStoneMaster.MileStoneID
order by CheckupDate DESC
SELECT * FROM RouteToGrowthRecord a inner join GradeMaster b inner
join MileStoneMaster c inner join RouteToGrowthRecord_st d on
a.GradeID=b.GradeID AND a.MileStoneID=c.MileStoneID and
d.GradeID=b.GradeID AND d.MileStoneID=c.MileStoneID
WHERE a.MemberID = 'ALV01L11034A06'
ORDER BY CheckupDate DESC

Slow Execution of MySQL Select Query

I have the following query…
SELECT DISTINCT * FROM
vPAS_Posts_Users
WHERE (post_user_id =:id AND post_type != 4)
AND post_updated >:updated
GROUP BY post_post_id
UNION
SELECT DISTINCT vPAS_Posts_Users.* FROM PAS_Follow
JOIN vPAS_Posts_Users ON
( PAS_Follow.folw_followed_user_id = vPAS_Posts_Users.post_user_id )
WHERE (( PAS_Follow.folw_follower_user_id =:id AND PAS_Follow.folw_deleted = 0 )
OR ( post_type = 4 AND post_passed_on_by = PAS_Follow.folw_follower_user_id
AND post_user_id !=:id ))
AND post_updated >:updated
GROUP BY post_post_id ORDER BY post_posted_date DESC LIMIT :limit
Where :id = 7, :updated = 0.0 and :limit=40 for example
My issue is that the query is taking about a minute to return results. Is there anything in this query that I can do to speed up the result?
I am using RDS
********EDIT*********
I was asked to run the query with an EXPLAIN the result is below
********EDIT**********
View Definitition
CREATE ALGORITHM=UNDEFINED DEFINER=`MySQLUSer`#`%` SQL SECURITY DEFINER VIEW `vPAS_Posts_Users`
AS SELECT
`PAS_User`.`user_user_id` AS `user_user_id`,
`PAS_User`.`user_country` AS `user_country`,
`PAS_User`.`user_city` AS `user_city`,
`PAS_User`.`user_company` AS `user_company`,
`PAS_User`.`user_account_type` AS `user_account_type`,
`PAS_User`.`user_account_premium` AS `user_account_premium`,
`PAS_User`.`user_sign_up_date` AS `user_sign_up_date`,
`PAS_User`.`user_first_name` AS `user_first_name`,
`PAS_User`.`user_last_name` AS `user_last_name`,
`PAS_User`.`user_avatar_url` AS `user_avatar_url`,
`PAS_User`.`user_cover_image_url` AS `user_cover_image_url`,
`PAS_User`.`user_bio` AS `user_bio`,
`PAS_User`.`user_telephone` AS `user_telephone`,
`PAS_User`.`user_dob` AS `user_dob`,
`PAS_User`.`user_sector` AS `user_sector`,
`PAS_User`.`user_job_type` AS `user_job_type`,
`PAS_User`.`user_unique` AS `user_unique`,
`PAS_User`.`user_deleted` AS `user_deleted`,
`PAS_User`.`user_updated` AS `user_updated`,
`PAS_Post`.`post_post_id` AS `post_post_id`,
`PAS_Post`.`post_language_id` AS `post_language_id`,
`PAS_Post`.`post_type` AS `post_type`,
`PAS_Post`.`post_promoted` AS `post_promoted`,
`PAS_Post`.`post_user_id` AS `post_user_id`,
`PAS_Post`.`post_posted_date` AS `post_posted_date`,
`PAS_Post`.`post_latitude` AS `post_latitude`,
`PAS_Post`.`post_longitude` AS `post_longitude`,
`PAS_Post`.`post_location_name` AS `post_location_name`,
`PAS_Post`.`post_text` AS `post_text`,
`PAS_Post`.`post_media_url` AS `post_media_url`,
`PAS_Post`.`post_image_height` AS `post_image_height`,
`PAS_Post`.`post_link` AS `post_link`,
`PAS_Post`.`post_link_title` AS `post_link_title`,
`PAS_Post`.`post_unique` AS `post_unique`,
`PAS_Post`.`post_deleted` AS `post_deleted`,
`PAS_Post`.`post_updated` AS `post_updated`,
`PAS_Post`.`post_original_post_id` AS `post_original_post_id`,
`PAS_Post`.`post_original_type` AS `post_original_type`,
`PAS_Post`.`post_passed_on_by` AS `post_passed_on_by`,
`PAS_Post`.`post_passed_on_caption` AS `post_passed_on_caption`,
`PAS_Post`.`post_passed_on_fullname` AS `post_passed_on_fullname`,
`PAS_Post`.`post_passed_on_avatar_url` AS `post_passed_on_avatar_url`
FROM (`PAS_User` join `PAS_Post` on((`PAS_User`.`user_user_id` = `PAS_Post`.`post_user_id`)));
try this query:
SELECT *
FROM
vPAS_Posts_Users
WHERE
post_user_id =:id
AND post_type != 4
AND post_updated > :updated
UNION
SELECT u.*
FROM vPAS_Posts_Users u
JOIN PAS_Follow f ON f.folw_followed_user_id = u.post_user_id
WHERE
u.post_updated > :updated
AND ( (f.folw_follower_user_id = :id AND f.folw_deleted = 0)
OR (u.post_type = 4 AND u.post_passed_on_by = f.folw_follower_user_id AND u.post_user_id != :id)
)
ORDER BY u.post_posted_date DESC;
LIMIT :limit
Other improvements
Indices:
Be sure you have indices on the following columns:
PAS_User.user_user_id
PAS_Post.post_user_id
PAS_Post.post_type
PAS_Post.post_updated
PAS_Follow.folw_followed_user_id
PAS_Follow.folw_deleted
PAS_Post.post_passed_on_by
After that is done, please 1- check the performance again (SQL_NO_CACHE) and 2- extract another explain plan so we can adjust the query.
EXPLAIN Results
Here are the some suggestions for the query and view first of all using the UNION for the two result sets which might makes your query to work slow instead you can use the UNION ALL
Why i am referring you to use UNION ALL
Reason is both UNION ALL and UNION use temporary table for result generation.The difference in execution speed comes from the fact UNION requires internal temporary table with index (to skip duplicate rows) while UNION ALL will create table without such index.This explains the slight performance improvement when using UNION ALL.
UNION on its own will remove any duplicate records so no need to use the DISTINCT clause, try to only one GROUP BY of the whole result set by subqueries this will also minimize the execution time rather then grouping results in each subquery.
Make sure you have added the right indexes on the columns especially the columns used in the WHERE,ORDER BY, GROUP BY, the data types should be appropriate for each column with respect to the nature of data in it like post_posted_date should be datetime,date with an index also.
Here is the rough idea for the query
SELECT q.* FROM (
SELECT * FROM
vPAS_Posts_Users
WHERE (post_user_id =:id AND post_type != 4)
AND post_updated >:updated
UNION ALL
SELECT vPAS_Posts_Users.* FROM PAS_Follow
JOIN vPAS_Posts_Users ON
( PAS_Follow.folw_followed_user_id = vPAS_Posts_Users.post_user_id
AND vPAS_Posts_Users.post_updated >:updated)
WHERE (( PAS_Follow.folw_follower_user_id =:id AND PAS_Follow.folw_deleted = 0 )
OR ( post_type = 4 AND post_passed_on_by = PAS_Follow.folw_follower_user_id
AND post_user_id !=:id ))
) q
GROUP BY q.post_post_id ORDER BY q.post_posted_date DESC LIMIT :limit
References
Difference Between Union vs. Union All – Optimal Performance Comparison
Optimize Mysql Union
MySQL Performance Blog
From your explain I can see that most of your table don't have any key except for the primary one, I would suggest you to add some extra key on the columns you're going to join, for example on: PAS_Follow.folw_followed_user_id and vPAS_Posts_Users.post_user_id, just this will result in a big performance boost.
Bye,
Gnagno

Improve SQL Query performance with JOIN

I've got the following, slow performing, SQL query:
SELECT *
FROM news_events
WHERE 1 AND (user_id = 2416) OR id IN(SELECT content_id FROM likes WHERE user_id = 2416)
ORDER BY id DESC
LIMIT 0,10
The news_events table has indexes on user_id. And the likes table has an index on user_id.
To try to improve performance I have re-written the query using an INNER JOIN the following way:
SELECT a.*
FROM news_events a
INNER JOIN likes b ON (a.id = b.content_id)
WHERE (a.user_id = 2416) OR (b.user_id = 2416)
ORDER BY a.id DESC
LIMIT 0,10
But performance doesn't improve either. I've run explain on this last query and this is the result:
I appreciate any pointer on what I could do to improve the performance of this query.
SELECT *
FROM
(
SELECT a.*
FROM news_events a
WHERE a.user_id = 2416
UNION
SELECT ne.*
FROM news_events ne
INNER JOIN likes l
ON ne.id=l.contentid
WHERE l.user_id = 2416
)
ORDER BY 1 DESC
LIMIT 0,10
Try this query -
SELECT * FROM news_events ne
LEFT JOIN (SELECT content_id FROM likes WHERE user_id = 2416) l
ON ne.user_id = 2416 OR ne.id = l.content_id
ORDER BY
ne.id DESC
LIMIT
0, 10
These columns should be indexed: news_events.user_id, news_events.id, likes.user_id, likes.content_id.
Your query is quite good enough. Posted queries by mates are also fine. But, if you are having large set of data and you did not rebuild indexes since long then, you need to rebuild indexes on both tables.
It is a standard protocol that db admin need to rebuild all the indexes timely as well as recompile all the objects+packages in the db.
I hope it will help :)
Keep querying!