代码之家  ›  专栏  ›  技术社区  ›  user9050678

MYSQL对连接查询索引的最佳使用

  •  1
  • user9050678  · 技术社区  · 7 年前

    homes_info 有4列

     home_id (primary_key)  
     title (varchar)
     price (double)
     location_id (foreign key)
    

    我还有一张桌子

    locations 有3列

     location_id (primary key)
     location_name (varchar)
     location_state (varchar)
    

    SELECT * FROM homes_info WHERE title = 'xxxxx' && price = 'xxxx'

    SELECT * FROM homes_info LEFT JOIN locations on homes_info.location_id = locations.location_id WHERE title = 'xxx' && price = 'xxxxx' && location_name = 'xxxx'

    ALTER TABLE homes_info ADD INDEX(title, price)

    ALTER TABLE locations ADD INDEX(location_name)

    我知道第一个查询是复合索引 INDEX(title, price) LEFT JOIN )? 使用单独的索引 title price

    简单地说,

    谢谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   ScaisEdge    7 年前

    对于表位置,可以使用y。。你可以用单一复合材料

    ALTER TABLE locations ADD locations_info INDEX(location_name , localtion_id)
    

    使用具有localtion(name)和location(id)的组合,可以确保where(join)子句所需的所有信息都由索引解析,从而避免对get的数据表的访问。。位置\u id