简体   繁体   English

如何优化我当前的Mysql查询

[英]How to optimize my current Mysql query

I have written a query, which works correctly. 我写了一个查询,可以正常工作。 But it takes more time to execute. 但是执行需要花费更多时间。 I can't understand how can I optimize my current query. 我不明白如何优化当前查询。

Here will be thousand or million data. 这里将有成千上万的数据。 Like dislike table every user per like/dislike a new row inserted. 像不喜欢表一样,每个用户喜欢/不喜欢插入新行。

My query: 我的查询:

  select i.id,i.category,i.url,i.upload_by,i.upload_date,
(select count(*) from `like_dislike` where `ulike`='1' and `imageid`=i.id) AS 'allLike',
(select count(*) from `like_dislike` where `ulike`='1' and `imageid`=i.id and i.category !='4' and FROM_UNIXTIME(performdate)>= NOW() - INTERVAL '1' DAY) AS 'daytotalLike',

(select count(*) from `like_dislike` where `ulike`='1' and `imageid`=i.id and i.category !='4'  and FROM_UNIXTIME(performdate)>= NOW() - INTERVAL '7' DAY) AS '7daytotalLike',
(select count(*) from `like_dislike` where `ulike`='1' and `imageid`=i.id and i.category !='4'  and FROM_UNIXTIME(performdate)>= NOW() - INTERVAL '30' DAY) AS '30daytotalLike',
(select count(*) from `like_dislike` where `ulike`='1' and `imageid`=i.id and i.category !='4'  and FROM_UNIXTIME(performdate)>= NOW() - INTERVAL '90' DAY) AS '90daytotalLike',
i.status from `image` as i, `like_dislike` as  likeDislike
WHERE i.status='approved' and i.id=likeDislike.imageid and FROM_UNIXTIME(likeDislike.performdate)>= NOW() - INTERVAL '90' DAY
    GROUP BY i.id ORDER BY '90daytotalLike' DESC Limit 50

sqlfiddle Demo sqlfiddle演示

The answer is simple: You try to do too much in one query. 答案很简单:您尝试在一个查询中做太多事情。 As far as I can see you want to know for all users, which 50 users had the highest scores on many variables. 据我所知,您想了解所有用户,其中50个用户在许多变量中得分最高。 Yeah, sorry, it's a bit unclear what you're trying to achieve here. 是的,很抱歉,您在这里想要实现的目标还不清楚。

So, get rid of all the subqueries, and leave one main query. 因此,摆脱所有子查询,并留下一个主查询。 Something like: 就像是:

SELECT 
  count(*) as 90daytotalLike,
  image.id,
  image.category,
  image.url,
  image.upload_by,
  image.upload_date,
  image.status 
FROM 
  image, 
  like_dislike
WHERE 
  like_dislike.ulike = 1 AND
  image.category != 4  AND
  image.status = 'approved' AND 
  image.id = like_dislike.imageid AND 
  FROM_UNIXTIME(like_dislike.performdate) >= NOW() - INTERVAL '90' DAY
GROUP BY image.id 
ORDER BY '90daytotalLike' DESC Limit 50

And if you need to know data for any of the other periods, run a seperate query. 如果您需要了解其他任何时期的数据,请运行单独的查询。

Another method to lower the load on your database is to store the total likes in the database with your user. 降低数据库负载的另一种方法是与用户一起在数据库中存储喜欢的总数。 It's just a simple counter, nothing fancy, but it will safe you having to summate the likes all the time. 它只是一个简单的计数器,没什么花哨的,但是它将使您不必时刻累加喜欢就可以保证。

You can have counters for periods as well, simply update each counter when a vote expires. 您也可以有一段时间的计数器,只需在投票到期时更新每个计数器即可。

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

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