表连接
truncate t_target;
insert into t_target
select distinct t1.* from t_source t1,
(select min(item_id) item_id,created_time,item_name from t_source group by created_time,item_name) t2
where t1.item_id = t2.item_id;
本次用时13秒,查询计划如下:
mysql> explain select distinct t1.* from t_source t1,
-> (select min(item_id) item_id,created_time,item_name from t_source group by created_time,item_name) t2
-> where t1.item_id = t2.item_id;
+----+-------------+------------+------------+-------+---------------+-------------+---------+-----------------+--------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+---------------+-------------+---------+-----------------+--------+----------+------------------------------+
| 1 | PRIMARY | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 997281 | 100.00 | Using where; Using temporary |
| 1 | PRIMARY | <derived2> | NULL | ref | <auto_key0> | <auto_key0> | 5 | test.t1.item_id | 10 | 100.00 | Distinct |
| 2 | DERIVED | t_source | NULL | index | idx_sort | idx_sort | 94 | NULL | 997281 | 100.00 | Using index |
+----+-------------+------------+------------+-------+---------------+-------------+---------+-----------------+--------+----------+------------------------------+
3 rows in set, 1 warning (0.00 sec)
和没有索引相比,子查询虽然从全表扫描变为了全索引扫描,但还是需要扫描100万行记录。因此查询性能提升并不是明显。
---------------------
相关文章
- C 语言字符串连接的 3种方式
- Mac 解决数据库连接失败的问题汇总
- 【IT运维】公司内网服务器可以远程桌面连接吗?怎么连接?
- python mysql-connector模块连接数据库详解
- Win11此站点的连接不安全解决教程
- QT信号与槽的6种连接方式以及自定义参数传递
- WebSocket 是什么原理?为什么可以实现持久连接?
- 《机器学习与数据科学(基于R的统计学习方法)》——第2章 连接数据
- c#连接Redis Redis的安装与配置
- hive——left join表连接,通常用到求“在1表但是不在2表的用户?”
- 解决Navicat连接MySQL出现1251-Client does not support authentication protocol requested by server;
- MAC篇 超级简单的连接远程主机方法(永久记住)
- Linux下SSH远程连接断开后让程序继续运行解决办法
- C# 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化