open Gauss 数据库-03 openGauss数据库维护管理指导手册

发文章是为了证明自己真的掌握了一个知识,同时给他人带来帮助,如有问题,欢迎指正,祝大家万事胜意!

目录

前言

openGauss数据库维护管理

1 操作系统参数检查

1.1 实验介绍

1.2 场景设置及操作步骤

2 openGauss 运行健康状态检查

2.1 实验介绍

2.2 场景设置及操作步骤

3 数据库性能检查

3.1 实验介绍

3.2 通过 gs_checkperf 工具来检查数据库性能

3.3 通过 EXPLAIN 进行 SQL 语句优化

4 日志检查

4.1 实验介绍

4.2 通过 gs_collector 工具来收集日志信息

5 最大连接数设置

5.1 实验介绍

5.2 场景设置及操作步骤

6 例行表、索引的维护

6.1 实验介绍

6.2 场景设置及操作步骤

前言

本实验主要内容为操作系统参数检查、 openGauss 健康状态检查、数据库性能检查、日志检查
和清理、时间一致性检查、应用连接数检查、例行维护表等
我的环境:
设备名称设备型号软件版本
虚拟机VMwareVMware-workstation-full-17.5.1
操作系统openEuler   openEuler 22.3LTS
数据库openGauss  openGauss 5.0.0

 需要的工具,大家不用现在下,后面用到了再下也可以,如果需要相关文件,可以评论,其实大多数都是可以去官网下的哈,因为我只能通过网盘给大家,文件又有点大,网盘的速度大家都是清楚的哈哈,所以还是推荐大家去官网,如果实在找不到可以找我

openGauss数据库维护管理

1 操作系统参数检查

1.1 实验介绍

gs_checkos 工具用来帮助检查操作系统、控制参数、磁盘配置等内容,并对系统控制参数、 I/O
配置、网络配置和 THP 服务等信息进行配置。
本实验主要是通过 gs_checkos 工具来检查操作系统参数设置是否合理。先进行场景设置,然后
根据检查结果进行参数调整。

1.2 场景设置及操作步骤

 步骤 1 ROOT 用户登录装有 openGauss 数据库服务的操作系统,登录后信息如下:

Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 03月 25日 星期一 19:09:30 CST

System load: 	0.02
Processes: 	195
Memory used: 	39.3%
Swap used: 	13.3%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	1

 步骤 2 ROOT 用户下执行 gs_checkos 先对系统参数进行检查。

[root@node0 ~]# gs_checkos -i A
Checking items:
    A1. [ OS version status ]                                   : Normal
    A2. [ Kernel version status ]                               : Normal
    A3. [ Unicode status ]                                      : Normal
    A4. [ Time zone status ]                                    : Normal
    A5. [ Swap memory status ]                                  : Warning
    A6. [ System control parameters status ]                    : Warning
    A7. [ File system configuration status ]                    : Normal
    A8. [ Disk configuration status ]                           : Normal
    A9. [ Pre-read block size status ]                          : Normal
BondMode Null
    A11.[ Network card configuration status ]                   : Normal
    A12.[ Time consistency status ]                             : Warning
    A13.[ Firewall service status ]                             : Normal
    A14.[ THP service status ]                                  : Normal
Total numbers:13. Abnormal numbers:0. Warning numbers:3.

2 openGauss 运行健康状态检查

2.1 实验介绍

gs_check 能够帮助用户在 openGauss 运行过程中,全量的检查 openGauss 运行环境,操作系
统环境,网络环境及数据库执行环境,也有助于在 openGauss 重大操作之前对各类环境进行全
面检查,有效保证操作执行成功。
本实验主要是通过 gs_check 工具来检查 openGauss 数据库运行状态。先进行场景设置,然后
根据检查结果进行数据库调整。

2.2 场景设置及操作步骤

步骤 1 ROOT 用户登录装有 openGauss 数据库服务的操作系统然后用 su – omm 命令切换至
OMM 用户环境,登录后信息如下。
[root@node0 ~]# su - omm
Last login: Mon Mar 25 18:47:31 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 03月 25日 星期一 19:31:35 CST

System load: 	0.30
Processes: 	195
Memory used: 	39.0%
Swap used: 	14.3%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 确认 openGauss 数据库服务是否启动。

[omm@node0 ~]$ gs_om -t status
-----------------------------------------------------------------------

cluster_name    : dbCluster
cluster_state   : Normal
redistributing  : No

-----------------------------------------------------------------------
cluster_state : Normal 表示已启动,可以正常使用。如果状态为非 Normal 表示不可用
为了实验场景设置,如果数据库服务已经启动,请执行步骤 3 先关闭服务。
步骤 3 关闭 openGauss 数据库服务。
[omm@node0 ~]$ gs_om -t stop
Stopping cluster.
=========================================
Successfully stopped cluster.
=========================================
End stop cluster.
步骤 4 检查 openGauss 实例连接。
[omm@node0 ~]$ gs_check -i CheckDBConnection -L

2024-03-25 19:37:53 [NAM] CheckDBConnection
2024-03-25 19:37:53 [STD] 检查能否连接数据库,如果连接成功则检查项通过,否则检查项不通过
2024-03-25 19:37:53 [RST] NG
The database can not be connected.
2024-03-25 19:37:53 [RAW]
说明:
CheckDBConnection...........................NG 表示连接检查项无用;
The database can not be connected. 表示实例不能连接;
Failed. All check items run completed. Total:1 NG:1 表示共检查 1 项并且检查结果未通过。

步骤 5 启动 openGauss 数据库服务。

[omm@node0 ~]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] node0
......
=========================================
Successfully started.
步骤 6 确认 openGauss 数据库服务已启动。
[omm@node0 ~]$ gs_om -t status;
-----------------------------------------------------------------------

cluster_name    : dbCluster
cluster_state   : Normal
redistributing  : No

-----------------------------------------------------------------------
步骤 7 再次检查 openGauss 实例连接。
[omm@node0 ~]$ gs_check -i CheckDBConnection -L

2024-03-25 19:46:27 [NAM] CheckDBConnection
2024-03-25 19:46:27 [STD] 检查能否连接数据库,如果连接成功则检查项通过,否则检查项不通过
2024-03-25 19:46:27 [RST] OK
The database connection is normal.
2024-03-25 19:46:27 [RAW]

source '/home/omm/.bashrc' && gsql -m -d postgres -p 15400 -c 'select pg_sleep(1);'

说明:
CheckDBConnection...........................OK 表示连接检查项正常;
Success. All check items run completed. Total:1 Success:1 表示共检查 1 项并且检查结
果成功。
openGauss 数据库运行健康状态检查实验结束

3 数据库性能检查

3.1 实验介绍

openGauss 提供了 gs_checkperf 工具来帮助用户了解 openGauss 的负载情况。
本实验主要是通过 gs_checkperf 工具来检查 openGauss 数据库性能以及通过 EXPLAIN 来进行
SQL 语句优化。

3.2 通过 gs_checkperf 工具来检查数据库性能

说明:
gs_checkperf 可以对以下级别进行检查:
  openGauss 级别(主机 CPU 占用率、 Gauss CPU 占用率、 I/O 使用情况等)、
  节点级别( CPU 使用情况、内存使用情况、 I/O 使用情况)、
  会话 / 进程级别( CPU 使用情况、内存使用情况、 I/O 使用情况)、
  SSD 性能(写入、读取性能)
其中检查 SSD 性能要用 root 用户执行,检查 openGauss 性能要用 openGauss 安装用户执行
本实验为检查 openGauss 性能。
步骤 1 ROOT 用户登录装有 openGauss 数据库服务的操作系统然后用 su – omm 命令切换至
OMM 用户环境,登录后信息如下。
[root@node0 ~]# su - omm
Last login: Mon Mar 25 18:47:31 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 03月 25日 星期一 19:31:35 CST

System load: 	0.30
Processes: 	195
Memory used: 	39.0%
Swap used: 	14.3%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 先启动数据库服务,再用 gs_checkperf 检查下,再使用 gsql 客户端以管理员用户身份连接
postgres 数据库,假设端口号为 15400
先启动数据库服务。
[omm@node0 ~]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] node0:
[2024-03-25 19:53:25.112][42128][][gs_ctl]: gs_ctl started,datadir is /opt/huawei/install/data/dn 
[2024-03-25 19:53:25.129][42128][][gs_ctl]:  another server might be running; Please use the restart command
=========================================
Successfully started.
gs_checkperf 检查下。
[omm@node0 ~]$ gs_checkperf
Cluster statistics information:
    Host CPU busy time ratio                     :    1.79       %
    MPPDB CPU time % in busy time                :    8.68       %
    Shared Buffer Hit ratio                      :    99.56      %
    In-memory sort ratio                         :    0
    Physical Reads                               :    759
    Physical Writes                              :    124
    DB size                                      :    50         MB
    Total Physical writes                        :    124
    Active SQL count                             :    4
    Session count                                :    8
然后连接 postgres 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

步骤 3 PMK 模式下的表进行统计信息收集。
openGauss=#  analyze pmk.pmk_configuration;
ANALYZE
openGauss=#  analyze pmk.pmk_meta_data;
ANALYZE
openGauss=#  analyze pmk.pmk_snapshot;
ANALYZE
openGauss=#  analyze pmk.pmk_snapshot_datanode_stat;
ANALYZE
gs_checkperf 工具的监控信息依赖于 pmk 模式下的表的数据,如果 pmk 模式下的表未执
analyze 操作,则可能导致 gs_checkperf 工具执行失败。
步骤 4 执行简要性能检查。
\q 先退出 postgres 数据库,然后在操作系统用户 omm 环境下去执行 gs_checkperf 检查
工具,具体如下:
openGauss=# \q
[omm@node0 ~]$ gs_checkperf
Cluster statistics information:
    Host CPU busy time ratio                     :    2.02       %
    MPPDB CPU time % in busy time                :    16.25      %
    Shared Buffer Hit ratio                      :    99.67      %
    In-memory sort ratio                         :    0
    Physical Reads                               :    777
    Physical Writes                              :    335
    DB size                                      :    51         MB
    Total Physical writes                        :    335
    Active SQL count                             :    4
    Session count                                :    8
步骤 5 执行详细性能检查。
[omm@node0 ~]$ gs_checkperf --detail
Cluster statistics information:
Host CPU usage rate:
    Host total CPU time                          :    26998790.000 Jiffies
    Host CPU busy time                           :    594830.000 Jiffies
    Host CPU iowait time                         :    5920.000   Jiffies
    Host CPU busy time ratio                     :    2.20       %
    Host CPU iowait time ratio                   :    .02        %
MPPDB CPU usage rate:
    MPPDB CPU time % in busy time                :    16.57      %
    MPPDB CPU time % in total time               :    .37        %
Shared buffer hit rate:
    Shared Buffer Reads                          :    1232
    Shared Buffer Hits                           :    430218
    Shared Buffer Hit ratio                      :    99.71      %
In memory sort rate:
    In-memory sort count                         :    0
    In-disk sort count                           :    0
    In-memory sort ratio                         :    0
I/O usage:
    Number of files                              :    121
    Physical Reads                               :    785
    Physical Writes                              :    492
    Read Time                                    :    168726     ms
    Write Time                                   :    5694       ms
Disk usage:
    DB size                                      :    51         MB
    Total Physical writes                        :    492
    Average Physical write                       :    86406.74
    Maximum Physical write                       :    492
Activity statistics:
    Active SQL count                             :    4
    Session count                                :    8
Node statistics information:
dn_6001:
    MPPDB CPU Time                               :    98590      Jiffies
    Host CPU Busy Time                           :    594830     Jiffies
    Host CPU Total Time                          :    26998790   Jiffies
    MPPDB CPU Time % in Busy Time                :    16.57      %
    MPPDB CPU Time % in Total Time               :    .37        %
    Physical memory                              :    1497370624 Bytes
    DB Memory usage                              :    5200146432 Bytes
    Shared buffer size                           :    284164096  Bytes
    Shared buffer hit ratio                      :    99.71      %
    Sorts in memory                              :    0
    Sorts in disk                                :    0
    In-memory sort ratio                         :    0
    Number of files                              :    121
    Physical Reads                               :    785
    Physical Writes                              :    492
    Read Time                                    :    168726
    Write Time                                   :    5694
Session statistics information(Top 10):
Session CPU statistics:
1 dn_6001-postgres-omm:
    Session CPU time                             :    3
    Database CPU time                            :    98680
    Session CPU time %                           :    0.00       %
2 dn_6001-postgres-omm:
    Session CPU time                             :    0
    Database CPU time                            :    98680
    Session CPU time %                           :    0.00       %
3 dn_6001-postgres-omm:
    Session CPU time                             :    0
    Database CPU time                            :    98680
    Session CPU time %                           :    0.00       %
4 dn_6001-postgres-omm:
    Session CPU time                             :    0
    Database CPU time                            :    98680
    Session CPU time %                           :    0.00       %

Session Memory statistics:
1 dn_6001-postgres-omm:
    Buffer Reads                                 :    303
    Shared Buffer Hit ratio                      :    100.00
    In Memory sorts                              :    0
    In Disk sorts                                :    0
    In Memory sorts ratio                        :    0
    Total Memory Size                            :    7002360
    Used Memory Size                             :    5792184
2 dn_6001-postgres-omm:
    Buffer Reads                                 :    300
    Shared Buffer Hit ratio                      :    99.01
    In Memory sorts                              :    0
    In Disk sorts                                :    0
    In Memory sorts ratio                        :    0
    Total Memory Size                            :    6994168
    Used Memory Size                             :    5790576
3 dn_6001-postgres-omm:
    Buffer Reads                                 :    303
    Shared Buffer Hit ratio                      :    100.00
    In Memory sorts                              :    0
    In Disk sorts                                :    0
    In Memory sorts ratio                        :    0
    Total Memory Size                            :    6961400
    Used Memory Size                             :    5782816
4 dn_6001-postgres-omm:
    Buffer Reads                                 :    1113
    Shared Buffer Hit ratio                      :    100.00
    In Memory sorts                              :    1
    In Disk sorts                                :    0
    In Memory sorts ratio                        :    100.00
    Total Memory Size                            :    12467920
    Used Memory Size                             :    10249640

Session IO statistics:
1 dn_6001-postgres-omm:
    Physical Reads                               :    3
    Read Time                                    :    11883
2 dn_6001-postgres-omm:
    Physical Reads                               :    0
    Read Time                                    :    0
3 dn_6001-postgres-omm:
    Physical Reads                               :    0
    Read Time                                    :    0
4 dn_6001-postgres-omm:
    Physical Reads                               :    0
    Read Time                                    :    0

3.3 通过 EXPLAIN 进行 SQL 语句优化

说明:
  使用 explain 能显示 SQL 语句的执行计划 ;
  执行计划将显示 SQL 语句所引用的表会采用什么样的扫描方式,如:简单的顺序扫描、索
引扫描等。如果引用了多个表,执行计划还会显示用到的 JOIN 算法 ;
  执行计划的最关键的部分是语句的预计执行开销,这是计划生成器估算执行该语句将花费
多长的时间 ;
  若指定了 ANALYZE 选项,则该语句模拟执行并形成最优的执行计划(并非真正执行),然
后根据实际的运行结果显示统计数据,包括每个计划节点内时间总开销(毫秒为单位)和
实际返回的总行数。这对于判断计划生成器的估计是否接近现实非常有用。
步骤 1 ROOT 用户登录装有 openGauss 数据库服务的操作系统然后用 su – omm 命令切换至
OMM 用户环境,登录后信息如下。
[root@node0 ~]# su - omm
Last login: Mon Mar 25 19:31:35 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 03月 25日 星期一 20:21:39 CST

System load: 	0.02
Processes: 	195
Memory used: 	38.6%
Swap used: 	13.9%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	1
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 先启动数据库服务,然后使用 gsql 客户端以管理员用户身份连接 postgres 数据库,假设端
口号为 15400
启动数据库服务。
[omm@node0 ~]$ gs_om -t start
Starting cluster.
=========================================
[SUCCESS] node0:
......
=========================================
Successfully started.
然后连接 postgres 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建 student 表。
openGauss=#  CREATE TABLE student 
openGauss-# ( std_id INT NOT NULL, 
openGauss(#  std_name VARCHAR(20) NOT NULL, 
openGauss(#  std_sex VARCHAR(6), 
openGauss(#  std_birth DATE, 
openGauss(#  std_in DATE NOT NULL, 
openGauss(#  std_address VARCHAR(100) 
openGauss(# );
CREATE TABLE
步骤 4 表数据插入。
下面是原数据,直接复制粘贴就行(从上的图也可以看出,如果我把代码录制出来,会有干扰,比如:openGauss=#啥的,就是让自己敲下面的插入代码很麻烦也没有很大的意义),后面这种代码量比较多的我就直接给批量的语句,方便大家复制,在虚拟机直接粘贴就行(如果不知道怎么粘贴,可以直接问)
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (1,'张一','男
','1993-01-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (2,'张二','男
','1993-01-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (3,'张三','男
','1993-01-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (4,'张四','男
','1993-01-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (5,'张五','男
','1993-01-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (6,'张六','男
','1993-01-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (7,'张七','男
','1993-01-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (8,'张八','男
','1993-01-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (9,'张九','男
','1993-01-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (10,'李一','男
','1993-01-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (11,'李二','男
','1993-01-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (12,'李三','男
','1993-01-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (13,'李四','男
','1993-01-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (14,'李五','男
','1993-01-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (15,'李六','男
','1993-01-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (16,'李七','男
','1993-01-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (17,'李八','男
','1993-01-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (18,'李九','男
','1993-01-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (19,'王一','男
','1993-01-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (20,'王二','男
','1993-01-20','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (21,'王三','男
','1993-01-21','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (22,'王四','男
','1993-01-22','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (23,'王五','男
','1993-01-23','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (24,'王六','男
','1993-01-24','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (25,'王七','男
','1993-01-25','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (26,'王八','男
','1993-01-26','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (27,'王九','男
','1993-01-27','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (28,'钱一','男
','1993-01-28','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (29,'钱二','男
','1993-01-29','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (30,'钱三','男
','1993-01-30','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (31,'钱四','男
','1993-02-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (32,'钱五','男
','1993-02-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (33,'钱六','男
','1993-02-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (34,'钱七','男
','1993-02-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (35,'钱八','男
','1993-02-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (36,'钱九','男
','1993-02-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (37,'吴一','男
','1993-02-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (38,'吴二','男
','1993-02-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (39,'吴三','男
','1993-02-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (40,'吴四','男
','1993-02-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (41,'吴五','男
','1993-02-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (42,'吴六','男
','1993-02-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (43,'吴七','男
','1993-02-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (44,'吴八','男
','1993-02-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (45,'吴九','男
','1993-02-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (46,'柳一','男
','1993-02-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (47,'柳二','男
','1993-02-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (48,'柳三','男
','1993-02-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (49,'柳四','男
','1993-02-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (50,'柳五','男
','1993-02-20','2011-09-01','江苏省南京市雨花台区');
步骤 5 数据查询统计。
openGauss=# select count(*) from student;
 count 
-------
    50
(1 row)

openGauss=# select * from student order by std_id;
 std_id | std_name | std_sex |      std_birth      |       std_in        |    
 std_address      
--------+----------+---------+---------------------+---------------------+----
------------------
      1 | 张一     | 男     +| 1993-01-01 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      2 | 张二     | 男     +| 1993-01-02 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      3 | 张三     | 男     +| 1993-01-03 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      4 | 张四     | 男     +| 1993-01-04 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      5 | 张五     | 男     +| 1993-01-05 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      6 | 张六     | 男     +| 1993-01-06 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      7 | 张七     | 男     +| 1993-01-07 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      8 | 张八     | 男     +| 1993-01-08 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      9 | 张九     | 男     +| 1993-01-09 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
     10 | 李一     | 男     +| 1993-01-10 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
     11 | 李二     | 男     +| 1993-01-11 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
     12 | 李三     | 男     +| 1993-01-12 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
     13 | 李四     | 男     +| 1993-01-13 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
     14 | 李五     | 男     +| 1993-01-14 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
     15 | 李六     | 男     +| 1993-01-15 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区

……(部分粘贴,有省略)

//注:就是查询结果,我就不完全粘贴了(我尽量减少一些无用的代码,不然影响观看)

注:就是查询结果,我就不完全粘贴了(我尽量减少一些无用的代码,不然影响观看)

步骤 6 查看表信息。
openGauss=# \d student
                  Table "public.student"
   Column    |              Type              | Modifiers 
-------------+--------------------------------+-----------
 std_id      | integer                        | not null
 std_name    | character varying(20)          | not null
 std_sex     | character varying(6)           | 
 std_birth   | timestamp(0) without time zone | 
 std_in      | timestamp(0) without time zone | not null
 std_address | character varying(100)         | 

步骤 7 收集表的统计信息
openGauss=# ANALYZE VERBOSE student;
INFO:  analyzing "public.student"(dn_6001 pid=3982)
INFO:  ANALYZE INFO : "student": scanned 1 of 1 pages, containing 50 live rows and 0 dead rows; 50 rows in sample, 50 estimated total rows(dn_6001 pid=3982)
ANALYZE
使用 ANALYZE VERBOSE 语句更新统计信息,会同时输出表的相关信息。
步骤 8 查看语句的执行计划。
openGauss=# explain select * from student where std_id = 30;
                       QUERY PLAN                       
--------------------------------------------------------
 Seq Scan on student  (cost=0.00..1.62 rows=1 width=63)
   Filter: (std_id = 30)
(2 rows)

Seq Scan on student 表示使用的是全表扫描。
步骤 9 给表添加主键
openGauss=# alter table student add primary key(std_id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "student_pkey" for table "student"
ALTER TABLE
步骤 10 再次查看表信息。
确定主键是否建好。
openGauss=# \d student
                  Table "public.student"
   Column    |              Type              | Modifiers 
-------------+--------------------------------+-----------
 std_id      | integer                        | not null
 std_name    | character varying(20)          | not null
 std_sex     | character varying(6)           | 
 std_birth   | timestamp(0) without time zone | 
 std_in      | timestamp(0) without time zone | not null
 std_address | character varying(100)         | 
Indexes:
    "student_pkey" PRIMARY KEY, btree (std_id) TABLESPACE pg_default
student_pkey 为主键名称。
步骤 11 通过 hint 来优化语句扫描方式。
通过加 hint 来使查询语句进行索引扫描,最后退出数据库连接。
openGauss=#  explain select /*+indexscan(student student_pkey)*/ * from student where std_id=30;
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 [Bypass]
 Index Scan using student_pkey on student  (cost=0.00..8.27 rows=1 width=63)
   Index Cond: (std_id = 30)
(3 rows)
Index Scan using student_pkey on student 表示语句通过 student 表上的主键索引
student_pkey 进行了索引扫描。
EXPLAIN 进行 SQL 优化实验结束。

4 日志检查

4.1 实验介绍

数据库运行时,某些操作在执行过程中可能会出现错误,数据库依然能够运行。但是此时数据
库中的数据可能已经发生不一致的情况。建议检查 openGauss 运行日志,及时发现隐患。
openGauss 发生故障时,使用 gs_collector 此工具收集 OS 信息、日志信息以及配置文件
等信息,来定位问题。
本实验主要是先手工设置收集配置信息,然后通过 gs_collector 工具调整用配置来收集相关日
志信息。

4.2 通过 gs_collector 工具来收集日志信息

步骤 1 设置收集配置文件。
openGauss=# \q
[omm@node0 ~]$ pwd
/home/omm
[omm@node0 ~]$ vi collector.json
在用 vi collector.json 创建配置文件后,输入 ”i” 进入 INSERT 模式,并将以下文本内容添加至配
置文件中,具体如下:
{
 "Collect":
 [
 {"TypeName": "System", "Content":"RunTimeInfo, HardWareInfo","Interval":"0", "Count":"1"},
 {"TypeName": "Log", "Content" : "Coordinator,DataNode,Gtm,ClusterManager", 
"Interval":"0", "Count":"1"},
 {"TypeName": "Database", "Content": 
"pg_locks,pg_stat_activity,pg_thread_wait_status","Interval":"0", "Count":"1"},
 {"TypeName": "Config", "Content": "Coordinator,DataNode,Gtm", "Interval":"0", "Count":"1"}
 ]
}
内容添加好后,按下“ Esc ”键,然后输入“ :wq ”进行保存文件退出。
配置文件中
利用 TypeName 指定需要收集的信息类型;
利用 Content 指定每一类信息的具体内容;
利用 Count 指定此类信息收集的次数;
利用 Interval 指定收集间隔,单位为秒;
TypeName Content 不允许缺失或者内容为空;
Interval Count 可以不指定,如果没有指定 Count ,则默认收集一次;
如果没有指定 Interval 则表示间隔为 0 秒, Interval Count 的值不能小于 0
如果不指定则使用默认的配置文件;
可以根据 gs_collector 内容收集对照表进行个性化定制配置;
配置文件格式采用 json 格式。
步骤 2 收集 OS 信息及日志信息。
begin-time、 end-time 的值根据自己实际想收集的时间来设置。
[omm@node0 ~]$ gs_collector --begin-time="20240301 23:00" --end-time="20240325 18:00"
Successfully parsed the configuration file.
create Dir.
Successfully create dir.
do system check interval 0 : count 1
Collecting OS information.
The cmd is source /home/omm/.bashrc; python3 '/opt/huawei/install/om/script/local/LocalCollect.py' -t system_check -U omm -l /var/log/omm/omm/om/gs_local.log -C '{#TypeName#: #System#, #Content#: #ps,ioStat,netFlow,spaceUsage,cpuInfo,memInfo,disk,#, #Interval#: #0#, #Count#: #1#}' 
Failed to collect OS information.
do database check interval 0 : count 1
Collecting catalog statistics.
Successfully collected catalog statistics.
do log check interval 0 : count 1
Collecting Log files.
Successfully collected Log files.
do Config check 0:1
Collecting Config files.
Successfully collected Config files.
Collecting files.
Successfully collected files.
All results are stored in /var/log/omm/omm/collector_20240328_213601.tar.gz.
收集完后,所有的结果存放在/var/log/omm/omm/collector_20240328_213601.tar.gz包中
步骤 3 查看日志信息。
先进入日志包所在的目录,然后将日志包进行解压。
[omm@node0 ~]$ cd /var/log/omm/omm/
[omm@node0 omm]$ ll
total 324
drwxr-x--- 3 omm dbgrp   4096 Mar 16 09:51 asp_data
drwxr-x--- 7 omm dbgrp   4096 Mar 15 17:44 bin
drwxr-x--- 3 omm dbgrp   4096 Mar 15 20:37 cm
-rw------- 1 omm dbgrp 136455 Mar 25 19:02 collector_20240325_190144.tar.gz
-rw------- 1 omm dbgrp 150982 Mar 28 21:36 collector_20240328_213601.tar.gz
drwx------ 3 omm dbgrp   4096 Mar 16 09:51 gs_profile
drwxr-x--- 3 omm dbgrp   4096 Mar 16 09:51 mem_log
drwxr-x--- 2 omm dbgrp   4096 Mar 28 21:36 om
drwxr-x--- 3 omm dbgrp   4096 Mar 15 17:44 pg_audit
drwxr-x--- 3 omm dbgrp   4096 Mar 15 17:44 pg_log
drwxr-x--- 3 omm dbgrp   4096 Mar 16 09:51 pg_perf
drwxr-x--- 3 omm dbgrp   4096 Mar 16 09:51 sql_monitor
[omm@node0 omm]$ tar -zxvf collector_20240328_213601.tar.gz
collector_20240328_213601/
collector_20240328_213601/node0.tar.gz
collector_20240328_213601/Summary.log
collector_20240328_213601/Detail.log

接下来,进入解压后的文件夹 collector_20240328_213601(这个每个人可能不一样,不要完全复制,观察一下,和你的解压时间挂钩,大家仔细看看),并对 node0.tar.gz (这个也是,和主机名挂钩)包进一步解压。

[omm@node0 omm]$ cd collector_20240328_213601
[omm@node0 collector_20240328_213601]$ ll
total 156
-rw------- 1 omm dbgrp   2870 Mar 28 21:36 Detail.log
-rw------- 1 omm dbgrp 150043 Mar 28 21:36 node0.tar.gz
-rw------- 1 omm dbgrp   1055 Mar 28 21:36 Summary.log
[omm@node0 collector_20240328_213601]$ tar -zxvf node0.tar.gz
node0/
node0/gstackfiles/
node0/configfiles/
node0/configfiles/config_20240328_213613624181/
node0/configfiles/config_20240328_213613624181/dn_6001/
node0/configfiles/config_20240328_213613624181/dn_6001/pg_ident.conf
node0/configfiles/config_20240328_213613624181/dn_6001/gaussdb.state
node0/configfiles/config_20240328_213613624181/dn_6001/pg_replslot/
node0/configfiles/config_20240328_213613624181/dn_6001/pg_hba.conf
node0/configfiles/config_20240328_213613624181/dn_6001/postgresql.conf
node0/systemfiles/
node0/systemfiles/OS_information_20240328_213603621784.txt
node0/systemfiles/database_system_info_20240328_213603672555.txt
node0/coreDumpfiles/
node0/planSimulatorfiles/
node0/catalogfiles/
node0/catalogfiles/dn_6001_pg_locks_20240328_213608752242.csv
node0/catalogfiles/dn_6001_pg_stat_activity_20240328_213609154400.csv
node0/catalogfiles/gs_clean_20240328_213610016028.txt
node0/catalogfiles/dn_6001_pg_thread_wait_status_20240328_213609607570.csv
node0/logfiles/
node0/logfiles/log_20240328_213611730074.tar.gz
node0/xlogfiles/

在解压的 node0(指的是服务器名/主机名,各自的不一样,请注意观察)下有各种定制收集的日志

[omm@node0 collector_20240328_213601]$ cd node0
[omm@node0 node0]$ ll
total 32
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 catalogfiles
drwx------ 3 omm dbgrp 4096 Mar 28 21:36 configfiles
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 coreDumpfiles
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 gstackfiles
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 logfiles
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 planSimulatorfiles
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 systemfiles
drwx------ 2 omm dbgrp 4096 Mar 28 21:36 xlogfiles
[omm@node0 node0]$ cd catalogfiles/
[omm@node0 catalogfiles]$ ll
total 16
-rw------- 1 omm dbgrp  392 Mar 28 21:36 dn_6001_pg_locks_20240328_213608752242.csv
-rw------- 1 omm dbgrp 1851 Mar 28 21:36 dn_6001_pg_stat_activity_20240328_213609154400.csv
-rw------- 1 omm dbgrp 1945 Mar 28 21:36 dn_6001_pg_thread_wait_status_20240328_213609607570.csv
-rw------- 1 omm dbgrp  286 Mar 28 21:36 gs_clean_20240328_213610016028.txt
[omm@node0 catalogfiles]$ 
步骤 4 下载收集后的日志文件。
根据自己需要比如可以通过 WinSCP (我用的这个,这个工具主要是连接主机和虚拟机的) 或者 XFTP SSH 工具将日志文件下载至自己本地电脑,
如:

5 最大连接数设置

5.1 实验介绍

当应用程序与数据库的连接数超过最大值,则新的连接无法建立。建议对连接数进行监控,及
时释放空闲的连接或者增加最大连接数。
本实验主要是讲如何来设置数据库最大连接个数。

5.2 场景设置及操作步骤

步骤 1 ROOT 用户登录装有 openGauss 数据库服务的操作系统然后用 su – omm 命令切换至
OMM 用户环境,登录后信息如下。
[root@node0 ~]# su - omm
Last login: Thu Mar 28 21:10:18 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 03月 28日 星期四 22:03:30 CST

System load: 	0.02
Processes: 	199
Memory used: 	39.1%
Swap used: 	21.8%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 确认 openGauss 数据库服务是否启动
[omm@node0 ~]$ gs_om -t status;
-----------------------------------------------------------------------

cluster_name    : dbCluster
cluster_state   : Normal
redistributing  : No

-----------------------------------------------------------------------
cluster_state : Normal 表示已启动,可以正常使用。如果状态为非 Normal 表示不可用
为了实验场景设置,如果数据库服务没有启动,请执行步 gs_om -t start 命令启动服务。
步骤 3 登录数据库
使用 gsql 客户端以管理员用户身份连接 postgres 数据库,假设端口号为 15400
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 4 查看当前数据库已使用的连接数
openGauss=# select count(1) from pg_stat_activity;
 count 
-------
     8
(1 row)

8 表示当前有 8 个应用已连接到数据库
步骤 5 查看数据库设置的最大连接数
openGauss=# SHOW max_connections;
 max_connections 
-----------------
 5000
(1 row)
5000 表示数据库设置的最大连接个数为 5000 。如果当前数据库已使用的连接数快接近于最大
连接数时,运维人员先要果断的增加最大连接数以防系统新的连接无法建立。
步骤 6 调整最大连接数参数
\q 退出数据库,然后在 omm 用户环境下通过 gs_guc 工具来增大参数值,如下:
openGauss=# \q
[omm@node0 ~]$ gs_guc reload -I all -c "max_connections= 6000";
The gs_guc run with the following arguments: [gs_guc -I all -c max_connections= 6000 reload ].
expected instance path: [/opt/huawei/install/data/dn/postgresql.conf]
gs_guc reload: max_connections=6000: [/opt/huawei/install/data/dn/postgresql.conf]
server signaled

Total instances: 1. Failed instances: 0.
Success to perform gs_guc!
步骤 7 重启数据库
gs_om -t stop 先关闭数据库 , 然后用 gs_om -t start 再启动数据库
[omm@node0 ~]$ gs_om -t stop;
Stopping cluster.
=========================================
Successfully stopped cluster.
=========================================
End stop cluster.
[omm@node0 ~]$ gs_om -t start;
Starting cluster.
=========================================
[SUCCESS] node0
2024-03-28 22:26:31.558 66057e17.1 [unknown] 140368399876032 [unknown] 0 dn_6001 01000  0 [BACKEND] WARNING:  could not create any HA TCP/IP sockets
2024-03-28 22:26:31.558 66057e17.1 [unknown] 140368399876032 [unknown] 0 dn_6001 01000  0 [BACKEND] WARNING:  could not create any HA TCP/IP sockets
2024-03-28 22:26:31.561 66057e17.1 [unknown] 140368399876032 [unknown] 0 dn_6001 01000  0 [BACKEND] WARNING:  Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (2314 Mbytes) is larger.
=========================================
Successfully started.
步骤 8 验证参数设置是否成功
使用 gsql 客户端以管理员用户身份连接 postgres 数据库,然后查看参数值,最后退出数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

openGauss=# SHOW max_connections;
 max_connections 
-----------------
 6000
(1 row)

这里显示 max_connections 6000 ,说明前面参数的修改已经生效。
最大连接数设置实验结束。

6 例行表、索引的维护

6.1 实验介绍

为了保证数据库的有效运行,数据库必须在插入 / 删除操作后,基于客户场景,定期做 VACUUM
FULL ANALYZE ,更新统计信息,以便获得更优的性能;
VACUUM FULL 可回收已更新或已删除的数据所占据的磁盘空间,同时将小数据文件合并;
VACUUM 对每个表维护了一个可视化映射来跟踪包含对别的活动事务可见的数组的页。
一个普通的索引扫描首先通过可视化映射来获取对应的数组,来检查是否对当前事务可见。
若无法获取,再通过堆数组抓取的方式来检查。因此更新表的可视化映射,可加速唯一索
引扫描;
ANALYZE 可收集与数据库中表内容相关的统计信息。统计结果存储在系统表
PG_STATISTIC 中。查询优化器会使用这些统计数据,生成最有效的执行计划。
数据库经过多次删除操作后,索引页面上的索引键将被删除,造成索引膨胀。例行重建索
引,可有效的提高查询效率。
本实验主要是通过使用 VACUUM VACUUM FULL FULL 来收缩表,用 ANALYZE 来收集表的
统计信息以及对表上的索引进行重建。

6.2 场景设置及操作步骤

步骤 1 ROOT 用户登录装有 openGauss 数据库服务的操作系统然后用 su – omm 命令切换至
OMM 用户环境,登录后信息如下。

[root@node0 ~]# su - omm
Last login: Thu Mar 28 22:03:30 CST 2024 on pts/0


Welcome to 5.10.0-153.12.0.92.oe2203sp2.x86_64

System information as of time: 	2024年 03月 28日 星期四 22:33:00 CST

System load: 	0.08
Processes: 	198
Memory used: 	37.1%
Swap used: 	9.4%
Usage On: 	25%
IP address: 	192.168.28.131
Users online: 	2
To run a command as administrator(user "root"),use "sudo <command>".
步骤 2 启动服务器后,然后使用 gsql 客户端以管理员用户身份连接 postgres 数据库,假设端口号
15400
启动数据库服务。
[omm@node0 ~]$ gs_om -t start;
Starting cluster.
=========================================
[SUCCESS] node0
2024-03-28 22:35:55.148 6605804b.1 [unknown] 140256478302144 [unknown] 0 dn_6001 01000  0 [BACKEND] WARNING:  could not create any HA TCP/IP sockets
2024-03-28 22:35:55.148 6605804b.1 [unknown] 140256478302144 [unknown] 0 dn_6001 01000  0 [BACKEND] WARNING:  could not create any HA TCP/IP sockets
2024-03-28 22:35:55.150 6605804b.1 [unknown] 140256478302144 [unknown] 0 dn_6001 01000  0 [BACKEND] WARNING:  Failed to initialize the memory protect for g_instance.attr.attr_storage.cstore_buffers (1024 Mbytes) or shared memory (2314 Mbytes) is larger.
=========================================
Successfully started.
连接 postgres 数据库。
[omm@node0 ~]$ gsql -d postgres -p 15400 -r;
gsql ((openGauss 5.0.0 build a07d57c3) compiled at 2023-03-29 03:37:13 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
步骤 3 创建 student
openGauss=# drop table student;
DROP TABLE
openGauss=#  CREATE TABLE student 
openGauss-# ( std_id INT NOT NULL, 
openGauss(#  std_name VARCHAR(20) NOT NULL, 
openGauss(#  std_sex VARCHAR(6), 
openGauss(#  std_birth DATE, 
openGauss(#  std_in DATE NOT NULL, 
openGauss(#  std_address VARCHAR(100) 
openGauss(# );
CREATE TABLE
步骤 4 表数据插入
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (1,'张一','男
','1993-01-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (2,'张二','男
','1993-01-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (3,'张三','男
','1993-01-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (4,'张四','男
','1993-01-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (5,'张五','男
','1993-01-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (6,'张六','男
','1993-01-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (7,'张七','男
','1993-01-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (8,'张八','男
','1993-01-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (9,'张九','男
','1993-01-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (10,'李一','男
','1993-01-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (11,'李二','男
','1993-01-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (12,'李三','男
','1993-01-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (13,'李四','男
','1993-01-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (14,'李五','男
','1993-01-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (15,'李六','男
','1993-01-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (16,'李七','男
','1993-01-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (17,'李八','男
','1993-01-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (18,'李九','男
','1993-01-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (19,'王一','男
','1993-01-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (20,'王二','男
','1993-01-20','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (21,'王三','男
','1993-01-21','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (22,'王四','男
','1993-01-22','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (23,'王五','男
','1993-01-23','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (24,'王六','男
','1993-01-24','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (25,'王七','男
','1993-01-25','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (26,'王八','男
','1993-01-26','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (27,'王九','男
','1993-01-27','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (28,'钱一','男
','1993-01-28','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (29,'钱二','男
','1993-01-29','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (30,'钱三','男
','1993-01-30','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (31,'钱四','男
','1993-02-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (32,'钱五','男
','1993-02-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (33,'钱六','男
','1993-02-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (34,'钱七','男
','1993-02-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (35,'钱八','男
','1993-02-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (36,'钱九','男
','1993-02-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (37,'吴一','男
','1993-02-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (38,'吴二','男
','1993-02-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (39,'吴三','男
','1993-02-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (40,'吴四','男
','1993-02-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (41,'吴五','男
','1993-02-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (42,'吴六','男
','1993-02-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (43,'吴七','男
','1993-02-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (44,'吴八','男
','1993-02-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (45,'吴九','男
','1993-02-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (46,'柳一','男
','1993-02-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (47,'柳二','男
','1993-02-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (48,'柳三','男
','1993-02-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (49,'柳四','男
','1993-02-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (50,'柳五','男
','1993-02-20','2011-09-01','江苏省南京市雨花台区');
步骤 5 数据查询统计
openGauss=# select count(*) from student;
 count 
-------
    50
(1 row)

openGauss=#  select * from student order by std_id;
 std_id | std_name | std_sex |      std_birth      |       std_in        |    
 std_address      
--------+----------+---------+---------------------+---------------------+----
------------------
      1 | 张一     | 男     +| 1993-01-01 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      2 | 张二     | 男     +| 1993-01-02 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      3 | 张三     | 男     +| 1993-01-03 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      4 | 张四     | 男     +| 1993-01-04 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      5 | 张五     | 男     +| 1993-01-05 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
        |          |         |                     |                     | 
      6 | 张六     | 男     +| 1993-01-06 00:00:00 | 2011-09-01 00:00:00 | 江
省南京市雨花台区
步骤 6 查看表信息
openGauss=#  \d student
                  Table "public.student"
   Column    |              Type              | Modifiers 
-------------+--------------------------------+-----------
 std_id      | integer                        | not null
 std_name    | character varying(20)          | not null
 std_sex     | character varying(6)           | 
 std_birth   | timestamp(0) without time zone | 
 std_in      | timestamp(0) without time zone | not null
 std_address | character varying(100)         | 
步骤 7 使用 VACUUM 命令,进行磁盘空间回收
openGauss=# vacuum student;
VACUUM
步骤 8 删除表中数据
openGauss=#  delete from student where std_id>30;
DELETE 20
步骤 9 使用 VACUUM FULL 命令,进行磁盘空间回收
openGauss=# vacuum full student;
VACUUM
步骤 10 使用 ANALYZE 语句更新统计信息
openGauss=#  analyze student;
ANALYZE
步骤 11 使用 ANALYZE VERBOSE 语句更新统计信息,并输出表的相关信息
openGauss=#  analyze verbose student;
INFO:  analyzing "public.student"(dn_6001 pid=3999)
INFO:  ANALYZE INFO : "student": scanned 1 of 1 pages, containing 30 live rows and 20 dead rows; 30 rows in sample, 30 estimated total rows(dn_6001 pid=3999)
ANALYZE
步骤 12 执行 VACUUM ANALYZE 命令进行查询优化
openGauss=# vacuum analyze student;
VACUUM
步骤 13 查看特定表的统计信息
openGauss=# select relname,n_tup_ins,n_tup_upd,n_tup_del,last_analyze,vacuum_count from PG_STAT_ALL_TABLES where relname='student';
 relname | n_tup_ins | n_tup_upd | n_tup_del |         last_analyze         | 
vacuum_count 
---------+-----------+-----------+-----------+------------------------------+-
-------------
 student |         0 |         0 |         0 | 2024-03-29 11:14:46.38435+08 | 
           1
(1 row)
PG_STAT_ALL_TABLES 视图将包含当前数据库中每个表的一行统计信息,以上查询结果中各列
分别表示:
Relname 表名
n_tup_ins 插入行数
n_tup_upd 更新行数
n_tup_del 删除行数
last_analyze 上次手动分析该表的时间
vacuum_count 这个表被手动清理的次数
步骤 14 索引维护
说明:
  如果数据发生大量删除后,索引页面上的索引键将被删除,导致索引页面数量的减少,造
成索引膨胀。重建索引可回收浪费的空间。
  新建的索引中逻辑结构相邻的页面,通常在物理结构中也是相邻的,所以一个新建的索引
比更新了多次的索引访问速度要快。
  重建索引有以下两种方式:
1 、使用 REINDEX 语句重建索引;
2 、先删除索引( DROP INDEX ),再创建索引( CREATE INDEX )。
先在 student 表的 std_name 列上创建一个索引,如下:
openGauss=#  create index inx_stu01 on student(std_name);
CREATE INDEX
方式 1 :使用 REINDEX 语句重建索引,具体如下:
openGauss=# reindex table student;
REINDEX
方式 2 :先删除索引( DROP INDEX ),再创建索引( CREATE INDEX ),具体如下
openGauss=# drop index inx_stu01;
DROP INDEX
openGauss=# create index inx_stu01 on student(std_name);
CREATE INDEX
查看表结构信息,具体如下:
openGauss=# \d student;
                  Table "public.student"
   Column    |              Type              | Modifiers 
-------------+--------------------------------+-----------
 std_id      | integer                        | not null
 std_name    | character varying(20)          | not null
 std_sex     | character varying(6)           | 
 std_birth   | timestamp(0) without time zone | 
 std_in      | timestamp(0) without time zone | not null
 std_address | character varying(100)         | 
Indexes:
    "inx_stu01" btree (std_name) TABLESPACE pg_default

例行表、索引的维护实验结束。
  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值