有时候union或者union all比左连接查询速度快
原来的语句
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比左连接查询速度快的更多相关文章
- SQL左连接查询 left join ... on
左连接查询 保留左边主表的所有行(即使在右表没有匹配的行),右表输出满足 on 条件的行,不满足的输出null 示例:组合两个表 - 力扣 表1: Person +--------------+- ...
- laravel利用subquery使左连接查询右表数据唯一查询
如:表a,连接表b,b中有多条符合查询的记录 1.建立需要的子查询 $sub = DB::table('b')->select(['aid'])->selectRaw('max(id) a ...
- mysql left join 左连接查询关联n多张表
left join 左连接即以左表为基准,显示坐标所有的行,右表与左表关联的数据会显示,不关联的则不显示.关键字为left join on. **基本用法如下: select table a left ...
- EF的左连接查询
在EF中,当在dbset使用join关联多表查询时,连接查询的表如果没有建立相应的外键关系时,EF生成的SQL语句是inner join(内联),对于inner join,有所了解的同学都知道,很多时 ...
- hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法
1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...
- MySQL左连接查询
1.语法: select 字段列表 from table1 别名1 left join table2 别名2 on 连接条件 [where 子句]
- sql左连接查询+右表带有条件的实现
select * from A表 a left join B表 b on a.id=b.a_id and b.字段='/*条件*/' ; 可查出左表所有数据 select * from A表 a le ...
- Linq to Sql 左连接查询
var query = from t0 in context.ExpressSendMaster join t1 in context.Supplier on t0.SupplierCode equa ...
- linq左连接查询加上into后怎么查询右表是否为空
//判断右表是否为空并为映射表进行赋值标志var query=from q in product join m in favProduct on q.Name equals m.Name into t ...
随机推荐
- Python day 02
基础&运算符 今日概要 循环 字符串格式化 运算符 编码 内容回顾 & 补充 内容回顾 计算机基础 解释器python 2 和 python 3 语法 print input if / ...
- Spring Boot 数据库连接池 HikariCP
简介 HikariCP 来源于日语,「光」的意思,意味着它很快!可靠的数据源,spring boot2.0 已经将 HikariCP 做为了默认的数据源链接池. 官网详细地说明了HikariCP所做的 ...
- oracle数据库查看和解除死锁
查看死锁: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_ ...
- _string
-- 游戏提示字符串设置-- 小技巧:可以修改游戏的自定义表相关提示符 1 |cFFF49B00[世界聊天]|r%s:%s2 |cFFFC5900[登录公告]|r|cFFFBF326欢迎%s登录创世魔 ...
- virtual box centos7 common operation
======= network part =======1.设置桥接模式 2.vi /etc/sysconfig/network-scripts/ifcfg-enp0s3BOOTPROTO=stati ...
- JDBC中execute、executeQuery和executeUpdate的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 1>方法e ...
- iOS坐标转换失败?
使用UIKit的坐标转换方法convertxxx,千万要注意: 一个控件可以转换子控件上的某个点,到另外一个控件上 但是不能转换自己本身的点,到另外一个控件上,否则会数量加倍 所以,一个控件若想转换本 ...
- SQL 基础学习(1):下载DB Browser for SQLite. 下载graphviz(为了使用Rails ERD的前提)出现❌,已debug.
SQL is a standard language for storing, manipulating and retrieving data in databases. 关系型数据库:RDBMS( ...
- linux debug tools
linux modules: IO schedule.VFS.OOM.memory.net.process schedule kernel debug: perf(IO bound\CPU bound ...
- SpringBoot之Java配置
Java配置也是Spring4.0推荐的配置方式,完全可以取代XML的配置方式,也是SpringBoot推荐的方式. Java配置是通过@Configuation和@Bean来实现的: 1.@Conf ...