有时候子查询比左连接查询速度快

最近几天在优化数据库,有些数据表因为建立时候不合理导致查询的时候速度比较慢,比如三个表,三个表中数据最少的都是十万条,这些表在左联或者右联的时候速度可能需要几秒钟,再加上where条件,条件中再加or,这时候速度是非常的慢的,往往需要10秒以上,这时候可以用子查询或者union 或者union all 代替,根据情况而定

比如这个语句用子查询速度就比较快

原来的语句:

select
K.EmployeeNumber,
K.PositionName,
K.Name,
K.SkillWages,
k.AchievementsWages,
K.TotleIncome as Income,
K.TotleCashIncome as CashIncome,
(ISNULL(M0.Val2,0)+ISNULL(M1.Val2,0))TurnoverMoney,
(ISNULL(N0.Val2,0)+ISNULL(N1.Val2,0)+ISNULL(N2.Val2,0)+ISNULL(N3.Val2,0)+ISNULL(N4.Val2,0))LadderMoney
from (SELECT
A.EmployeeNumber
,A.Name
,B.PositionName
,A.SkillWages
,A.AchievementsWages
,A.ExtText2
,ISNULL((select SUM(Performance) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=0 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)SerPerformance
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=1 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)ProPerformance_Cash
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=4 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)PlanPerformance_Cash
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=2 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)OpenPerformance_Cash
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=3 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)RechargePerformance_Cash
,ISNULL((select SUM(Performance) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotlePerformance
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotlePerformance_Cash
,ISNULL((select SUM(ISNULL(Income,0)) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotleIncome
,ISNULL((select SUM(ISNULL(CashIncome,0)) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotleCashIncome
from
[User] A left join t_UserLevel B on A.ExtText2=B.Id where A.OrgCode=‘{2}‘ and A.[Status]!=0 and A.DeleFlag=1 and A.OrgType=6 and A.DeleFlag=1 {4}
)K
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopTurnoverPercentageSet A inner join t_ShopTurPerDetailSet B ON A.ID=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=57 and a.ShopCode=‘{3}‘)M0 ON K.ExtText2=M0.RoleCode AND M0.BeginVal<=K.TotlePerformance AND M0.EndVal>=K.TotlePerformance
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopTurnoverPercentageSet A INNER JOIN t_ShopTurPerDetailSet B ON A.ID=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=55 and a.ShopCode=‘{3}‘)M1 ON K.ExtText2=M1.RoleCode AND M1.BeginVal<=K.TotlePerformance_Cash AND M1.EndVal>=K.TotlePerformance_Cash
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=59 and a.ShopCode=‘{3}‘)N0 ON K.ExtText2=N0.RoleCode AND N0.BeginVal<=K.SerPerformance AND N0.EndVal>=K.SerPerformance
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=60 and a.ShopCode=‘{3}‘)N1 ON K.ExtText2=N1.RoleCode AND N1.BeginVal<=K.ProPerformance_Cash AND N1.EndVal>=K.ProPerformance_Cash
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=61 and a.ShopCode=‘{3}‘)N2 ON K.ExtText2=N2.RoleCode AND N2.BeginVal<=K.OpenPerformance_Cash AND N1.EndVal>=K.OpenPerformance_Cash
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=62 and a.ShopCode=‘{3}‘)N3 ON K.ExtText2=N3.RoleCode AND N3.BeginVal<=K.RechargePerformance_Cash AND N3.EndVal>=K.RechargePerformance_Cash
left join
(select A.[Type],A.RoleCode,B.BeginVal,B.EndVal,B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=63 and a.ShopCode=‘{3}‘)N4 ON K.ExtText2=N4.RoleCode AND N4.BeginVal<=K.PlanPerformance_Cash AND N4.EndVal>=K.PlanPerformance_Cash )

修改过的语句

select
K.EmployeeNumber,
K.PositionName,
K.Name,
K.SkillWages,
k.AchievementsWages,
K.TotleIncome as Income,
K.TotleCashIncome as CashIncome,
(ISNULL((select B.Val2 from t_ShopTurnoverPercentageSet A inner join t_ShopTurPerDetailSet B ON A.ID=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=57 and a.ShopCode=‘{3}‘ AND K.ExtText2=A.RoleCode AND B.BeginVal<=K.TotlePerformance AND B.EndVal>=K.TotlePerformance),0)
+
ISNULL((select B.Val2 from t_ShopTurnoverPercentageSet A inner join t_ShopTurPerDetailSet B ON A.ID=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=55 and a.ShopCode=‘{3}‘and K.ExtText2=A.RoleCode AND B.BeginVal<=K.TotlePerformance_Cash AND B.EndVal>=K.TotlePerformance_Cash),0)
)TurnoverMoney,
(ISNULL((select B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=59 and a.ShopCode=‘{3}‘ AND K.ExtText2=A.RoleCode AND B.BeginVal<=K.SerPerformance AND B.EndVal>=K.SerPerformance),0)
+
ISNULL((select B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=60 and a.ShopCode=‘{3}‘ AND K.ExtText2=A.RoleCode AND B.BeginVal<=K.ProPerformance_Cash AND B.EndVal>=K.ProPerformance_Cash),0)
+
ISNULL((select B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=61 and a.ShopCode=‘{3}‘ AND K.ExtText2=A.RoleCode AND B.BeginVal<=K.OpenPerformance_Cash AND B.EndVal>=K.OpenPerformance_Cash),0)
+
ISNULL((select B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=62 and a.ShopCode=‘{3}‘ AND K.ExtText2=A.RoleCode AND B.BeginVal<=K.RechargePerformance_Cash AND B.EndVal>=K.RechargePerformance_Cash),0)
+
ISNULL((select B.Val2 from t_ShopLadderPercentageSet A INNER JOIN t_ShopLaPerDetailSet B ON A.Id=B.SetCode WHERE A.DeleFlag=1 AND B.DeleFlag=1 AND A.[Type]=63 and a.ShopCode=‘{3}‘ AND K.ExtText2=A.RoleCode AND B.BeginVal<=K.PlanPerformance_Cash AND B.EndVal>=K.PlanPerformance_Cash),0))LadderMoney
from (SELECT
A.EmployeeNumber
,A.Name
,B.PositionName
,A.SkillWages
,A.AchievementsWages
,A.ExtText2
,ISNULL((select SUM(Performance) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=0 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)SerPerformance
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=1 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)ProPerformance_Cash
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=4 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)PlanPerformance_Cash
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=2 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)OpenPerformance_Cash
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and SaleType=3 and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)RechargePerformance_Cash
,ISNULL((select SUM(Performance) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotlePerformance
,ISNULL((select SUM(Performance_Cash) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotlePerformance_Cash
,ISNULL((select SUM(ISNULL(Income,0)) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotleIncome
,ISNULL((select SUM(ISNULL(CashIncome,0)) from t_ShopSerWaiter where [Status]=1 and DeleFlag=1 and UserCode=A.UserCode and CreateTime>=‘{0}‘ and CreateTime<‘{1}‘),0)TotleCashIncome
from
(SELECT EmployeeNumber,ExtText2,Name,SkillWages,AchievementsWages,UserCode FROM dbo.[User] where OrgCode=‘{2}‘ and [Status]!=0 and DeleFlag=1 and OrgType=6 and DeleFlag=1 {4}) A left join t_UserLevel B on A.ExtText2=B.Id
)K

时间: 2024-11-04 18:21:08

有时候子查询比左连接查询速度快的相关文章

MySQL学习——SQL查询语句(连接查询&amp;子查询)(三)

一:连接查询: 连接查询是将俩个或者俩个以上的表按照某个条件连接起来,从中选择需要的数据,连接查询同时查询俩个或者俩个以上的表时使用,当不同的表中存在表示相同意义的字段时,可以通过该字段来连接这几个表,例如,学生表中有course_id字段来表示所学课程的课程号,课程表中有num字段来表示课程号,那么可以通过学生表中的course_id字段与课程表中的num字段来进行连接查询,连接查询包括内连接查询和外连接查询. 1.1 内连接查询 内连接查询是一种常用的连接查询,内连接查询可以查询俩个或者以上

EF的左连接查询

在EF中,当在dbset使用join关联多表查询时,连接查询的表如果没有建立相应的外键关系时,EF生成的SQL语句是inner join(内联),对于inner join,有所了解的同学都知道,很多时候这并不是我们的本意,实例如下: var list = from o in context.CTMS_OD_ORDERS join d in context.CTMS_SUP_DOCTOR on o.OWNERDOCID equals d.USERID join e in context.CTMS_

左连接查询

左连接查询语句 表1 left join 表2 on 条件: 然后where,having,group等语句可以照常使用 以下例子用两次左连接进行匹配 select t1.tname,mres,t2.tname,matime from m left join t as t1 on t1.tid=m.hid left join t as t2 on t2.tid=m.gid; mysql> set names gbk; Query OK, 0 rows affected (0.02 sec) my

《Entity Framework 6 Recipes》中文翻译系列 (16) -----第三章 查询之左连接和在TPH中通过派生类排序

翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-10应用左连接 问题 你想使用左外连接来合并两个实体的属性. 解决方案 假设你有如图3-11所示的模型. 图3-11 一个包含Product和TopSelling的模型 畅销产品有一个与之关联的TopSelling实体.当然,不是所有的产品都是畅销产品.这就是为什么关系为零或者1.当一个产品为畅销产品时,与之关联的topSelling实体包含一个用户评级.你想查找和呈现所有的产品,和与之

多个左连接查询

建表 create table AAAAA0 ( GKNAME VARCHAR2(50), NUM    VARCHAR2(10) ) create table AAAAA1 ( CNAME VARCHAR2(50), NUM0  VARCHAR2(10), NUM1  VARCHAR2(10), NUM2  VARCHAR2(10) ) 问题描述 ORACLE SQL 多列外连接怎么匹配查询? 列出每条船的当前港口名,下一站港口名,终点港口名. 解决 select b0.cname,a0.gk

菜鸟笔记——纠结的左外连接查询和右外连接查询

1.前言 外连接查询也是很基础的知识,可是我也花了几个小时才理解透.在本文不太过多用书本语言来描述,我会以自己的思维理解和描述该知识 2.引出  首先,连接查询的方法也有很多.但是有些知识点学了,我们必须清楚会在什么情况下用到. 例:查询没有参加考试的学生信息.  按常规的做法内连接查询是无法得到的,为什么?因为内连接查询时,数据结果集是匹配条件列相等的数据.如果某行数据匹配的列的值不相等,那么行 数据将不会显示.这时候就需要用到外连接查询. 3.什么是做表?什么是右表? 写在关键字Left j

mysql left join 左连接查询关联n多张表

eft join 左连接即以左表为基准,显示坐标所有的行,右表与左表关联的数据会显示,不关联的则不显示.关键字为left join on. **基本用法如下: select table a left join table b on a.id = b.ta_id** 注意:1??其中on后面关联的字段应该是同一字段(两表关联的外键) 2??由于以左表为基准,左表一条记录如果对应右表多条记录,那查出的数据中右表的数据也只显示一条,如果要都显示,可以用group_contact()将字段用逗号隔开显示

003.分组统计查询和表连接查询

--分组统计查询 group by having 1 select 分组字段 ,聚合函数 2 having 后常跟聚合函数,也可以跟分组字段 3 where 后不可以直接跟聚合函数 4 where(筛选行) -group by (分组) -having (筛选组) --表连接查询 1 笛卡尔积 (交叉连接 cross join) a*b A) SELECT * FROM A,B B) SELECT * FROM A CROSS JOIN B 2 内连接 原理: 将两个表中关联字段相等的行查询出来

thinkphp左连接查询,原生查询

原生查询: $Model = new Model();$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;$voList = $Model->query($sql); 2.左查询 $model=M('picture