简体   繁体   English

mysql联接优化大查询

[英]mysql join optimization for big query

i have big query like this and i can't totally rebuild application because of customer: 我有这样的大查询,由于客户原因,我无法完全重建应用程序:

SELECT count(AdvertLog.id) as count,AdvertLog.advert,AdvertLog.ut_fut_tstamp_dmy as day,
          AdvertLog.operation,
          Advert.allow_clicks,
          Advert.slogan as name, 
          AdvertLog.log,
                        (User.tx_reality_credit
            +-20
            -(SELECT COUNT(advert_log.id) FROM advert_log WHERE ut_fut_tstamp_dmy <= day AND operation = 0 AND advert IN (168))
            +(SELECT IF(ISNULL(SUM(log)),0,SUM(log)) FROM advert_log WHERE ut_fut_tstamp_dmy <= day AND operation IN (1, 2) AND advert = 40341 )) AS points 
FROM `advert_log` AS AdvertLog
LEFT JOIN `tx_reality_advert` Advert ON Advert.uid = AdvertLog.advert
LEFT JOIN `fe_users` AS User ON (User.uid = Advert.user or User.uid = AdvertLog.advert)
WHERE User.uid = 40341 and AdvertLog.id>0
GROUP BY AdvertLog.ut_fut_tstamp_dmy, AdvertLog.advert
ORDER BY AdvertLog.ut_fut_tstamp_dmy_12 DESC,AdvertLog.operation,count DESC,name
LIMIT 0, 15

It takes 1.5s approximately which is too long. 大约需要1.5秒,这太长了。

Indexes: 索引:

User.uid
AdvertLog.advert
AdvertLog.operation
AdvertLog.advert
AdvertLog.ut_fut_tstamp_dmy
AdvertLog.id
Advert.user
AdvertLog.log

Output of Explain: 解释输出:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1       PRIMARY User    const    PRIMARY    PRIMARY 4       const 1     Using temporary; Using filesort
1       PRIMARY AdvertLog range  PRIMARY,advert PRIMARY 4   NULL 21427  Using where
1       PRIMARY Advert  eq_ref   PRIMARY    PRIMARY 4   etrend.AdvertLog.advert 1   Using where
3   DEPENDENT SUBQUERY  advert_log  ref ut_fut_tstamp_dmy,operation,advert  advert  5   const   1   Using where
2   DEPENDENT SUBQUERY  advert_log  index_merge ut_fut_tstamp_dmy,operation,advert  advert,operation    5,2 NULL    222 Using intersect(advert,operation); Using where

Can anyone help me, because i tried different things but no improvements 谁能帮我,因为我尝试了不同的方法但没有任何改善

The query is pretty large, and I'd expect this to take a fair bit of time, but you could try adding an index on Advert.uid, if it's not present. 该查询非常大,我希望这会花费一些时间,但是如果不存在,可以尝试在Advert.uid上添加索引。 Other than that, someone with much better SQL-foo than I will have to answer this. 除此之外,SQL-foo比我好得多的人将不得不回答这个问题。

First, your WHERE clause is based on a specific "User.ID", yet there is an index on the Advert_Log by the Advert (user ID). 首先,您的WHERE子句基于特定的“ User.ID”,但在Advert_Log上按广告(用户ID)有一个索引。 So, first, change the WHERE clause to reflect this... 因此,首先,更改WHERE子句以反映这一点...

Where 
   AdverLog.Advert = 40341

Then, remove the "LEFT JOIN" to just a "JOIN" to the user table. 然后,删除“ LEFT JOIN”,仅将“ JOIN”添加到用户表。

Finally (without a full rewrite of the query), I would tack on the "STRAIGHT_JOIN" keyword... 最后(无需完全重写查询),我将使用“ STRAIGHT_JOIN”关键字...

select STRAIGHT_JOIN
    ... rest of query ...

Which tells the optimizer to perform the query in the order / relations explicitly stated. 告诉优化器以明确说明的顺序/关系执行查询。

Another area to optimize would be to pre-query the "points" (counts and logs based on advert and operation) once and pull the answer from that (as a subquery) instead of it running through two queries)... but I'd be interested to know impact of above WHERE, JOIN and STRAIGHT_JOIN helps. 要优化的另一个领域是预先查询“点”(基于广告和操作的计数和日志)一次,然后从中获取答案(作为子查询),而不是通过两个查询来运行)...但是我d有兴趣了解以上WHERE,JOIN和STRAIGHT_JOIN帮助的影响。

Additionally, looking at the join to the user table based on EITHER The Advert_Log.Advert (userID), or the TX_Reality_Credit.User (another user ID which does not appear to be the same since the join between Advert_Log and TX_Reality_Credit (TRC) is based on the TRC.UID) unless that is an incorrect assumption. 此外,基于EITHER,查看到用户表的联接Advert_Log.Advert(userID)或TX_Reality_Credit.User(另一个用户ID,该ID似乎不相同,因为Advert_Log和TX_Reality_Credit(TRC)之间的联接是基于,除非是错误的假设。 This could possibly give erroneous results as you are testing for MULTIPLE User IDs... the advert user, and whoever else is the "user" from the "TRC" table... which would result in which user's credit is being applied to the "points" calculation. 当您测试多个用户ID时,这可能会给出错误的结果...广告用户,以及“ TRC”表中的“用户”是谁……这将导致用户信用被应用于“积分”计算。

To better understand the relationship and context, can you give some more clarification of what is IN these tables from the Advert_Log to TX_Reality_Credit perspective, and the Advert vs UID vs User... 为了更好地理解关系和上下文,您是否可以从Advert_Log到TX_Reality_Credit透视图,以及Advert vs UID vs User,进一步澄清这些表中的内容,...

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

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