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 ...
随机推荐
- Django笔记二十四之数据库函数之比较和转换函数
本文首发于公众号:Hunter后端 原文链接:Django笔记二十四之数据库函数之比较和转换函数 这一篇笔记开始介绍几种数据库函数,以下是几种函数及其作用 Cast 转换类型 Coalesce 优先取 ...
- 2023年windows DockerDeskTop最新款4.18.0 全程保姆级安装
目录 前景提示 windows 10 内置的linux系统 1.这个内置系统一定要在windows store里安装,否则,无法使用,这是重点.进入商店,搜索linux. 2.一般画圈这些都可以使用. ...
- node安装node-sass
安装node-sass使用node版本不能太高,否则会报错 checking for Python executable "C:\Program Files\python" in ...
- GDB使用简单总结
简单总结常用gdb调试命令 不长篇讨论gdb是什么,或者怎么使用了,因为网上很多都讲的比较详细,以下只是做个备录,经常使用的命令,偶尔不用容易忘记! 1.set args xxxx (xxx为参数) ...
- Python 函数及参数的使用
函数 带名字的代码块,用于完成具体的工作 关键字def定义一个函数,定义函数名,括号内是需要完成任务所需要的信息,最后定义冒号结尾 缩进构成函数体 函数调用,依次指定函数名以及冒号括起来的必要信息 d ...
- 一文彻底搞懂ZAB算法,看这篇就够了!!!
最近需要设计一个分布式系统,需要一个中间件来存储共享的信息,来保证多个系统之间的数据一致性,调研了两个主流框架Zookeeper和ETCD,发现都能满足我们的系统需求.其中ETCD是K8s中采用的分布 ...
- 2022-09-29:在第 1 天,有一个人发现了一个秘密。 给你一个整数 delay ,表示每个人会在发现秘密后的 delay 天之后, 每天 给一个新的人 分享 秘密。 同时给你一个整数 forg
2022-09-29:在第 1 天,有一个人发现了一个秘密. 给你一个整数 delay ,表示每个人会在发现秘密后的 delay 天之后, 每天 给一个新的人 分享 秘密. 同时给你一个整数 forg ...
- 2022-09-10:以下go语言代码输出什么?A:编译错误;B:49.0;C:49。 package main import ( “fmt“ ) func main() { ch
2022-09-10:以下go语言代码输出什么?A:编译错误:B:49.0:C:49. package main import ( "fmt" ) func main() { ch ...
- 2020-10-29:使用redis实现分布式限流组件,要求高并发场景同一IP一分钟内只能访问100次,超过限制返回异常,写出实现思路或伪代码均可。
福哥答案2020-10-29: 简单回答:固定窗口:string.key存ip,value存次数.滑动窗口:list.key存ip,value=list,存每次访问的时间. 中级回答:固定窗口:用re ...
- \n被当成回车处理
Regex.Escape C# 字符串变量str 的值为"a\nb"如果直接输出显示的话,就成了:ab需要输出显示为:a\nb string str = "a\nb& ...