sql中当关联查询主表很大影响查询速度时怎么办?
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中当关联查询主表很大影响查询速度时怎么办?的更多相关文章
- elasticsearch的store属性跟_source字段——如果你的文档长度很长,存储了_source,从_source中获取field的代价很大,你可以显式的将某些field的store属性设置为yes,否则设置为no
转自:http://kangrui.iteye.com/blog/2262860 众所周知_source字段存储的是索引的原始内容,那store属性的设置是为何呢?es为什么要把store的默认取值设 ...
- SQL中的关联更新和关联删除
在SQL中,经常用到关联查询,比如select a.* from A a inner join B b on a.PId=b.FId where 条件,SQL中也支持类似的关联更新和关联删除. 关联更 ...
- SQL中以count及sum为条件的查询
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- SQL中以count或sum为条件的查询方式
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- 川普和习G-20会面为缓和中美贸易战提供了很大的机会
川普和习将于这周在Buenos Aires(阿根廷首都)会面,互相商讨虚弱经济全球化的最大威胁. 自从川普在今年七月第一次开始提高中国商品关税之后,对全球的投资者和逐渐削弱的经济活动来说,两位领导人可 ...
- sql中对查询出来的数据进行分页
当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...
- Spark SQL中的Catalyst 的工作机制
Spark SQL中的Catalyst 的工作机制 答:不管是SQL.Hive SQL还是DataFrame.Dataset触发Action Job的时候,都会经过解析变成unresolved的逻 ...
- SQL中Truncate的用法(转)
转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...
- SQL 中的 IFNULL和NULLIF
sql 中的IFNULL和NULLIF很容易混淆,在此记录一下. IFNULL IFNULL(expression1, expression2) 如果expression1为null, 在函数返回ex ...
- SQL中DateTime转换成Varchar样式
SQL中DateTime转换成Varchar样式语句及查询结果:Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect ...
随机推荐
- 景顺长城基于 Apache APISIX 在金融云原生的生产实践
本文介绍了景顺长城在金融云原生架构演进中选择 APISIX 作为网关工具的技术细节,同时分享了使用 APISIX 的实践细节,并对 APISIX 的未来展望进行了探讨. 作者李奕浩,景顺长城信息技术部 ...
- k8s介绍与常用命令
kubernetes基础与常用命令 原文地址 https://blog.csdn.net/footless_bird/article/details/125798691 官方文档 https://ku ...
- .Net8的快速JIT,分层编译,R2R的设置
前言 本篇通过一些简单的JIT设置,比如快速JIT,适用于循环的快速 JIT,分层编译,R2R等核心内容设置,快速进入.Net8核心区域. 概括 1.快速JIT 什么是快速JIT,顾名思义,被Rosy ...
- ToF技术全解读
文章目录 ToF技术全解读 1. 什么是ToF 2. ToF的原理 3. ToF 优缺点 ToF技术全解读 1. 什么是ToF ToF: Time of flight. 飞行时间.当然这只是一种翻译的 ...
- 如何实现一个sync.Once
sync.Once 是 golang里用来实现单例的同步原语.Once 常常用来初始化单例资源, 或者并发访问只需初始化一次的共享资源,或者在测试的时候初始化一次测试资源. 单例,就是某个资源或者对象 ...
- 当我再次用Kotlin完成五年前已经通过Kotlin完成的项目后
> 近日来对Kotlin的使用频率越来越高, 也对自己近年来写过的Kotlin代码尝试进行一个简单的整理. 翻到了自己五年前第一次使用Kotlin来完成的一个项目([贝塞尔曲线](https ...
- 2021-01-29:redis同步机制是怎样的?
福哥答案2021-01-30: [答案1:](https://italk.mashibing.com/question/detail/ques_00006009)全量同步master服务器会开启一个后 ...
- vue全家桶进阶之路44:Vue3 Element Plus el_row和el_col组件
在 Vue 3 中,Element Plus 也提供了 ElRow 和 ElCol 组件,用于实现栅格布局. ElRow 组件的常用属性: gutter:栅格间距,默认为 0. type:布局模式,可 ...
- ToolStrip1
Private Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles Too ...
- 合合信息亮相CCIG2023:多位大咖共话智能文档未来,文档图像内容安全还面临哪些技术难题?
近日,中国图象图形大会(CCIG 2023)(简称"大会")在苏州圆满落幕.本届大会以"图象图形·向未来"为主题,由中国科学技术协会指导,中国图象图形学学会 ...