duanboxue3422 2017-02-02 12:51
浏览 37

“分组”的另一种方法是避免低性能速度?

I want to group by b.hash however it is too costly to do it. Is there any way to have this query but also with group by but faster?

SELECT STRAIGHT_JOIN b.hash, b.page, n.favorite
FROM behaviour b, new_table n
WHERE b.timestamp >= NOW( ) - INTERVAL 20 SECOND 
AND b.hash = n.hash

This is the EXPLAIN

enter image description here

PROFILING

enter image description here

  • 写回答

2条回答 默认 最新

  • dougang1605 2017-02-02 14:03
    关注

    I'm guessing you want this query

    SELECT b.hash, b.page, n.favorite
      FROM behaviour b
      JOIN new_table n ON b.hash = n.hash
     WHERE b.timestamp >= NOW( ) - INTERVAL 20 SECOND 
       AND b.hash = n.hash
     ORDER BY b.hash
    

    I don't understand your remark about GROUP BY in your question. It looks like you want the most recent third of a minute's worth of items.

    At any rate, if you create a compound index on your behaviour table containing the following columns, your query will probably start running acceptably fast.

     (timestamp, hash, page)
    

    Why? MySQL's query planner can random-access the index to start at the timestamp value in the query. That's O(log n) quick. Then it can scan the index sequentially for the information needed by your query. That's almost instantaneous.

    Is it possible you want this?

    SELECT b.hash, b.page, MAX(n.favorite)
      FROM behaviour b
      JOIN new_table n ON b.hash = n.hash
     WHERE b.timestamp >= NOW( ) - INTERVAL 20 SECOND 
       AND b.hash = n.hash
     GROUP BY b.hash, b.page
    

    Or this?

    SELECT b.hash, b.page, GROUP_CONCAT(n.favorite)
      FROM ...
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能