简体   繁体   English

如何优化mysql查询

[英]how to optimize the mysql Query

how to increase the performance of this mysql query 如何提高这个mysql查询的性能

SELECT ''                                           AS sharedid,
       hubber_posts.userID                          AS postowner,
       hubber_posts.*,
       ''                                           AS sharedby,
       hubber_posts.userID                          AS userID,
       hubber_posts.posted_date                     AS DATE,
       ''                                           AS sharebyusr,
       ''                                           AS sharebyusrimg,
       Concat_ws(' ', firstname, lastname)          AS fullname,
       username                                     AS postedBy,
       hubber_user.image,
       hubber_user.gender                           AS gender,
       (SELECT accounttype
        FROM   hubber_user_security us
        WHERE  hubber_user.ID = us.userID
               AND hubber_posts.userID = us.userID) AS accounttype,
       ''                                           AS sharebyusrtype
FROM   hubber_posts
       INNER JOIN hubber_user
               ON hubber_posts.userID = hubber_user.ID
WHERE  hubber_posts.status = 1 

My suggestion is to support a join where hubber_posts is the base table and the 2 other tables are joined using nested loops. 我的建议是支持一个join,其中hubber_posts是基表,另外两个表是使用嵌套循环连接的。

  • No need to index hubber_posts for the join. 无需为连接索引hubber_posts。
  • hubber_user.ID should be a PK. hubber_user.ID应该是PK。
  • hubber_user_security.userID should be indexed (and defined as a FK references hubber_user.ID). 应该对hubber_user_security.userID建立索引(并定义为FK引用hubber_user.ID)。

As for the WHERE clause - only if you have relatively few rows where hubber_posts.status = 1 then you should add an index on hubber_posts.status 至于WHERE子句 - 只有当hubber_posts.status = 1的行数相对较少时,才应在hubber_posts.status上添加索引

Ps PS

since the join contain the condition - 由于连接包含条件 -

ON hubber_posts.userID = hubber_user.ID 

There is no need to compare both hubber_posts.userID and hubber_user.ID to us.userID 无需将hubber_posts.userIDhubber_user.IDhubber_user.ID进行us.userID

Your example code has a correlated subquery. 您的示例代码具有相关子查询。 MySQL performs poorly with those, as of late 2016. 截至2016年底,MySQL的表现不佳。

Try converting it to a JOINed table. 尝试将其转换为JOINed表。

   SELECT all that stuff,
          us.accounttype
     FROM   hubber_posts
     JOIN hubber_user ON hubber_posts.userID = hubber_user.ID
     LEFT JOIN hubber_user_security us ON hubber_user.ID = us.userID
    WHERE  hubber_posts.status = 1 

I used LEFT JOIN . 我使用LEFT JOIN Without the LEFT , any rows without a matching entry in that table will be suppressed from the result set. 如果没有LEFT ,则将从结果集中抑制该表中没有匹配条目的任何行。

You query is essentially this: 您的查询基本上是这样的:

SELECT . . .
       (SELECT accounttype
        FROM   hubber_user_security us
        WHERE  u.ID = us.userID AND
               p.userID = us.userID
       ) AS accounttype,
       . . .
FROM hubber_posts p INNER JOIN
     hubber_user u
     ON p.userID = u.ID
WHERE p.status = 1 ;

For this query, the optimal indexes are: 对于此查询,最佳索引是:

  • hubber_posts(status, userId)
  • hubber_user(Id)
  • hubber_user_security(userId)

I would note that the subquery has an extra correlation condition that is not necessary -- the user ids are already equal. 我会注意到子查询有一个额外的相关条件,这是不必要的 - 用户ID已经相等。 And, you run the risk of getting an error if there are multiple account types. 而且,如果有多种帐户类型,则存在收到错误的风险。

You may intend: 你可能打算:

   (SELECT GROUP_CONCAT(accounttype)
    FROM   hubber_user_security us
    WHERE  u.ID = us.userID
   ) as accounttypes,

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

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