dongyue4964 2014-10-05 07:56
浏览 56
已采纳

在MYSQL中指定SUBTIME间隔是否会阻止查询不必要地遍历整个表?

Please tell me, if I specify an interval like such: subtime(now(), INTERVAL 1 day) inside a MySQL SELECT query while having a proper datetime column to use as reference - will this prevent from the query to look through the entire table (over 100,000 records in my case) each time it runs but rather look through records made only the past 24 hours? Is DESC order needed for the datetime table or such? Also, if I have SUM(column) in the query, will it also run only for the interval specified?

Edit: If I just would like to use the above mentioned SUM to sum a column where there only are integers of value "1" - would it be better to simply check how many rows the SELECT query returns with mysql_num_rows - is it more efficient in combination with the time interval setting?

Thank you!

  • 写回答

1条回答 默认 最新

  • dshakcsq64956 2014-10-05 08:15
    关注

    It will in fact prevent MySQL to go through the whole table but not if you just subtime() in the SELECT-part. Instead you have to do something like this:

    SELECT * FROM myTable 
    WHERE myDateCol BETWEEN DATE_SUB(now(), INTERVAL 1 day) AND now()
    

    The query will now select only rows one day old. Add a B-TREE index on myDateCol to speed things up:

    ALTER TABLE myTable ADD INDEX myIdx USING BTREE (myDateCol)
    

    See MySQL doc on that topic

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

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)