Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用日期范围优化MySQL查询_Mysql - Fatal编程技术网

使用日期范围优化MySQL查询

使用日期范围优化MySQL查询,mysql,Mysql,谢谢你的阅读。我有一个预订系统,我正在尝试搜索可用性,但查询需要花费大量时间运行。有人对我如何让它运行得更快有什么建议吗 日期表列出了从2015年到2030年的每个日期。 property_bands表是属性可用日期的日期范围 “预订”属性表是所有预订的列表 当我在不使用booking_properties表的情况下运行下面的查询时,查询需要0.17秒,而使用left join则需要54秒 SET @start = '2018-05-01' , @end = '2018-05-04'; sel

谢谢你的阅读。我有一个预订系统,我正在尝试搜索可用性,但查询需要花费大量时间运行。有人对我如何让它运行得更快有什么建议吗

日期表列出了从2015年到2030年的每个日期。 property_bands表是属性可用日期的日期范围 “预订”属性表是所有预订的列表

当我在不使用booking_properties表的情况下运行下面的查询时,查询需要0.17秒,而使用left join则需要54秒

SET @start = '2018-05-01' , @end = '2018-05-04';

select * from `dates`

left join `property_bands` on property_bands.propband_start <= dates.day_date
and property_bands.propband_end >= dates.day_date

left join `booking_properties` on booking_properties.property_id = property_bands.property_id
and(
    (
        booking_properties.bprop_start <= @start
        and booking_properties.bprop_end >= @end
    )
    or(
        booking_properties.bprop_start >= @start
        and booking_properties.bprop_start < @end
    )
    or(
        booking_properties.bprop_end > @start
        and booking_properties.bprop_end <= @end
    )
)
and(
    booking_properties.bprop_deleted = '0000-00-00 00:00:00'
    or booking_properties.bprop_deleted > now()
)
and booking_properties.bprop_void < 1
where
    dates.day_date >= @start and dates.day_date < @end
我试图重写我在2009年写的一个查询,这个查询一直运行良好,但随着客户端平台的发展,系统变得越来越慢。这是当前运行的原始查询,需要14秒

SELECT datediff('2018-05-04', '2018-05-01') as `nights`,
    '2018-05-01' as arrival, 
    '2018-05-04' as departure, 
    sum(property_bands.propband_pricedirect) as bookingprice, 
    sum(property_bands.propband_pricespecial) as bookingspecial, 
    datediff('2018-05-04', '2018-05-01')-(count(*)) as mismatch, 
    property_bands.propband_minduration,
    properties.*,
    resorts.resort_name,
    resorts.resort_area,
    resorts.resort_country,
    resorts.resort_url,
    resorts.resort_tripid,
    resorts.resort_desc,
    property_types.proptype_name, (

    select booking_properties.booking_id from booking_properties where booking_properties.property_id = property_bands.property_id and ((booking_properties.bprop_start <= '2018-05-01' and booking_properties.bprop_end >= '2018-05-04') 
    or (booking_properties.bprop_start >= '2018-05-01' and booking_properties.bprop_start < '2018-05-04') or (booking_properties.bprop_end > '2018-05-01' and booking_properties.bprop_end <= '2018-05-04')) 
    and booking_properties.company_id = '100' and (booking_properties.bprop_deleted = '0000-00-00 00:00:00' or booking_properties.bprop_deleted > now()) and booking_properties.bprop_void < 1 limit 1) as booking_id

    FROM dates, property_bands, properties, property_types, resorts

    where properties.property_id = property_bands.property_id and property_types.proptype_id = properties.proptype_id
    and dates.day_date >= '2018-05-01' 
    and dates.day_date < '2018-05-04' 
    and property_bands.propband_start <= dates.day_date 
    and property_bands.propband_end >= dates.day_date

    and resorts.resort_id = properties.resort_id


    and (property_bands.propband_deleted = '0000-00-00 00:00:00' or property_bands.propband_deleted > now()) 
    and (properties.property_deleted = '0000-00-00 00:00:00' or properties.property_deleted > now())
    and (resorts.resort_deleted = '0000-00-00 00:00:00' or resorts.resort_deleted > now())
    group by property_bands.property_id order by properties.booking_count asc;

您是否尝试过将这些条件从开始移动到WHERE子句中?@bufjape,但这可能会改变查询的逻辑。我不这么认为。试试看,或者解决它…请参阅:。此外,除了显示创建表外,有关查询性能的问题始终需要解释。另外,重叠测试(如果是这样的话)远比查询所暗示的简单:如果事件A在事件B结束之前开始,则事件A与事件B重叠,如果事件A在事件B开始之后结束。