利用窗口函数
MySQL 8中新增的窗口函数使得原来麻烦的去重操作变得很简单。
truncate t_target;
insert into t_target
select item_id, created_time, modified_time, item_name, other
from (select *, row_number(http://www.amjmh.com/v/) over(partition by created_time,item_name) as rn
from t_source) t where rn=1;
这个语句执行只需要12秒,而且写法清晰易懂,其查询计划如下:
mysql> explain select item_id, created_time, modified_time, item_name, other
-> from (select *, row_number() over(partition by created_time,item_name) as rn
-> from t_source) t where rn=1;
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
| 1 | PRIMARY | <derived2> | NULL | ref | <auto_key0> | <auto_key0> | 8 | const | 10 | 100.00 | NULL |
| 2 | DERIVED | t_source | NULL | ALL | NULL | NULL | NULL | NULL | 997281 | 100.00 | Using filesort |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
2 rows in set, 2 warnings (0.00 sec)
该查询对t_source表进行了一次全表扫描,同时用filesort对表按分区字段created_time、item_name进行了排序。外层查询从每个分区中保留一条数据。因为重复created_time和item_name的多条数据中可以保留任意一条,所以oevr中不需要使用order by子句。
从执行计划看,窗口函数去重语句似乎没有消除嵌套查询的变量去重好,但此方法实际执行是最快的。
相关文章
- 逆向 stdio.h 函数库 fseek 函数(调试版本)
- OpenCV2学习笔记(十五):利用Cmake高速查找OpenCV函数源代码
- mysql中json_object函数的使用?
- Matlab中min函数的使用
- Matlab中waitfor函数用法
- 地球引擎中级教程——如何计算每个类中像素的面积。使用该area()函数计算特征的面积?
- Google Earth Engine——如何下载里利用evaluate函数和for循环叠加使用遍历所有单景影像并导出(ui.button)
- Google Earth Engine(GEE)——利用光谱距离函数实现滑坡监测以sentinel-2数据为例
- python中wraps函数用法详情
- SQL 函数
- Oracle数据库:oracle组函数,聚合函数,多行函数,avg,sum,min,max,count,group by,having
- 利用OpenCV的函数HoughCircles()实现霍夫梯度法圆检测时参数dp的值对于最终结果影响是挺大的
- 利用OpenCV的函数mean()和meanStdDev()计算图像的均值和标准差
- 利用OpenCV的函数warpAffine()做图像的仿射变换【实现图像的平移、缩放、旋转、翻转等操作】
- 利用OpenCV的函数compareHist()对图像的直方图进行相似性比较
- 利用OpenCV的函数threshold()对图像作基于OTSU的二值阈值化处理-并附比较好的介绍OTSU原理的博文链接
- 利用keras搭建神经网络拟合非线性函数
- 用substr()函数高效的输出一个字符串的所用子串
- 常见损失函数
- hash函数的选择
- 浅谈JS函数节流及应用场景
- HMAC是一种利用哈希函数构造消息认证码的方法
- 共享内存:mmap函数实现
- Lua的API函数