sql中当关联查询主表很大时,直接关联,查询速度会较慢,这时可以先利用子查询经筛选条件筛除一部数据,这样主连接表体量减少,这样能一定程度加快速度。

(1)常规join -- 最慢7.558s

select
A.commodity_id,
B.commodity_name,
B.bar_code,
sum(A.sell_quantity) as sellAmount,
sum(A.sell_amount) as sellingPrice,
sum(A.sell_amount)-sum(A.profit) as purchasePrice,
sum(A.profit) as profit,
sum(A.profit)/sum(A.sell_amount)*100 as profitRate
from t_commodity_daily_sales A
join t_commodity B on A.commodity_id = B.commodity_id
where A.shop_id in (select son_shop_id from t_shop_ship where main_shop_id = 133 and son_shop_id != main_shop_id)
and A.create_time >= 1564588800000 and A.create_time < 1572364800000
group by A.commodity_id
order by A.commodity_id

(2)子查询作为join表 -- 其次6.446s

select
A.commodity_id,
A.commodity_name,
A.bar_code,
sum(B.sell_quantity) as sellAmount,
sum(B.sell_amount) as sellingPrice,
sum(B.sell_amount)-sum(B.profit) as purchasePrice,
sum(B.profit) as profit,
sum(B.profit)/sum(B.sell_amount)*100 as profitRate
from t_commodity A
join
(
select commodity_id,sell_quantity,sell_amount,profit
from t_commodity_daily_sales
where shop_id in(select son_shop_id from t_shop_ship where main_shop_id = 133 and son_shop_id != main_shop_id)
and create_time >= 1564588800000 and create_time < 1572364800000
)B on A.commodity_id = B.commodity_id
group by A.commodity_id
order by A.commodity_id

(3)子查询作为连接主表 -- 最快6.402s

select
A.commodity_id,
B.commodity_name,
B.bar_code,
sum(A.sell_quantity) as sellAmount,
sum(A.sell_amount) as sellingPrice,
sum(A.sell_amount)-sum(A.profit) as purchasePrice,
sum(A.profit) as profit,
sum(A.profit)/sum(A.sell_amount)*100 as profitRate
from (
select commodity_id,sell_quantity,sell_amount,profit
from t_commodity_daily_sales
where shop_id in (select son_shop_id from t_shop_ship where main_shop_id = 133 and son_shop_id != main_shop_id)
and create_time >= 1564588800000 and create_time < 1572364800000
)A
join t_commodity B on A.commodity_id = B.commodity_id
group by A.commodity_id
order by A.commodity_id

sql中当关联查询主表很大影响查询速度时怎么办?的更多相关文章

  1. elasticsearch的store属性跟_source字段——如果你的文档长度很长,存储了_source,从_source中获取field的代价很大,你可以显式的将某些field的store属性设置为yes,否则设置为no

    转自:http://kangrui.iteye.com/blog/2262860 众所周知_source字段存储的是索引的原始内容,那store属性的设置是为何呢?es为什么要把store的默认取值设 ...

  2. SQL中的关联更新和关联删除

    在SQL中,经常用到关联查询,比如select a.* from A a inner join B b on a.PId=b.FId where 条件,SQL中也支持类似的关联更新和关联删除. 关联更 ...

  3. SQL中以count及sum为条件的查询

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  4. SQL中以count或sum为条件的查询方式

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  5. 川普和习G-20会面为缓和中美贸易战提供了很大的机会

    川普和习将于这周在Buenos Aires(阿根廷首都)会面,互相商讨虚弱经济全球化的最大威胁. 自从川普在今年七月第一次开始提高中国商品关税之后,对全球的投资者和逐渐削弱的经济活动来说,两位领导人可 ...

  6. sql中对查询出来的数据进行分页

    当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...

  7. Spark SQL中的Catalyst 的工作机制

      Spark SQL中的Catalyst 的工作机制 答:不管是SQL.Hive SQL还是DataFrame.Dataset触发Action Job的时候,都会经过解析变成unresolved的逻 ...

  8. SQL中Truncate的用法(转)

    转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...

  9. SQL 中的 IFNULL和NULLIF

    sql 中的IFNULL和NULLIF很容易混淆,在此记录一下. IFNULL IFNULL(expression1, expression2) 如果expression1为null, 在函数返回ex ...

  10. SQL中DateTime转换成Varchar样式

    SQL中DateTime转换成Varchar样式语句及查询结果:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect ...

随机推荐

  1. SpringCloud Gateway 3.x 响应头添加 Skywalking TraceId

    在微服务架构中,一次请求可能会被多个服务处理,而每个服务又会产生相应的日志,且每个服务也会有多个实例.在这种情况下,如果系统发生异常,没有 Trace ID,那么在进行日志分析和追踪时就会非常困难,因 ...

  2. java POI创建HSSFWorkbook工作簿

    1. POI Apache POI 是基于 Office Open XML 标准(OOXML)和 Microsoft 的 OLE 2 复合文档格式(OLE2)处理各种文件格式的开源项目. 2. 样式设 ...

  3. 用Aspose-Java免费实现 PDF、Word、Excel、Word互相转换并将转换过得文件上传OSS,返回转换后的文件路径

    嘿嘿嘿.嘿嘿,俺又回来了! github代码地址 https://github.com/Tom-shushu/work-study 接口文档有道云 https://note.youdao.com/s/ ...

  4. SqlParameter的作用与用法

    有时候为图方便,会直接用sqlhelper文件进行相关操作,会出现如下的类: public static object ExecuteScalar(string sqlStr, params SqlP ...

  5. 关于前端vue打包项目以及静态网站部署项目到阿里云ECS云服务器初学简单教程

    准备工作: 1.首先进入https://ecs.console.aliyun.com/ 领取或者购买一台简单的ECS云服务器. 进入网站注册登录后拉到页面最下面或者顶部搜索免费云服务器领取立即试用 , ...

  6. 2022-11-06:给定平面上n个点,x和y坐标都是整数, 找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的。 返回最短距离,精确到小数点后面4位。

    2022-11-06:给定平面上n个点,x和y坐标都是整数, 找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的. 返回最短距离,精确到小数点后面4位. 答案2022-11- ...

  7. 2022-01-10:路径交叉。给你一个整数数组 distance 。 从 X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南

    2022-01-10:路径交叉.给你一个整数数组 distance . 从 X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南 ...

  8. Element Cascader 级联选择器去除空叶子节点

    此处以后端获取部门级联List为例 以下为数据结构 { data: { children: [ 0:{childre:[ 0:{}, 1:{} ]}, 1:{}, 2:{}, 3:{}, 4:{}, ...

  9. 云服务器挂载easyconnect

    参考:easyconnect命令版地址 https://github.com/Hagb/docker-easyconnect 前提条件: 云服务器(以阿里云服务器 Centos7.6 为例) 用途: ...

  10. WPF 入门笔记 - 02 - 布局综合应用

    本篇博文对接上篇末尾处WPF常用布局控件的综合应用,为痕迹g布局控件介绍课后作业的一个思路方法. 前言 首先来谈一谈布局原则: WPF窗口只能包含一个元素(Window元素属于内容控件,内容控件只允许 ...