原来的语句

select count(1) from  ( SELECT CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,ExamineStatus,[Status],DeleFlag,CreatorCode,CashMoney,CardGiftMoney,FreeMoney,BorrowMoney,CouponMoney,AlipayMoney,WeChatMoney,BankMoney,CardMoney,CustShopCode FROM t_ShopSerLog y where 1=1 and y.CreateTime >='2017/7/31 0:00:00' and y.CreateTime <'2017/8/1 0:00:00' ) a  left join t_Cust b on a.CustCode= b.UniqCode    left join t_CustVCard c on a.CustCode= c.CustUniqCode and a.ShopCode=c.ShopCode  where 1=1  and (a.Status=1 or a.ExamineStatus=-1 or a.SaleType=9) and a.DeleFlag=1 and a.ShopCode='a4fc6360-7d6d-41b5-907e-8815e2246e2b'  ;

修改后的语句,代码虽然看着多了,但是速度缺提升了很多

select count(1) from (
select
distinct
a.UniqCode,
a.CustCode,
a.SaleType,
a.TotalMoney,
b.Name as CustName,
c.CardNo,a.ExamineStatus,
IsUser=(select ISNULL(SUM(IsUse),0) from (select IsUse,SerLogCode,[Status] from t_ShopSerElement g where g.SerLogCode=a.UniqCode union all select IsUse,SerLogCode,[Status] from t_GoodsSale h where h.SerLogCode=a.UniqCode union all select 0,SerLogCode,[Status] from t_PackageSalesRecords i where i.SerLogCode=a.UniqCode)s),
DetailsName = stuff((SELECT ',' + Name FROM (select Name,SerLogCode,[Status] from t_ShopSerElement g where g.SerLogCode=a.UniqCode union all select Name,SerLogCode,[Status] from t_GoodsSale g where g.SerLogCode=a.UniqCode union all select CustSerPlanName,SerLogCode,[Status] from t_PackageSalesRecords g where g.SerLogCode=a.UniqCode) AS t FOR xml path('')), 1, 1, ''),
a.CreateTime
from (( SELECT Distinct CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,ExamineStatus,[Status],DeleFlag,CreatorCode,CashMoney,CardGiftMoney,FreeMoney,BorrowMoney,CouponMoney,AlipayMoney,WeChatMoney,BankMoney,CardMoney,CustShopCode FROM t_ShopSerLog y where 1=1 and datediff(DAY,'2017/7/31 0:00:00',y.CreateTime) >=0 and datediff(DAY,'2017/8/1 0:00:00',y.CreateTime) <0 ) a left join (SELECT DISTINCT ShopSerCode,UserCode from t_ShopSerWaiter) d on a.UniqCode=d.ShopSerCode ) left join t_Cust b on a.CustCode= b.UniqCode left join t_CustVCard c on a.CustCode= c.CustUniqCode and a.ShopCode=c.ShopCode where 1=1 and (a.Status=1 or a.ExamineStatus=-1 or a.SaleType=9) and a.DeleFlag=1 and a.ShopCode='a4fc6360-7d6d-41b5-907e-8815e2246e2b' and d.UserCode='e751987e-981e-49ee-a615-c45961ea580b'
UNION

select
distinct
a.UniqCode,
a.CustCode,
a.SaleType,
a.TotalMoney,
b.Name as CustName,
c.CardNo,a.ExamineStatus,
IsUser=(select ISNULL(SUM(IsUse),0) from (select IsUse,SerLogCode,[Status] from t_ShopSerElement g where g.SerLogCode=a.UniqCode union all select IsUse,SerLogCode,[Status] from t_GoodsSale h where h.SerLogCode=a.UniqCode union all select 0,SerLogCode,[Status] from t_PackageSalesRecords i where i.SerLogCode=a.UniqCode)s),
DetailsName = stuff((SELECT ',' + Name FROM (select Name,SerLogCode,[Status] from t_ShopSerElement g where g.SerLogCode=a.UniqCode union all select Name,SerLogCode,[Status] from t_GoodsSale g where g.SerLogCode=a.UniqCode union all select CustSerPlanName,SerLogCode,[Status] from t_PackageSalesRecords g where g.SerLogCode=a.UniqCode) AS t FOR xml path('')), 1, 1, ''),
a.CreateTime
from
( SELECT Distinct CustCode,ShopCode,CreateTime,UniqCode,SaleType,TotalMoney,ExamineStatus,[Status],DeleFlag,CreatorCode,CashMoney,CardGiftMoney,FreeMoney,BorrowMoney,CouponMoney,AlipayMoney,WeChatMoney,BankMoney,CardMoney,CustShopCode FROM t_ShopSerLog y where 1=1 and datediff(DAY,'2017/7/31 0:00:00',y.CreateTime) >=0 and datediff(DAY,'2017/8/1 0:00:00',y.CreateTime) <0 ) a left join t_Cust b on a.CustCode= b.UniqCode left join t_CustVCard c on a.CustCode= c.CustUniqCode and a.ShopCode=c.ShopCode where 1=1 and (a.Status=1 or a.ExamineStatus=-1 or a.SaleType=9) and a.DeleFlag=1 and a.ShopCode='a4fc6360-7d6d-41b5-907e-8815e2246e2b' and a.CreatorCode='e751987e-981e-49ee-a615-c45961ea580b'
)t

有时候union或者union all比左连接查询速度快的更多相关文章

  1. SQL左连接查询 left join ... on

    左连接查询 保留左边主表的所有行(即使在右表没有匹配的行),右表输出满足 on 条件的行,不满足的输出null   示例:组合两个表 - 力扣 表1: Person +--------------+- ...

  2. laravel利用subquery使左连接查询右表数据唯一查询

    如:表a,连接表b,b中有多条符合查询的记录 1.建立需要的子查询 $sub = DB::table('b')->select(['aid'])->selectRaw('max(id) a ...

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

    left join 左连接即以左表为基准,显示坐标所有的行,右表与左表关联的数据会显示,不关联的则不显示.关键字为left join on. **基本用法如下: select table a left ...

  4. EF的左连接查询

    在EF中,当在dbset使用join关联多表查询时,连接查询的表如果没有建立相应的外键关系时,EF生成的SQL语句是inner join(内联),对于inner join,有所了解的同学都知道,很多时 ...

  5. hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法

    1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...

  6. MySQL左连接查询

    1.语法: select 字段列表 from table1 别名1 left join table2 别名2 on 连接条件 [where 子句]

  7. sql左连接查询+右表带有条件的实现

    select * from A表 a left join B表 b on a.id=b.a_id and b.字段='/*条件*/' ; 可查出左表所有数据 select * from A表 a le ...

  8. Linq to Sql 左连接查询

    var query = from t0 in context.ExpressSendMaster join t1 in context.Supplier on t0.SupplierCode equa ...

  9. linq左连接查询加上into后怎么查询右表是否为空

    //判断右表是否为空并为映射表进行赋值标志var query=from q in product join m in favProduct on q.Name equals m.Name into t ...

随机推荐

  1. Autowired(required=true)

    问题原因 没有实现类的接口上添加了@Autowired注解 问题解决 删掉@Autowired注解 bug详情 Description: Field userDAO in com.crab.servi ...

  2. wireshark基础学习—第二部分wireshark的基础操作

    抓取报文: 下载和安装好Wireshark之后,启动Wireshark并且在接口列表中选择接口名,然后开始在此接口上抓包.例如,如果想要在无线网络上抓取流量,点击无线接口.点击Capture Opti ...

  3. Django 的命令及简单例子

     第一步:下载mysql驱动 cmd进入创建好的django项目目录:然后使用下面的命令创建一个项目testdj.  sudo /usr/lib/python3/dist-packages/djang ...

  4. [CentOS_7.4]Linux编译安装ffmpeg

    [CentOS_7.4]Linux编译安装ffmpeg   安装过程: 下载安装源,配置,编译,安装,设置环境变量. # wget http://www.ffmpeg.org/releases/ffm ...

  5. vfile.hpp

    //vov #ifndef VFILE_HPP #define VFILE_HPP #include <cstdio> #include <unistd.h> #include ...

  6. js实现bind方法

    //目标函数 function fun(...args) { console.log(this); console.log(args); } //目标函数原型对象上的一个方法cher func.pro ...

  7. Python 进度条原理

    #进度条原理 import sys,time for i in range(50): sys.stdout.write("#")#标准输出 #若不能够按照时间一个一个依次显示,则代 ...

  8. leetcode刷题——一些算法技巧总结1.0

    运算符优先级,简单记就是:! > 算术运算符 > 关系运算符 > && > || > 赋值运算符 把数字取反,可以作为一种标记 pythonlast = ...

  9. oracle drop 表后 恢复

    1.查看回收站中表 select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recy ...

  10. C语言实验一(2)

    #include<stdio.h>int main(){ char c1,c2; c1=97; c2=98; printf("c1=%c,c2=%c\n",c1,c2) ...