使用left join比直接使用where速度快的原因-宏鹏
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-查询/SQL语句


使用left join比直接使用where速度快的原因

发表时间:2014/5/7 8:38:26 评论(1) 浏览(7401)  评论 | 加入收藏 | 复制
   
摘 要:使用left join比直接使用where速度快的原因
正 文:

多表使用left join只是把主表里的所有数据查询出来,其他表只查询表中的符合条件的某一条记录,所以速度非常快;而多表使用where内联,是把所有表的数据全查出来,然后进行比对,所以速度非常慢。

使用left join要注意确定哪一张表是主表,如果无法确定主表,则选择哪张表查询的字段最多,就把哪张表作为主表。

示例如下:

使用left join,同样的数据量,时间不到1秒钟!

Select a.projectno,MAX(a.projectname) projectname,max(a.projectMoney) projectMoney,
max(a.projectLimitYear) projectLimitYear,max(a.monthcharge) monthcharge,
max(c.orgname) orgname,max(d.businesstypename) businesstypename,max(e.name) name,
max(b.dicvalue) dicvalue,
min(CONVERT(varchar(100),DATEADD("DAY",jbl.DelayDays,jbl.ReportTime),23)) as period,
max(f.fiveleveltype) fiveleveltype,max(a.ProjectInfoId) ProjectInfoId,
max(g.FlowRunId) FlowRunId
FROM
(select ProjectInfoId,ProjectNo,Status,OrgId,TypeID,UserAId,IsDelete,ProjectName,ProjectMoney,ProjectLimitYear,MonthCharge from jt_biz_projectinfo) a
LEFT JOIN
(select b.* from (select MAX(id) id, ProjectNo from JT_Biz_Lecture group by ProjectNo) a,
JT_Biz_Lecture b where a.id=b.id) f
on a.projectno=f.projectno
LEFT JOIN
(select DicId,IsDelete,DicValue,DicCode from jt_Base_Dictionary) b
ON a.status=b.dicId
LEFT JOIN
(select ProjectNo,DelayDays,ReportTime from Jt_Biz_LectureTemp) jbl
ON a.projectno=jbl.projectno
LEFT JOIN
(select IsDelete,OrgName,OrgId from jt_base_org) c
ON a.orgid=c.orgid
LEFT JOIN
(select ID,BusinessTypeName from d_businesstype) d
ON a.typeid=d.id
LEFT JOIN
(select UserInfoId,IsDelete,Name,UserName from jt_base_userInfo) e
ON a.useraid=e.userinfoid
LEFT JOIN
(select UserId,PostId,OrgIds from JT_Base_Post_UserOrg) j
ON j.UserId=e.userinfoid
LEFT JOIN
(select PostId from JT_Base_Post) k
ON j.PostId=k.PostId
LEFT JOIN
(select FlowRunId,ProjectId from jt_flow_run) g
ON a.ProjectInfoId=g.ProjectId
LEFT JOIN
(select FlowRunId from JT_Flow_Run_Prcs) h
ON g.FlowRunId = h.FlowRunId
LEFT JOIN
(select orgId,OrgName from JT_Base_Org) l
ON a.OrgId=l.OrgId
Where a.status in(82,83,84) and a.isdelete=0
and b.isdelete=0 and c.isdelete=0 and e.isdelete=0
and (l.OrgId in(null) or e.UserName='chenqf') group by a.ProjectNo;
--加快查询速度之对比

不使用left join,同样数据量,时间大概在50秒的样子!
select a.projectno,max(a.projectname) projectname,max(a.projectMoney) projectMoney,
max(a.projectLimitYear) projectLimitYear,max(a.monthcharge) monthcharge,
max(c.orgname) orgname,max(d.businesstypename) businesstypename,max(e.name) name,
max(b.dicvalue) dicvalue,
min(CONVERT(varchar(100),DATEADD("DAY",jbl.DelayDays,jbl.ReportTime),23)) as period,
max(f.fiveleveltype) fiveleveltype,max(a.ProjectInfoId) ProjectInfoId,
max(g.FlowRunId) FlowRunId
from (select DicId,IsDelete,DicValue,DicCode from jt_Base_Dictionary) b,
(select IsDelete,OrgName,OrgId from jt_base_org) c,
(select UserInfoId,IsDelete,Name,UserName from jt_base_userInfo) e,
(select ID,BusinessTypeName from d_businesstype) d,
(select FlowRunId,ProjectId from jt_flow_run) g,
(select FlowRunId from JT_Flow_Run_Prcs) h,
(select UserId,PostId,OrgIds from JT_Base_Post_UserOrg) j,
(select PostId from JT_Base_Post) k,
(select orgId,OrgName from JT_Base_Org) l,
(select ProjectNo,DelayDays,ReportTime from Jt_Biz_LectureTemp) jbl,
(select ProjectInfoId,ProjectNo,Status,OrgId,TypeID,UserAId,IsDelete,ProjectName,ProjectMoney,ProjectLimitYear,MonthCharge from jt_biz_projectinfo) a
left join
(select b.* from (select MAX(id) id, ProjectNo from JT_Biz_Lecture group by ProjectNo) a,
JT_Biz_Lecture b where a.id=b.id) f
on a.projectno=f.projectno where a.status=b.dicId and a.status in(82,83,84)
and a.projectno=jbl.projectno and a.orgid=c.orgid and a.typeid=d.id
and a.useraid=e.userinfoid and j.UserId=e.userinfoid and a.isdelete=0
and b.isdelete=0 and c.isdelete=0 and e.isdelete=0 and g.FlowRunId = h.FlowRunId 
and (l.OrgId in(null) or e.UserName='chenqf')
group by a.ProjectNo;


Access软件网交流QQ群(群号:198465573)
 
 相关文章
LEFT JOIN 用法一点心得  【andymark  2008/11/29】
SQL必知必会(10) 使用WHERE子句   【(美)Ben Forta 著  2009/7/31】
[access查询]【access小品】杀鬼子--论having和...  【todaynew  2010/9/27】
Access快速开发平台--筛选名称与Where条件应用示例  【红尘如烟  2013/6/17】
access数据库多表联合查询(Left Join等)的sql语句...  【缪炜  2013/8/9】
常见问答
技术分类
相关资源
文章搜索
关于作者

宏鹏

文章分类

文章存档

友情链接