有时候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 ...
随机推荐
- AI illustrator 如何裁剪图片(扣取局部区域)
AI如何裁剪图片 在使用illustrator的过程中,很多亲想要AI的裁剪功能与ps一样强大,但是AI常用的裁剪图片方法如下. 工具/原料 illustrator cs6 应用剪切蒙版剪切图片 ...
- SPOJ 10570 LONGCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成多组数据就有三倍经验了 代码 #include <cstdio> #inclu ...
- 初学者易上手的SSH-spring 01控制反转(IOC)
这章开始学习SSH中最后的一个框架spring.Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用. 首先就来学习一下I ...
- vue试题
1.v-for可是实现数据遍历显示,不仅可以遍历数组,也可以遍历对象,还可以从数值中取值. 2.vue的生命周期钩子 1)实例,组件通过new Vue()创建出来之后会初始化事件和生命周期,然后就会执 ...
- MySQL优化查询 5.7版本
1. 变更参数 : query_cache_type 如果何配置查询缓存: query_cache_type 这个系统变量控制着查询缓存工能的开启的关闭.query_cache_type=0时表示关闭 ...
- python---- pyqt 十字光标
# encoding: UTF-8 import sys,os import pyqtgraph as pg import datetime as dt import numpy as np impo ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 自定义分页
jQuery EasyUI 数据网格 - 自定义分页 数据网格(datagrid)内置一个很好特性的分页功能,自定义也相当简单.在本教程中,我们将创建一个数据网格(datagrid),并在分页工具栏上 ...
- volley+okhttp封装,一行代码就可访问网络
volley+okhttp封装,一行代码就可访问网络 前言: 我写这个可能不会完全的教会你去怎么封装一个网络框架,我的目的在于让你不需要你自己封装就可以访问网络,或者说互相学习也可以,我只是想让你大概 ...
- Android从启动到程序运行整个过程的整理
1Android是基于Linux的一个操作系统,它可以分为五层,下面是它的层次架构图,可以记一下,因为后面应该会总结到SystemServer这些Application Framework层的东西 A ...
- LCSS最长公共子序列算法
0.论文基本介绍以及相关内容 分析移动用户位置的相似性,提取移动用户的相似路径在出行路径预测.兴趣区域发现.轨迹聚类.个性化路径推荐等领域具有广泛的应用. 重点:利用移动用户定位数据找到合适轨迹的表示 ...