如何优化联合查询这样两个表?
时间:2011-12-20
来源:互联网
A表(排班表)
usrID time1 time2
123 2011-12-19 08:00:00 2011-12-19 18:00:00
456 2011-12-19 08:00:00 2011-12-19 18:00:00
123 2011-12-20 08:00:00 2011-12-20 18:00:00
B表(进出时间表)
userID time
123 2011-12-19 08:12:01
123 2011-12-19 08:30:01
456 2011-12-19 08:16:01
B表有上万记录,A表固定上千条记录
select B.*,
(select max(A.time) from A where A.userID = B.userID and A.time between B.time1 and B.time2) as acttime
from B
这样查询花费时间太久,如何优化?
usrID time1 time2
123 2011-12-19 08:00:00 2011-12-19 18:00:00
456 2011-12-19 08:00:00 2011-12-19 18:00:00
123 2011-12-20 08:00:00 2011-12-20 18:00:00
B表(进出时间表)
userID time
123 2011-12-19 08:12:01
123 2011-12-19 08:30:01
456 2011-12-19 08:16:01
B表有上万记录,A表固定上千条记录
select B.*,
(select max(A.time) from A where A.userID = B.userID and A.time between B.time1 and B.time2) as acttime
from B
这样查询花费时间太久,如何优化?
作者: the_else 发布时间: 2011-12-20
a:userID time
b:userID time1 time2
建立复合索引
b:userID time1 time2
建立复合索引
作者: wwwwb 发布时间: 2011-12-20
SQL code
select B.userid,B.time,max(A.time) from A,B where A.userID = B.userID and A.time between B.time1 and B.time2 group by B.userid,B.time
作者: rucypli 发布时间: 2011-12-20
B表你反正是全面扫描,有没有索引对效率不起作用。添加A表如下索引即可。
create index xxx on A ( userID,time)
create index xxx on A ( userID,time)
作者: ACMAIN_CHM 发布时间: 2011-12-20
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28