繁体   English   中英

用SUM优化选择…案例

[英]optimize select with SUM … CASE

有什么方法可以优化此查询:

select 
orders_id,
orders_products_id,
sum(CASE products_options_id WHEN 1 THEN products_options_values_id ELSE 0 END) as color,
sum(CASE products_options_id WHEN 2 THEN products_options_values_id ELSE 0 END) as talle

from 
orders_products_attributes 

group by 
orders_products_id

这是EXPLAIN输出:

id  select_type   table                       type   possible_keys   key   key_len   ref     rows   Extra
1   SIMPLE        orders_products_attributes  ALL    NULL            NULL  NULL      NULL  69006  Using temporary; Using filesort

谢谢!

卢西亚诺

我建议使用列(orders_product_id,products_options_id,orders_id)为此查询创建覆盖索引。

CREATE INDEX ix_opa_opid_poid_oid
    ON orders_products_attributes (orders_product_id, products_options_id, orders_id)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM