Intelligent MySQL GROUP BY for Activity Streams

前端 未结 4 983
长发绾君心
长发绾君心 2020-12-13 18:52

I\'m building an activity stream for our site, and have made some decent headway with something that works pretty well.

It\'s powered by two tables:

4条回答
  •  爱一瞬间的悲伤
    2020-12-13 19:31

    Some observations about your desired results:

    Some of the items are aggregated (Jack Sprat hearted seven sellers) and others are itemized (Lord Nelson chartered the Golden Hind). You probably need to have a UNION in your query that pulls together these two classes of items from two separate subqueries.

    You use a fairly crude timestamp-nearness function to group your items ... DATE(). You may want to use more sophisticated and tweakable scheme... like this, maybe

      GROUP BY TIMESTAMPDIFF(HOUR,CURRENT_TIME(),stream_date) DIV hourchunk
    

    This will let you group stuff by age chunks. For example if you use 48 for hourchunk you'll group stuff that's 0-48 hours ago together. As you add traffic and action to your system you may want to decrease the hourchunk value.

提交回复
热议问题