doujie9252 2013-01-30 15:27
浏览 33

在当前时间之前查询和返回时间较慢

My site is loading very slowly, it's only a single page that is loading slow, and I suspect it is because of a MySQL query being sent to the database. How can I speed it up?

$depquery = "SELECT * FROM phpvms_schedules 
WHERE code = 'FE'
AND locate(dayofweek(convert_tz(now(),'+1:00','+0:00'))-1,".TABLE_PREFIX."schedules.daysofweek)>0
AND phpvms_schedules.enabled = '1'
ORDER BY deptime ASC";

The query is basically working to extract information into a 'Departure Board' table. The table is sorted by deptime, and it displays results from current time onwards. However, I also want it to go back at least 4 records, to 4 flights before current time.

To clarify, let me give you an example:

It's now 15:25. Because of if(($flight->deptime) >= $time) and if($count < 15) and inside a foreach it will display 15 records on and after that time. However, I want to also travel back 4 records, so it finds last 4 records before 15:25. Is this possible?

Basically, I want to search up and down around the current time.

  • 写回答

1条回答 默认 最新

  • duanjianxu4288 2013-01-30 15:38
    关注

    You want a UNION, something like:

    SELECT * phpvms_schedules 
    WHERE code = 'FE' 
    AND locate(dayofweek(convert_tz(now(),'+1:00','+0:00'))-1,
         ".TABLE_PREFIX."schedules.daysofweek)>0
    AND phpvms_schedules.enabled = '1'
    ORDER BY deptime ASC
    LIMIT 15
    UNION ALL
    SELECT * phpvms_schedules 
    WHERE code = 'FE' 
    AND locate(dayofweek(convert_tz(now(),'+1:00','+0:00'))-1,
         ".TABLE_PREFIX."schedules.daysofweek)<0
    AND phpvms_schedules.enabled = '1'
    ORDER BY deptime DESC
    LIMIT 4
    

    Note I didn't guess on your date function because I don't know what it is doing. I just changed the > to a < and the ordering on deptime to DESC. (One of those < > might need to also have an = to not miss a departure right now?)

    Using the LIMIT will do the filtering in the DB as opposed to in code which it sounds like you are doing. This will reduce the overhead of processing this information in code and should make it much quicker. I would also sort the UNION of these results in the DB and avoid doing it in code.

    EDIT

    Just to be completely clear this should handle the sorting in the DB:

    SELECT tbl* FROM (
    SELECT * phpvms_schedules 
    WHERE code = 'FE' 
    AND locate(dayofweek(convert_tz(now(),'+1:00','+0:00'))-1,
         ".TABLE_PREFIX."schedules.daysofweek)>0
    AND phpvms_schedules.enabled = '1'
    ORDER BY deptime ASC
    LIMIT 15
    UNION ALL
    SELECT * phpvms_schedules 
    WHERE code = 'FE' 
    AND locate(dayofweek(convert_tz(now(),'+1:00','+0:00'))-1,
         ".TABLE_PREFIX."schedules.daysofweek)<0
    AND phpvms_schedules.enabled = '1'
    ORDER BY deptime DESC
    LIMIT 4
    ) as tbl ORDER BY tbl.deptime ASC
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法