0

如果我有以下查询,可以创建哪些索引来加快速度?

SELECT `residentials`.* 
FROM `residentials` 
WHERE 
  (is_active = 1) AND 
  (
    (created_at > '2012-02-20 20:51:56' OR modified > '2012-02-20 20:51:56') AND 
    list_price >= 229000 AND 
    list_price <= 311000 AND 
    square_feet >= '1223' AND 
    square_feet <= '1654' AND 
    property_type IN ('commercial','condo','detached','house','multi','rowtownhouse','semidetached') AND
    (zip LIKE '%19147%')
  )
ORDER BY list_price DESC

仅供参考,这个查询是由 Rails 生成的,所以我无法完全控制它的构造方式。

使用 EXPLAIN 会产生以下结果:

编号 = 1

选择类型 = 简单

表 = 住宅

类型 = 范围

possible_keys = index_residentials_on_list_price,index_residentials_on_property_style,index_residentials_on_square_feet,index_residentials_on_modified,index_residentials_on_is_active,index_residentials_on_is_active_and_board_id,index_residentials_is_active_board_id_list_price_property_type,index_residentials_on_is_active_and_created_at_and_modified,dates_and_type,dates_type_price,board_price_type_zip,board_price_type_zip_mls

关键 = index_residentials_on_list_price

key_len = 6

参考 = 空

行 = 209272

额外 = 使用 where

4

2 回答 2

2

你试过了吗 :

EXPLAIN SELECT `residentials`.* 
FROM `residentials` 
WHERE 
  (is_active = 1) AND 
  (
    (created_at > '2012-02-20 20:51:56' OR 
     modified > '2012-02-20 20:51:56') AND 
     list_price >= 229000 AND 
     list_price <= 311000 AND 
     square_feet >= '1223' AND 
     square_feet <= '1654' AND 
     property_type IN ('commercial','condo','detached','house','multi','rowtownhouse','semidetached') AND
     (zip LIKE '%19147%')
  )
ORDER BY list_price DESC
于 2012-03-08T16:10:57.947 回答
0

列的顺序对索引非常重要,请尝试:

is_active, created_at, modified, list_price, square_feet, property_type 
于 2012-03-08T19:00:18.060 回答