Polardb-X 多存储节点下sql执行计划

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS Agent(兼容OpenClaw),2核4GB
RDS DuckDB + QuickBI 企业套餐,8核32GB + QuickBI 专业版
简介: 在单机上创建polardb-X两存储节点集群,查看执行计划

1 检查现有集群

(venv) [root@my_ob ~]# pxd list

         /root/venv/lib64/python3.6/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.

           from cryptography.hazmat.backends import default_backend

         /root/venv/lib/python3.6/site-packages/deployer

         NAME                          CN        DN        CDC       STATUS

         pxc-tryout                    1         1         1         running

2 清除现有集群

这一步会清楚已创建的所有Polardb-X集群,要谨慎使用。

(venv) [root@my_ob ~]# pxd cleanup

         /root/venv/lib64/python3.6/site-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.

           from cryptography.hazmat.backends import default_backend

         /root/venv/lib/python3.6/site-packages/deployer

         Prepare to delete all PolarDB-X clusters

         All PolarDB-X clusters will be deleted, do you want to continue? [y/N]: y

         Prepare to delete PolarDB-X cluster: pxc-tryout

         stop and remove container: pxc-tryout-cn-bLzM, id: b4b29949a0 at 127.0.0.1

         stop and remove container: pxc-tryout-cdc-LJhR, id: 2fa39a46f4 at 127.0.0.1

         stop and remove container: pxc-tryout-gms-Cand-17077, id: e930288c58 at 127.0.0.1

         stop and remove container: pxc-tryout-dn-0-Cand-14508, id: 1bd0be8eb6 at 127.0.0.1

3 重新创建一个两个DN节点的集群

(venv) [root@my_ob ~]pxd tryout -cn_replica 1 -cn_version latest -dn_replica 2 -dn_version latest -cdc_replica 1 -cdc_version latest

     latest:Pulling from docker.io/polardbx/xstore-tools

     Digest: sha256:b061e1dd57380b455804c091df583708204fa292776e02a0e005d617b59b5d82

     Status: Image is up to date for docker.io/polardbx/xstore-tools:latest

     Processing  [#########---------------------------]   25%    create gms node

     Processing  [############------------------------]   33%    create gms db and tables

     Processing  [###############---------------------]   41%    create PolarDB-X root account

     Processing  [##################------------------]   50%    create dn

     Processing  [#####################---------------]   58%    register dn to gms

     Processing  [########################------------]   66%    create cn

     Processing  [###########################---------]   75%    wait cn ready

     Processing  [##############################------]   83%    create cdc containers

     Processing  [#################################---]   91%    wait PolarDB-X ready

     Processing  [####################################]  100%

     PolarDB-X cluster create successfully, you can try it out now.

     Connect PolarDB-X using the following command:

         mysql -h127.0.0.1 -P11058 -upolardbx_root -pHOxcSLDj

4 连接至集群,用的是obclient客户端,创建数据库,测试表,插入数据

obclient -h127.0.0.1 -P11058 -upolardbx_root -pHOxcSLDj

MySQL [(none)]> show databases;

     +--------------------+

     | DATABASE           |

     +--------------------+

     | information_schema |

     +--------------------+

     1 row in set (0.004 sec)

看一下计算节点状态

MySQL [(none)]> show mpp;

     +------------+------------------+------+--------+

     | ID         | NODE             | ROLE | LEADER |

     +------------+------------------+------+--------+

     | pxc-tryout | 172.17.0.5:11060 | W    | Y      |

     +------------+------------------+------+--------+

     1 row in set (0.007 sec)

看一下存储节点状态,有三个存储节点,一个是gms

MySQL [(none)]> show storage;

     +-----------------+------------------+------------+-----------+----------+-------------+--------+-----------+-------+--------+

     | STORAGE_INST_ID | LEADER_NODE      | IS_HEALTHY | INST_KIND | DB_COUNT | GROUP_COUNT | STATUS | DELETABLE | DELAY | ACTIVE |

     +-----------------+------------------+------------+-----------+----------+-------------+--------+-----------+-------+--------+

     | pxc-tryout-dn-0 | 172.17.0.3:14308 | true       | MASTER    | 1        | 2           | 0      | false     | null  | null   |

     | pxc-tryout-dn-1 | 172.17.0.4:16306 | true       | MASTER    | 1        | 1           | 0      | true      | null  | null   |

     | pxc-tryout-gms  | 172.17.0.2:16456 | true       | META_DB   | 2        | 2           | 0      | false     | null  | null   |

     +-----------------+------------------+------------+-----------+----------+-------------+--------+-----------+-------+--------+

     3 rows in set (0.038 sec)

创建测试库

MySQL [(none)]> create database test partition_mode='partitioning';

     Query OK, 1 row affected (1.280 sec)

进入测试库

MySQL [(none)]> use test;

     Database changed

创建测试表,用hash分区,分区数同计算节点数

MySQL [test]> create table emp (  

 `id` bigint(11) auto_increment NOT NULL,

 `name` varchar(255) DEFAULT NULL,

 `dept_no` int,

 `salary` bigint(10),

 primary key (`id`) ) engine=InnoDB default charset=utf8  

 partition by hash(id)  partitions 2;

 Query OK, 0 rows affected (5.045 sec)

插入四行数据

MySQL [test]> insert into emp(name,dept_no,salary) values ('tom', 1, 3000),('jack', 2, 3100), ('john', 4, 2900),('zhangsan',2,2800);

 Query OK, 4 rows affected (0.382 sec)

 

检查一下数据

MySQL [test]> select * from emp;

 +--------+----------+---------+--------+

 | id     | name     | dept_no | salary |

 +--------+----------+---------+--------+

 | 100001 | tom      |       1 |   3000 |

 | 100004 | zhangsan |       2 |   2800 |

 | 100002 | jack     |       2 |   3100 |

 | 100003 | john     |       4 |   2900 |

 +--------+----------+---------+--------+

 4 rows in set (0.593 sec)

看一下表拓扑,表数据分布到两个节点上

MySQL [test]> show topology from emp;

 +----+-------------------+----------------+----------------+

 | ID | GROUP_NAME        | TABLE_NAME     | PARTITION_NAME |

 +----+-------------------+----------------+----------------+

 |  0 | TEST_P00001_GROUP | emp_kfYc_00001 | p2             |

 |  1 | TEST_P00000_GROUP | emp_kfYc_00000 | p1             |

 +----+-------------------+----------------+----------------+

 2 rows in set (0.008 sec)

5 查看执行计划

MySQL [test]> explain select * from emp;

 +----------------------------------------------------------------------------------------------------------------------+

 | LOGICAL EXECUTIONPLAN                                                                                                |

 +----------------------------------------------------------------------------------------------------------------------+

 | Gather(concurrent=true)                                                                                              |

 |   LogicalView(tables="emp[p1,p2]", shardCount=2, sql="SELECT `id`, `name`, `dept_no`, `salary` FROM `emp` AS `emp`") |

 | HitCache:true                                                                                                        |

 | Source:PLAN_CACHE                                                                                                    |

 | TemplateId: d3a01e16                                                                                                 |

 +----------------------------------------------------------------------------------------------------------------------+

 5 rows in set (0.288 sec)

上面是逻辑执行计划,logicalview下推到存储节点执行,访问了两个分区,在两个分片上执行,然后在计算节点汇总,可以并行执行。

MySQL [test]> explain execute select * from emp;

     +----------+-------------+-------+------------+------+---------------+-----+---------+-----+----------+----------+-------+

     | id       | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows     | filtered | Extra |

     +----------+-------------+-------+------------+------+---------------+-----+---------+-----+----------+----------+-------+

     |         | SIMPLE      | emp   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |         | 100      | NULL  |

     +----------+-------------+-------+------------+------+---------------+-----+---------+-----+----------+----------+-------+

     1 row in set (0.090 sec)

explain execute 选项用来查看物理执行计划,输出上面如上图,物理执行计划是在存储节点执行的,同MySQL的执行计划相同。

MySQL [test]> explain select * from emp where name='zhangsan';

     +-----------------------------------------------------------------------------------------------------------------------------------------+

     | LOGICAL EXECUTIONPLAN                                                                                                                   |

     +-----------------------------------------------------------------------------------------------------------------------------------------+

     | Gather(concurrent=true)                                                                                                                 |

     |   LogicalView(tables="emp[p1,p2]", shardCount=2, sql="SELECT `id`, `name`, `dept_no`, `salary` FROM `emp` AS `emp` WHERE (`name` = ?)") |

     | HitCache:true                                                                                                                           |

     | Source:PLAN_CACHE                                                                                                                       |

     | TemplateId: decd96f4                                                                                                                    |

     +-----------------------------------------------------------------------------------------------------------------------------------------+

     5 rows in set (0.032 sec)

加个where条件删选一下,name列上没有任何索引,也不是主键,仍然需要访问两个存储节点,数据在计算节点汇总。

MySQL [test]> explain select * from emp where id=100001;

     +--------------------------------------------------------------------------------------------------------------------+

     | LOGICAL EXECUTIONPLAN                                                                                              |

     +--------------------------------------------------------------------------------------------------------------------+

     | LogicalView(tables="emp[p1]", sql="SELECT `id`, `name`, `dept_no`, `salary` FROM `emp` AS `emp` WHERE (`id` = ?)") |

     | HitCache:false                                                                                                     |

     | Source:PLAN_CACHE                                                                                                  |

     | TemplateId: f0ccb8c4                                                                                               |

     +--------------------------------------------------------------------------------------------------------------------+

     4 rows in set (0.096 sec)

   

以主键作为查询条件,只需要访问一下分区,计算节点也不需要做汇总,可以看出polardb-x具备水平分表能力,可以提高并发读写能力。

MySQL [test]> explain execute select * from emp where id=100001;

     +----------+-------------+-------+------------+-------+---------------+---------+---------+-------+----------+----------+-------+

     | id       | select_type | table | partitions | type  | possible_keys | key     | key_len | ref   | rows     | filtered | Extra |

     +----------+-------------+-------+------------+-------+---------------+---------+---------+-------+----------+----------+-------+

     |         | SIMPLE      | emp   | NULL       | const | PRIMARY       | PRIMARY | 8       | const |         | 100      | NULL  |

     +----------+-------------+-------+------------+-------+---------------+---------+---------+-------+----------+----------+-------+

     1 row in set (0.011 sec)

节点上执行的物理计划,使用了主键,同MySQL相同。

相关文章
|
SQL 关系型数据库 MySQL
MySQL进阶突击系列(07) 她气鼓鼓递来一条SQL | 怎么看执行计划、SQL怎么优化?
在日常研发工作当中,系统性能优化,从大的方面来看主要涉及基础平台优化、业务系统性能优化、数据库优化。面对数据库优化,除了DBA在集群性能、服务器调优需要投入精力,我们研发需要负责业务SQL执行优化。当业务数据量达到一定规模后,SQL执行效率可能就会出现瓶颈,影响系统业务响应。掌握如何判断SQL执行慢、以及如何分析SQL执行计划、优化SQL的技能,在工作中解决SQL性能问题显得非常关键。
|
存储 容灾 关系型数据库
PolarDB开源数据库进阶课11 激活容灾(Standby)节点
本文介绍了如何激活PolarDB容灾(Standby)节点,实验环境依赖于Docker容器中用loop设备模拟共享存储。通过`pg_ctl promote`命令可以将Standby节点提升为主节点,使其能够接收读写请求。激活后,原Standby节点不能再成为PolarDB集群的Standby节点。建议删除对应的复制槽位以避免WAL文件堆积。相关操作和配置请参考系列文章及视频教程。
352 1
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课2 创建容灾(standby)节点
本文介绍了如何在macOS中搭建PolarDB的容灾(standby)节点,作为“穷鬼玩PolarDB RAC一写多读集群”系列的一部分。基于前一篇通过Docker和loop设备模拟共享存储的经验,本文详细描述了创建虚拟磁盘、启动容器、配置网络、格式化磁盘、备份数据及配置standby节点的具体步骤。
429 0
【YashanDB知识库】使用leading hint调整SQL执行计划后报错YAS-04522 invalid hint leading
【YashanDB知识库】使用leading hint调整SQL执行计划后报错YAS-04522 invalid hint leading
【YashanDB知识库】使用leading hint调整SQL执行计划后报错YAS-04522 invalid hint leading
【YashanDB 知识库】使用 leading hint 调整 SQL 执行计划后报错 YAS-04522 invalid hint leading
在 YashanDB 的所有版本中,使用 leading hint 调整 SQL 执行计划时可能出现“YAS-04522 invalid hint leading”错误,导致 SQL 无法正常执行。原因是 YashanDB 优化器的 Bug。解决方法为避免使用 leading hint。可通过创建测试表 a、b、c 并执行特定 SQL 语句来验证问题是否存在。
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课3 共享存储在线扩容
本文继续探讨穷鬼玩PolarDB RAC一写多读集群系列,介绍如何在线扩容共享存储。实验环境依赖《在Docker容器中用loop设备模拟共享存储》搭建。主要步骤包括:1) 扩容虚拟磁盘;2) 刷新loop设备容量;3) 使用PFS工具进行文件系统扩容;4) 更新数据库实例以识别新空间。通过这些步骤,成功将共享存储从20GB扩容至30GB,并确保所有节点都能使用新的存储空间。
320 1
|
SQL 存储 关系型数据库
【YashanDB知识库】如何从内存中获取SQL语句的执行计划
【YashanDB知识库】如何从内存中获取SQL语句的执行计划
【YashanDB知识库】过期统计信息导致SQL执行计划变差
【YashanDB知识库】过期统计信息导致SQL执行计划变差
【YashanDB知识库】收集分区表统计信息采样率小于1导致SQL执行计划走偏
【YashanDB知识库】收集分区表统计信息采样率小于1导致SQL执行计划走偏
|
SQL Oracle 关系型数据库
Oracle SQL:了解执行计划和性能调优
Oracle SQL:了解执行计划和性能调优
566 1

热门文章

最新文章