0

我想做优化这个查询。我已经给出了使用表格的统计数据。

products 和 products_categories 表有大约 5 laks 记录。但对于下面提到的类别,它有 1600 条记录。我为这 1600 条记录创建了插槽。每个产品可以有最少 1 个插槽和最多 10 个插槽。但老虎机表有大约 3 万条记录。槽表也可以有已经过期的槽。我想获得即将到期的产品,而其余产品则排在该产品之后。

我为 end_time 列创建了索引。但是我使用了条件运算符,所以在这个查询中没有使用索引。我想优化这个查询。请告诉我最好的方法。

EXPLAIN 
    SELECT products.*, IF(slot.end_time > NOW(), 1, 2) as order_by_end_time 
    FROM products 
        INNER JOIN products_categories ON products_categories.productid = products.productid 
        LEFT JOIN (select product_id, end_time from slot where end_time>now() order by end_time) as slot ON slot.product_id = products.productid
    WHERE products_categories.categoryid='4410' 
        AND products.saleid = 2 
    GROUP BY products.productid 
    ORDER BY order_by_end_time ASC ,  slot.end_time ASC
4

1 回答 1

0

id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY xcart_products_categories ref PRIMARY,cpm,productid,orderby,pm cpm 4 const 1523 使用索引;使用临时的;使用文件排序 1 PRIMARY xcart_products eq_ref PRIMARY,saleid PRIMARY 4 wwwbvira_xcart.xcart_products_categories.productid 1 使用 where 1 PRIMARY ALL NULL NULL NULL NULL 395802
2 DERIVED bvira_megahour_time_slot ALL i_end_time NULL NULL NULL 398907 使用文件排序

于 2013-01-29T12:45:12.473 回答