duanbimo7212 2017-06-12 15:25
浏览 47
已采纳

什么是“mysqli_query(来自create_at> $ time的表中的SELECT count(*)”的较便宜的替代品

I have a script that I need to run in under 1 second. I have this line that takes 4.4 seconds on average to execute.

    $result = mysqli_query($conn, "SELECT count(*) from record where created_at > '$time'");

The rest of the script takes less than 0.1 second.

  • 写回答

1条回答 默认 最新

  • dpitqpax07626 2017-06-13 09:03
    关注

    I aggree with @xQbert and wonder if COUNT(1) or COUNT(ID) could increase speed... but nevermind...

    Edit - just had the answer Count(*) vs Count(1)

    You should add an index involving created_at row. Try to add a UNIQUE INDEX on created_at if possible or on created_at and another row (i.e. created_at + user_id). The more restrictive you will be, the faster you will get your results.

    Another point is, if you only need 5 results... limit your query with LIMIT 0,5

    Believe me... PHP won't be faster than MySQL! ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序