给定三张表Hotel、Room、Price, Hotle表主要栏位有Hotel_id、name; Room表主要栏位有Room_id、hotel_fk、name Price表主要栏位Price_id、room_fk、name、price、date写sql返回5月1日当天,房间价格在170~190之间的酒店名称(假设酒店有4000个,每个酒店有1000个房间,每个房间有1000个价格,尽量优化性能)。
我能想到的就是简单的联接查询。
从sql语句层面该如何优化呢? 这个我没有太多经验,大家能帮忙下么?

解决方案 »

  1.   

    select *
    from hotle A,room B,price C
    wher A.hotel_id=B.hotel_fk and B.room_id=C.room_fk
       and C.date ='2013-05-01' and price between 170 and 190;
      

  2.   

    create index xxx on Price(date,price)
      

  3.   


    嗯 我也明白创建索引会提高效率。题目问的意思是 单从查询的sql这个地方优化
      

  4.   

    create index table on Price(date,price) 试一下
      

  5.   

    只是要酒店名称而已,那么我们可以只遍历A而不遍历C,优化方式如下:select a.name from Hotel a where exists(select 1 from room b, price c where b.hotel_fk=a.hotel_id and c.room_fk=b.room_id and c.date ='2013-05-01' and (c.price between 170 and 190) and rownum <=1)