+ -
当前位置:首页 → 问答吧 → 问个mysql查询速度的问题为什么select子句特别慢

问个mysql查询速度的问题为什么select子句特别慢

时间:2010-12-21

来源:互联网

两个表:

a表
-------------------------------
id     price         两个字段
-------------------------------
1     100
2     120
3     130

b表
-------------------------------
aid   bid  两个字段
-------------------------------
1     2
1     3

b表的两个字段都是用来存放a表的id的。代表a表的第2和第三条记录是第一条记录的子记录。
现在我要求a表里面id为1的记录的价格和包含的子记录的价格和。
select sum(price) as total_price from a where id=1 or id in (select bid from b where aid=1)
速度特别慢。大概要50秒左右。两个表记录大概都是几千条。b表无主键。
为什么会这么慢呢?select语句有没有其他写法?

作者: elevenkey   发布时间: 2010-12-21

用联合查询,如,left join,inner join

select sum(a.price) as total_price from a,b where a.id=b.aid where a.id=1

作者: kelon   发布时间: 2010-12-21