首页 > 数据库 >Mysql union all+括号引起的性能问题

Mysql union all+括号引起的性能问题

时间:2023-06-07 16:37:18浏览次数:41  
标签:union lastname SQL 括号 Mysql NULL where id select

SQL如下(MYSQL版本为8.0.17):

select lastname from (select id,loginid,workcode,lastname from HrmResource union all select id,loginid,'' as workcode,lastname from HrmResourceManager)  a   where (id in (83109));

mysql> explain select lastname from (select id,loginid,workcode,lastname from HrmResource union all select id,loginid,'' as workcode,lastname from HrmResourceManager)  a   where (id in (83109));
+----+-------------+--------------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
| id | select_type | table              | partitions | type | possible_keys | key         | key_len | ref   | rows   | filtered | Extra |
+----+-------------+--------------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
|  1 | PRIMARY     | <derived2>         | NULL       | ref  | <auto_key0>   | <auto_key0> | 5       | const |     10 |   100.00 | NULL  |
|  2 | DERIVED     | HrmResource        | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 236351 |   100.00 | NULL  |
|  3 | UNION       | HrmResourceManager | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  |      1 |   100.00 | NULL  |
+----+-------------+--------------------+------------+------+---------------+-------------+---------+-------+--------+----------+-------+
3 rows in set, 1 warning (0.00 sec)

 

两表各自走了全表扫描,未使用ID列索引.

 

将过滤条件前置

select lastname from (select id,loginid,workcode,lastname from HrmResource where id=83109 union all select id,loginid,'' as workcode,lastname from HrmResourceManager where id=83109)  a

mysql> explain select lastname from (select id,loginid,workcode,lastname from HrmResource where id=83109 union all select id,loginid,'' as workcode,lastname from HrmResourceManager where id=83109)  a;
+----+-------------+--------------------+------------+-------+------------------------+-----------------+---------+-------+------+----------+-------+
| id | select_type | table              | partitions | type  | possible_keys          | key             | key_len | ref   | rows | filtered | Extra |
+----+-------------+--------------------+------------+-------+------------------------+-----------------+---------+-------+------+----------+-------+
|  1 | PRIMARY     | <derived2>         | NULL       | ALL   | NULL                   | NULL            | NULL    | NULL  |    3 |   100.00 | NULL  |
|  2 | DERIVED     | HrmResource        | NULL       | const | PRIMARY,HRMRESOURCE_ID | PRIMARY         | 4       | const |    1 |   100.00 | NULL  |
|  3 | UNION       | HrmResourceManager | NULL       | ref   | IX_HRMRSCMGR_ID        | IX_HRMRSCMGR_ID | 5       | const |    1 |   100.00 | NULL  |
+----+-------------+--------------------+------------+-------+------------------------+-----------------+---------+-------+------+----------+-------+
3 rows in set, 1 warning (0.00 sec)

 

此时为正确的执行计划.

 

Oracle 11G我们构建一个类似的SQL,在对比一下执行计划.

select /*+monitor*/ name from (

select deptno,ename name from scott.emp

union all

select deptno,dname name from scott.dept

) a where (deptno in(10));

 

SQL Monitoring Report

SQL Text
------------------------------
select /*+monitor*/ name from ( select deptno,ename name from scott.emp union all select deptno,dname name from scott.dept ) a where (deptno in(10))

Global Information
------------------------------
 Status              :  DONE (ALL ROWS)                
 Instance ID         :  1                              
 Session             :  SYSTEM (1190:29561)            
 SQL ID              :  306qn9tg7hz2x                  
 SQL Execution ID    :  16777216                       
 Execution Started   :  06/07/2023 16:10:28            
 First Refresh Time  :  06/07/2023 16:10:28            
 Last Refresh Time   :  06/07/2023 16:10:28            
 Duration            :  .001467s                       
 Module/Action       :  PL/SQL Developer/SQL ����- ��� 
 Service             :  sharedb                        
 Program             :  plsqldev.exe                   
 Fetch Calls         :  1                              

Global Stats
=================================================
| Elapsed |   Cpu   |  Other   | Fetch | Buffer |
| Time(s) | Time(s) | Waits(s) | Calls |  Gets  |
=================================================
|    0.00 |    0.00 |     0.00 |     1 |      4 |
=================================================

SQL Plan Monitoring Details (Plan Hash Value=758530686)
===========================================================================================================================================
| Id |            Operation            |    Name    |  Rows   | Cost |   Time    | Start  | Execs |   Rows   | Activity | Activity Detail |
|    |                                 |            | (Estim) |      | Active(s) | Active |       | (Actual) |   (%)    |   (# samples)   |
===========================================================================================================================================
|  0 | SELECT STATEMENT                |            |         |      |         1 |     +0 |     1 |        4 |          |                 |
|  1 |   VIEW                          |            |       6 |    3 |         1 |     +0 |     1 |        4 |          |                 |
|  2 |    UNION-ALL                    |            |         |      |         1 |     +0 |     1 |        4 |          |                 |
|  3 |     TABLE ACCESS BY INDEX ROWID | EMP        |       5 |    2 |         1 |     +0 |     1 |        3 |          |                 |
|  4 |      INDEX RANGE SCAN           | IDX_DEPTNO |       5 |    1 |         1 |     +0 |     1 |        3 |          |                 |
|  5 |     TABLE ACCESS BY INDEX ROWID | DEPT       |       1 |    1 |         1 |     +0 |     1 |        1 |          |                 |
|  6 |      INDEX UNIQUE SCAN          | PK_DEPT    |       1 |      |         1 |     +0 |     1 |        1 |          |                 |
===========================================================================================================================================

 

Oracle查询优化器还是比较智能一点,自动做了谓词推进查询优化,生成了最佳的执行计划.

标签:union,lastname,SQL,括号,Mysql,NULL,where,id,select
From: https://www.cnblogs.com/muzisanshi/p/17463748.html

相关文章

  • mysql 慢查询的简要介绍
    如何配置与查看慢查询mysql慢查询相关命令:#查看慢查询是否开启SHOWVARIABLESLIKE'%slow_query_log%'#开启慢查询SETGLOBALslow_query_log='ON'#查看慢查询日志位置SHOWVARIABLESLIKE'%slow_query_log_file%'#查看慢查询阈值,单位:秒SHOWGLOBALVARIABLES......
  • mysql-编写脚本-批量插入数据
    一、代码--报工设置,添加数据set@org_id='租户id';set@created_user='yike';set@updated_user='yike';set@dispatch_list_id=<物料id>;set@process_name='工序名称';set@process_code='工序code';set@code=&......
  • MySQL逻辑架构及执行过程
    一:MySQL逻辑架构1:MySQL逻辑架构客户端进程发送请求后服务器进程对接受的客户端请求做了什么处理,才能产生最后的处理结果呢?下面就看看大致的逻辑架构图上图基本组件介绍:连接层:Connectors【连接器】:MySQL服务之外的客户端程序请求MySQL或进行权限验证......
  • mysql 查询故障排查之字符集的锅
    在查询优化的处理中,一般想到的是在对应关联字段上增加对应索引来加快访问速度.但有时加了索引,即使是唯一索引也无法提高查询速度. descselectxxx;查看对应自行计划,使用了索引,但是对应的耗时却特别的高. descformat=jsonselectxxxx;查看具体的消耗发现在表字段关联的......
  • MySQL基础
    使用命令行窗口链接MySQL数据库。操作示意图。数据库三层结构。1.所谓安装MySQL数据库,就是在主机安装一个数据库管理系统(DBMS),这个管理程序可以管理多个数据库。DBMS(databasemanagesystem)2.一个数据库中可以创建多个表。以保存数据。3.数据库管理系统(DBMS)、数据库和表的关......
  • mysql 常用命令总结
    mysql常用命令总结1、修改mysql密码mysqladmin-u用户名-p旧密码password新密码命令行setpasswordforroot=PASSWORD("root");2、增加mysql新用户并授权:createuser'test'@'localhost'identifiedby'test'grant select,update,delete,inserton库名.......
  • mysql 中添加索引的三种方法
    在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以通过不同的应用场景来进行索引的新建,在此列出三种新建索引的方法mysql中添加索引的三种方法1.1新建表中添加索引①普通索引1234567createtablet_dept(nointnotnullprimarykey,nameva......
  • .net core WebAPI 初探及连接MySQL
    1.前言笔者最近跟着微软官方文档学习.netcoreWebAPI,但发现其对WebAPI连接数据库、读取数据库方面讲得不够细致明了。写此文的目的,即实现.netcoreWebAPI的GET、POST方法访问数据库,并输出结果。2.开发准备2.1操作系统Windows10/Windows72.2.netcoresdk.netcore......
  • MySQL HA 方案 MMM、MHA、MGR、PXC 对比
    ySQL高可用架构MMM(MultiMasterReplicationManager)   资源数量说明主DB2用于主备模式的主主复制从DB0~N台可以根据需要配置N台从服务器IP地址2n+1N为MySQL服务器的数量监控用户1用户监控数据库状态的MySQL用户(replication)代理用户1......
  • MySQL安装及配置
    MySQL安装及配置环境。1.软件下载:本人学习使用的版本是MySQL5.7.19(目前最稳定的)下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-winx64.zip2.添加环境变量:电脑-属性-高级系统设置-环境变量3.手动创建初始化ini文件:在MySQL目录下创建my.ini文件......