- SQL code
select fd_shortenedform,fd_spec, ( select sum(number) as number from [order] where refund_status ='NO_REFUND' and (outer_iid = tb_goods.fd_spec or outer_sku_id = tb_goods.fd_spec)) as fd_number from tb_goods
主要是 那个 or 的条件,导致索引无效,查询效率有点低
如果没有聚合函数的话 可以用 union all !现在有一个 sum的聚合函数 ,这个语句要如何优化,还请前辈们帮助下...
------解决方案--------------------
- SQL code
select fd_shortenedform,fd_spec, ( select sum(number) as number from [order] where refund_status ='NO_REFUND' and (outer_iid = tb_goods.fd_spec or outer_sku_id = tb_goods.fd_spec)) as fd_number from tb_goods with(index(index_name))--加个强行使用索引试一下。实在不行就分成两次然后再累加求和 SQL 语句的长短和效率不是成正比的
------解决方案--------------------
union all 和OR理论上功能是不同的,如果两个条件都符合的记录,union all会计算两次造成SUM虚增