简体   繁体   English

这是执行MySQL查询的最佳/有效方式

[英]which is the best / efficient way to execute MySQL query

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 JOIN ( SELECT ... )没有索引使其有效
  • LEFT JOIN ( ... ) begs for evaluating the subquery after the left table, thereby begging for the subquery to be evaluated repeatedly. LEFT JOIN ( ... )乞求在左侧表之后评估子查询,从而请求对子查询进行重复评估。
  • SELECT * (in your subquery) is gathering lots of stuff that is not used. SELECT * (在您的子查询中)正在收集很多未使用的东西。 ( SELECT * in EXISTS does not mean to fetch everything; the * is just a place holder.) EXISTS中的SELECT *并不意味着获取所有内容; *只是一个占位符。)
  • EXISTS evaluates to 1 (true) or 0 (false), which seems to be what you want. EXISTS计算结果为1(真)或0(假),这似乎是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM