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 ...
随机推荐
- 使用js截取路径参数方法
1.根据传入的路径和参数名称截取 export function getUrlParams(href,name) { var reg = new RegExp("(^|\\?|&)& ...
- [OpenCV-Python] 10 图像上的算术运算
文章目录 OpenCV-Python: 核心操作 10 图像上的算术运算 10.1 图像加法 10.2 图像混合 10.3 按位运算 OpenCV-Python: 核心操作 10 图像上的算术运算 目 ...
- 【必知必会的MySQL知识】②使用MySQL
目录 前言 启动MySQL服务 连接MySQL MySQL数据库基本命令 小结 前言 根据上一篇文章[必知必会的MySQL知识]①初探MySQL的内容,想必您对MySQL数据库有了一个整体的了解了,并 ...
- flink之Sink to MySQL和Redis
前言 下面这篇文章是使用Flink的Sink 写出数据到Redis和MySQL Flink之Sink写入Redis和MySQL Flink需要添加Sink的时候,需要自己去添加写Sink,我们可以实现 ...
- java优先队列PriorityQueue
文章目录 前言 PriorityQueue 优先队列 java中优先队列的声明 按优先级排序 常见方法 private void grow(int minCapacity) public boolea ...
- jdk环境配置已经版本切换
下载安装jdk到电脑盘,两个版本要下载到一个盘里面下安装好后右击我的电脑 找到高级系统设置,点开环境变量 新建环境变量 总计环境变量一共添加四个其中 java8和java11是是选择文件路径 JAVA ...
- 【Ubuntu】3.配置下载源与更新
在 Ubuntu 中,更改下载源可以加快下载速度.以下是更改 Ubuntu 下载源的步骤: 方法一: 备份之前的 sources.list 文件: sudo cp /etc/apt/sources.l ...
- 【Python基础】集合的基本使用
Python中的集合是一种无序且唯一的数据结构.集合是通过花括号{}或者set()函数来创建的. 创建集合 s = set() 声明空集合 s = {1,2,3,4,5} 声明非空集合 添加元素 s. ...
- 带大小写忽略的Replace
#region 以下函数用于忽略大小写替换操作 public static string Replace(string Expression, string Find, string Replacem ...
- 2020-10-11:一条sql语句执行时间过长,应该如何优化?从哪些方面进行优化?
福哥答案2020-10-11:#福大大架构师每日一题# 简单回答:执行计划调优.语句调优.索引调优.设计调优.业务调优. 中级回答:时间有限,回答得不全面.1.执行计划调优熟读执行计划,十大参数. 2 ...