1

다음 쿼리가 있으면 속도를 높이기 위해 어떤 인덱스를 만들 수 있습니까?이 MySQL 쿼리에 대해 어떤 인덱스를 생성해야합니까?

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 

참고 :이 쿼리는 레일스에 의해 생성되었으므로 작성 방법을 완전히 제어 할 수 없습니다. 다음 수율 EXPLAIN 사용

:

ID = 1

SELECT_TYPE = SIMPLE

테이블 = residentials

= 범위

이 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 = 209,272

추가 = 사용 6

심판 = NULL

행 어디

012 당신은 시도 3,516,
+0

속도에 대한 제안의 커플 (도움을 확인하려면 다음과 같이 EXPLAIN 사용) - > 및 <조건에 BETWEEN을 사용해보십시오. 또한 지퍼'LIKE'는 성능이 무거울 것입니다. 나는 그것의 유무에 관계없이 그리고 zip에 대한 인덱스가 있거나없이 쿼리를 시도 할 것이다. –

답변

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 
+0

+1 예, 설명해주세요 (계획)이 목적으로 만 작성되었습니다. 나는 아직도 결과를 게시하시기 바랍니다 붙어 있습니다. 그렇지 않으면 당신이 보는 다른 모든 제안들이 어둠 속에서 찔 리게 될 것입니다. –

+0

만약 내가 도움이된다면 위의 EXPLAIN 결과를 추가했습니다. – kidbrax

0

인덱스에 대한 정말 중요한 컬럼의 순서는, 시도 :

is_active, created_at, modified, list_price, square_feet, property_type 
관련 문제