问个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语句有没有其他写法?
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
如
select sum(a.price) as total_price from a,b where a.id=b.aid where a.id=1
作者: kelon 发布时间: 2010-12-21
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28