dongyingla8668 2016-07-02 19:33
浏览 24

从过去n小时中选择观看次数最多的帖子的最佳方法

I'm using PHP and MYSQL(innodb engine).

As MYSQL reference says, selecting with comparison of one column and ordering by another can't use our considered index.

I have a table named News.

This table has at least 1 million records with two important columns: time_added and number_of_views.

I need to select most viewed records from last n hours. What is the best index to do this? Or is it possible to run this kind of queries very fast for a table with millions of records?

I've already done this for "last day", meaning I can select most viewed records from last day by adding a new column (date_added). But if I decide to select these records from last week, I'm in trouble again.

  • 写回答

3条回答 默认 最新

  • duangong0690 2016-07-02 19:41
    关注

    First, write the query:

    select n.*
    from news n
    where time_added >= date_sub(now(), interval <n> hours)
    order by number_of_views desc
    limit ??;
    

    The best index is (time_added, number_of_views). Actually, number_of_views won't be used for the full query, but I would include it for other possible queries.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大