2012-12-16 43 views
10

我有這樣的ActiveRecord查詢使用MySQL特定指數與軌道

issue = Issue.find(id) 
issue.articles.includes(:category).merge(Category.where(permalink: perma)) 

而轉換爲MySQL查詢

SELECT `articles`.`id` AS t0_r0, `articles`.`title` AS t0_r1, 
     `articles`.`hypertitle` AS t0_r2, `articles`.`html` AS t0_r3, 
     `articles`.`author` AS t0_r4, `articles`.`published` AS t0_r5, 
     `articles`.`category_id` AS t0_r6, `articles`.`issue_id` AS t0_r7, 
     `articles`.`date` AS t0_r8, `articles`.`created_at` AS t0_r9, 
     `articles`.`updated_at` AS t0_r10, `articles`.`photo_file_name` AS t0_r11, 
     `articles`.`photo_content_type` AS t0_r12, `articles`.`photo_file_size` AS t0_r13, 
     `articles`.`photo_updated_at` AS t0_r14, `categories`.`id` AS t1_r0, 
     `categories`.`name` AS t1_r1, `categories`.`permalink` AS t1_r2, 
     `categories`.`created_at` AS t1_r3, `categories`.`updated_at` AS t1_r4, 
     `categories`.`issued` AS t1_r5, `categories`.`order_articles` AS t1_r6 
     FROM `articles` LEFT OUTER JOIN `categories` ON 
     `categories`.`id` = `articles`.`category_id` WHERE 
     `articles`.`issue_id` = 409 AND `categories`.`permalink` = 'Διεθνή' LIMIT 1 

在此查詢中,我看到的explation使用錯誤的索引

+----+-------------+------------+-------+---------------------------------------------------------------------------+-------------------------------+---------+-------+------+----------+-------------+ 
| id | select_type | table  | type | possible_keys                | key       | key_len | ref | rows | filtered | Extra  | 
+----+-------------+------------+-------+---------------------------------------------------------------------------+-------------------------------+---------+-------+------+----------+-------------+ 
| 1 | SIMPLE  | categories | const | PRIMARY,index_categories_on_permalink          | index_categories_on_permalink | 768  | const | 1 | 100.00 |    | 
| 1 | SIMPLE  | articles | ref | index_articles_on_issue_id_and_category_id, index_articles_on_category_id | index_articles_on_category_id | 2  | const | 10 | 100.05 | Using where | 
+----+-------------+------------+-------+---------------------------------------------------------------------------+-------------------------------+---------+-------+------+----------+-------------+ 

我有兩個索引,category_id單獨和issue_id - category_id

在此查詢中,我正在使用issue_idcategory_id進行搜索,這比使用index_articles_on_issue_id_and_category_id時要快得多,比index_articles_on_category_id快得多。

如何使用活動記錄查詢選擇正確的索引?

+0

麥克的具體的例子,你可以只用一個綜合指數,以達到同樣的效果。如果您只有使用category_id的查詢以及其他使用category_id和issue_id(組合)的查詢,那麼正確的索引應該是'index_articles_on_category_id_and_issue_id'。有了這樣的複合索引,你可以利用這兩種查詢類型。看看這個http://stackoverflow.com/questions/1823685/when-should-i-use-a-composite-index – alup

回答

23

您可以方便AREL像這樣使用一個索引:

class Issue 
    def self.use_index(index) 
    # update: OP fixed my mistake 
    from("#{self.table_name} USE INDEX(#{index})") 
    end 
end 

# then 
Issue.use_index("bla").where(some_condition: true) 
+2

謝謝,它適用於litle修復'from(「#{self.table_name} USE INDEX(#{}指數)「)' –