SQL语句执行太慢,怎么优化?
selectstorage.prodcode,(selectsum(billsale.prod_number)frombillsalewherebillsale.prod...
select storage.prodcode,(select sum(billsale.prod_number) from billsale where billsale.prodcode=storage.prodcode) as '已销售数量'
from storage,查询出来有三百行数据 ,差不多要46秒钟,怎么优化呢 展开
from storage,查询出来有三百行数据 ,差不多要46秒钟,怎么优化呢 展开
展开全部
不用子查询,用临时表,子查询的效率下降时必然的,你建立临时表试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select billsale.prodcode,
sum(billsale.prod_number) as '已销售数量'
from storage, billsale
where billsale.prodcode = storage.prodcode
group by billsale.prodcode
sum(billsale.prod_number) as '已销售数量'
from storage, billsale
where billsale.prodcode = storage.prodcode
group by billsale.prodcode
更多追问追答
追问
谢谢您,不过这样查出来只有128行数量,少了很多,storage这个表是库存表,billsale这个表是销售表,storage里有的产品可能billsale里可能没有,但这些产品全部得显示出来
追答
是什么数据库?
select storage.prodcode,
sum(
case when billsale.prod_number is null then 0.0
else billsale.prod_number
end
) as '已销售数量'
from storage left join billsale
on storage.prodcode = billsale.prodcode
group by storage.prodcode
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Try this:
select storage.prodcode, sum(case when billsale.prod_number is null then 0 else billsale.prod_number end) as '已销售数量'
from storage left join billsale on billsale.prodcode=storage.prodcode
group by storage.prodcode
select storage.prodcode, sum(case when billsale.prod_number is null then 0 else billsale.prod_number end) as '已销售数量'
from storage left join billsale on billsale.prodcode=storage.prodcode
group by storage.prodcode
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在数据建索引撒,或通过引擎优化顾问
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询