-1

我正在开发一个酒店搜索引擎。对于每个日期,它都有类似hotel info,的表格。hotel availability它运行良好,但与其他酒店搜索引擎(如 easytobook 或 hotel.com)相比,结果很慢。我的问题是:我怎样才能让它更快?目前它使用PHP直接从数据库中获取结果。

这是用于从数据库中获取结果的当前查询

SELECT DISTINCT t_hotel_info.hotelid, 
                t_hotel_info.hotelname, 
                t_hotel_info.description, 
                t_hotel_info.userid, 
                t_hotel_info.starrating, 
                t_hotel_info.currency, 
                t_hotel_info.hotel_url, 
                t_hotel_info.address1, 
                t_hotel_info.state, 
                t_hotel_info.country, 
                t_hotel_images1.imagepath1 
FROM   t_hotel_info 
       INNER JOIN t_hotel_images1 
               ON t_hotel_info.userid = t_hotel_images1.userid 
       INNER JOIN t_hotel_availability 
               ON t_hotel_info.userid = t_hotel_availability.userid 
       INNER JOIN t_hotel_account_info 
               ON t_hotel_info.userid = t_hotel_account_info.userid 
       INNER JOIN t_hotel_facilities 
               ON t_hotel_info.userid = t_hotel_facilities.userid 
WHERE  city = '$_GET[city]' 
       AND t_hotel_availability.sdate >= '$_GET[checkin]' 
       AND t_hotel_availability.sdate < '$_GET[checkout]' 
       AND t_hotel_availability.roomrate <> 0 

这是我对表格的解释声明

id  select_type  table                 type  possible_keys  key      key_len  ref   rows   Extra
1   SIMPLE       t_hotel_facilities    ALL   NULL           NULL     NULL     NULL  627    Using temporary
1   SIMPLE       t_hotel_info          ref   PRIMARY        PRIMARY  4         ezee2book.t_hotel_facilities.UserID   8  Using where
1   SIMPLE       t_hotel_account_info  ALL   NULL           NULL     NULL     NULL  767    Using where; Using join buffer
1   SIMPLE       t_hotel_images1       ALL   NULL           NULL     NULL     NULL  732    Using where; Using join buffer
1   SIMPLE       t_hotel_availability  ALL   NULL           NULL     NULL     NULL  51806  Using where; Distinct; Using join buffer
4

1 回答 1

1

首先,我很确定你可以稍微修剪一下:

SELECT DISTINCT t_hotel_info.hotelid, 
                t_hotel_info.hotelname, 
                t_hotel_info.description, 
                t_hotel_info.userid, 
                t_hotel_info.starrating, 
                t_hotel_info.currency, 
                t_hotel_info.hotel_url, 
                t_hotel_info.address1, 
                t_hotel_info.state, 
                t_hotel_info.country, 
                t_hotel_images1.imagepath1 
FROM   t_hotel_info 
       INNER JOIN t_hotel_images1 
               ON t_hotel_info.userid = t_hotel_images1.userid 
       INNER JOIN t_hotel_availability 
               ON t_hotel_info.userid = t_hotel_availability.userid 
WHERE  city = '$_GET[city]' 
       AND t_hotel_availability.sdate >= '$_GET[checkin]' 
       AND t_hotel_availability.sdate  0 
       AND t_hotel_availability.roomrate <> 0 

那些额外JOIN的陈述正在杀死你。

此外,在性能方面,我首先看两件事:

  1. 查询计划缓存
  2. 索引

查询计划缓存

很多人会为此争论不休,但我亲眼见过。通常最好进行查询,尤其是这种大小的查询,然后将其转换为存储过程。为什么?因为查询计划被缓存了。请记住,服务器仅在确定如何运行查询时就花费了大量精力。

最近,数据库引擎在缓存计划方面变得更加智能。例如,如果您要参数化查询,而不是在WHERE子句中发送原始值(哦,我不会提及 SQL 注入),它可以在第一次运行后缓存该查询计划。

索引

索引很可能是关键。最重要的是,虽然我喜欢一个覆盖索引,但你要确保它没有扫描表格。因此,例如,您需要确保在 上city和在上都有一个索引sdate。这将允许引擎至少利用有序索引来过滤查询。

覆盖索引,这是您真正开始看到性能提高的地方,在这里是不现实的,因为您要撤回这么多字段。如果碰巧您不需要所有这些字段,则将它们修剪掉,您也可以利用覆盖索引。

于 2013-07-27T10:01:53.053 回答