表之间的关系如下图
Product.paginate_by_sql("SELECT distinct products.* FROM products left JOIN product_prices ON product_prices.product_id =products.id left JOIN taggings ON taggings.other_id = products.id AND taggings.t_type='product' left JOIN sub_taggings ON sub_taggings.other_id = products.id AND sub_taggings.t_type='product' left JOIN brand_products ON brand_products.product_id = products.id where #{conditions} order by #{order_info}",:per_page=>28, :page=>page||1)
下面是日志
19:39:10 DEBUG: [1m[35mProduct Load (826.8ms)[0m SELECT distinct products.* FROM products left JOIN product_prices ON product_prices.product_id = products.id left JOIN taggings ON taggings.other_id = products.id AND taggings.t_type='product' left JOIN sub_taggings ON sub_taggings.other_id = products.id AND sub_taggings.t_type='product' left JOIN brand_products ON brand_products.product_id = products.id where products.status=true order by products.is_top desc, products.is_recommend desc, products.rank desc, products.created_at desc LIMIT 28 OFFSET 0
19:39:10 DEBUG: [1m[36mEXPLAIN (15.6ms)[0m [1mEXPLAIN SELECT distinct products.* FROM products left JOIN product_prices ON product_prices.product_id = products.id left JOIN taggings ON taggings.other_id = products.id AND taggings.t_type='product' left JOIN sub_taggings ON sub_taggings.other_id = products.id AND sub_taggings.t_type='product' left JOIN brand_products ON brand_products.product_id = products.id where products.status=true order by products.is_top desc, products.is_recommend desc, products.rank desc, products.created_at desc LIMIT 28 OFFSET 0[0m
19:39:10 WARN: EXPLAIN for: SELECT distinct products.* FROM products left JOIN product_prices ON product_prices.product_id = products.id left JOIN taggings ON taggings.other_id = products.id AND taggings.t_type='product' left JOIN sub_taggings ON sub_taggings.other_id = products.id AND sub_taggings.t_type='product' left JOIN brand_products ON brand_products.product_id = products.id where products.status=true order by products.is_top desc, products.is_recommend desc, products.rank desc, products.created_at desc LIMIT 28 OFFSET 0
+----+-------------+----------------+------+-------------------------------------------------------------+------------------------------------+---------+-----------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------+------+-------------------------------------------------------------+------------------------------------+---------+-----------------+------+----------------------------------------------+
| 1 | SIMPLE | products | ref | index_products_on_status | index_products_on_status | 2 | const | 8627 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | product_prices | ref | index_product_prices_on_product_id | index_product_prices_on_product_id | 5 | qdd.products.id | 1 | Using index; Distinct |
| 1 | SIMPLE | taggings | ref | index_taggings_on_t_type,index_taggings_on_other_id | index_taggings_on_other_id | 5 | qdd.products.id | 1 | Distinct |
| 1 | SIMPLE | sub_taggings | ref | index_sub_taggings_on_other_id,index_sub_taggings_on_t_type | index_sub_taggings_on_other_id | 5 | qdd.products.id | 1 | Distinct |
| 1 | SIMPLE | brand_products | ref | index_brand_products_on_product_id | index_brand_products_on_product_id | 5 | qdd.products.id | 1 | Using index; Distinct |
+----+-------------+----------------+------+-------------------------------------------------------------+------------------------------------+---------+-----------------+------+----------------------------------------------+
5 rows in set (0.03 sec)
19:39:12 DEBUG: [1m[35mProduct Count (1497.6ms)[0m SELECT COUNT(*) FROM (SELECT distinct products.* FROM products left JOIN product_prices ON product_prices.product_id = products.id left JOIN taggings ON taggings.other_id = products.id AND taggings.t_type='product' left JOIN sub_taggings ON sub_taggings.other_id = products.id AND sub_taggings.t_type='product' left JOIN brand_products ON brand_products.product_id = products.id where products.status=true ) AS count_table
will_paginate 插件支持对数组进行分页
def paginate(options = {})
page = options[:page] || 1
per_page = options[:per_page] || WillPaginate.per_page
total = options[:total_entries] || self.length
WillPaginate::Collection.create(page, per_page, total) do |pager|
pager.replace self[pager.offset, pager.per_page].to_a
end