1st approach :-
SELECT
clipComment.commentId,
clipComment.commentedBy,
clipComment.clipId AS commentTypeId,
'clip' AS commentType,
clipComment.commentDescription,
clipComment.commentCreatedDateTime,
clipComment.commentModifiedDateTime,
clipComment.commentLikeCount,
userProfile.userName,
userProfile.firstName,
userProfile.LastName,
userProfile.profilePicUrl,
userProfile.themeForeground,
userProfile.themeBackground,
IF(derCommentLike.commentId = clipComment.commentId,
1,
0) likedByMe
FROM
clipComment
LEFT JOIN
(SELECT
*
FROM
clipCommentLikes
WHERE
commentLikedBy = 16) derCommentLike
ON
derCommentLike.commentId = clipComment.commentId
LEFT JOIN
userProfile
ON
userProfile.userId = clipComment.commentedBy
WHERE
clipComment.clipId = 141
2nd approach :-
SELECT
clipComment.commentId,
clipComment.commentedBy,
clipComment.clipId AS commentTypeId,
'clip' AS commentType,
clipComment.commentDescription,
clipComment.commentCreatedDateTime,
clipComment.commentModifiedDateTime,
clipComment.commentLikeCount,
userProfile.userName,
userProfile.firstName,
userProfile.LastName,
userProfile.profilePicUrl,
userProfile.themeForeground,
userProfile.themeBackground,
IF( derCommentLike.commentId = clipComment.commentId , 1 , 0 ) AS likedByMe
FROM
(SELECT
*
FROM
clipCommentLikes
WHERE
commentLikedBy = 16) derCommentLike
RIGHT OUTER JOIN clipComment
ON derCommentLike.commentId = clipComment.commentId
RIGHT OUTER JOIN userProfile
ON clipComment.commentedBy = userProfile.userId
WHERE
clipComment.clipId = 141
both query returns same result, but just want to know which approach should i follow & which one is more efficient to follow. record set will contain millions of record, so i want to use best way. or i am doing work then please correct me. thank you in advance.
explain statement 1st approach
explain statement 2nd approach
explain statement 1st approach
IF(derCommentLike.commentId = clipComment.commentId, 1, 0) likedByMe
...
LEFT JOIN
(SELECT *
FROM clipCommentLikes
WHERE commentLikedBy = 16
) derCommentLike
ON derCommentLike.commentId = clipComment.commentId
-->
( EXISTS SELECT * FROM clipCommentLikes
WHERE commentId = clipComment.commentId
) AS likedByMe
Explanation:
JOIN ( SELECT ... )
has no index to make it efficient LEFT JOIN ( ... )
begs for evaluating the subquery after the left table, thereby begging for the subquery to be evaluated repeatedly. SELECT *
(in your subquery) is gathering lots of stuff that is not used. ( SELECT *
in EXISTS
does not mean to fetch everything; the *
is just a place holder.) EXISTS
evaluates to 1 (true) or 0 (false), which seems to be what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.