iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >PostgreSQL怎么创建分区表
  • 688
分享到

PostgreSQL怎么创建分区表

2024-04-02 19:04:59 688人浏览 八月长安
摘要

这篇文章主要介绍“postgresql怎么创建分区表”,在日常操作中,相信很多人在Postgresql怎么创建分区表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Postgr

这篇文章主要介绍“postgresql怎么创建分区表”,在日常操作中,相信很多人在Postgresql怎么创建分区表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PostgreSQL怎么创建分区表”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

PG 11在插入分区表时,无论涉及多少个分区都会住每个分区,如果分区数很多,会存在性能问题.在PG 12,只需要对涉及的分区上锁,也就是说如果只插入一行,则只需要锁一个分区.这种变化还与分区元组路由代码的完全重写相结合,大大减少了在executor启动期间设置元组路由数据结构的开销。

创建分区表

[local]:5432 pg12@testdb=# drop table if exists t_counter;
NOTICE:  table "t_counter" does not exist, skipping
DROP TABLE
Time: 29.768 ms
[local]:5432 pg12@testdb=# create table t_counter(id int);
CREATE TABLE
Time: 120.165 ms
[local]:5432 pg12@testdb=# insert into t_counter select generate_series(0,100000);
INSERT 0 100001
Time: 333.637 ms
[local]:5432 pg12@testdb=# drop table if exists t_hash_manypartitions;
NOTICE:  table "t_hash_manypartitions" does not exist, skipping
DROP TABLE
Time: 1.536 ms
[local]:5432 pg12@testdb=# create table t_hash_manypartitions (c1 int,c2  varchar(40),c3 varchar(40)) partition by hash(c2);
CREATE TABLE
Time: 45.986 ms
[local]:5432 pg12@testdb=# 
[local]:5432 pg12@testdb=# \o /tmp/script.sql
[local]:5432 pg12@testdb=# select 'create table t_hash_manypartitions_'
pg12@testdb-#       ||id
pg12@testdb-#       ||' partition of t_hash_manypartitions for values with (modulus 8192,remainder '||id||');'
pg12@testdb-# from t_counter
pg12@testdb-# where id < 8192
pg12@testdb-# order by id ;
Time: 78.499 ms
[local]:5432 pg12@testdb=# \o
[local]:5432 pg12@testdb=# 
[root@localhost ~]# tail -n 10 /tmp/script.sql 
 create table t_hash_manypartitions_8184 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8184);
 create table t_hash_manypartitions_8185 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8185);
 create table t_hash_manypartitions_8186 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8186);
 create table t_hash_manypartitions_8187 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8187);
 create table t_hash_manypartitions_8188 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8188);
 create table t_hash_manypartitions_8189 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8189);
 create table t_hash_manypartitions_8190 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8190);
 create table t_hash_manypartitions_8191 partition of t_hash_manypartitions for values with (modulus 8192,remainder 8191);
(8192 rows)
[local]:5432 pg12@testdb=# \i /tmp/script.sql
...
CREATE TABLE
Time: 20.784 ms
CREATE TABLE
Time: 21.107 ms
psql:/tmp/script.sql:8196: ERROR:  syntax error at or near "8192"
LINE 1: (8192 rows)
         ^
Time: 0.198 ms
[local]:5432 pg12@testdb=#

PG 11
启动事务,插入一行

[xdb@localhost ~]$ psql -d testdb -p 5433
psql (11.2)
Type "help" for help.
testdb=# \timing
Timing is on.
testdb=# begin;
BEGIN
Time: 1.750 ms
testdb=# insert into t_hash_manypartitions(c1,c2,c3) values(1,'c2-1','c3-1');
INSERT 0 1
Time: 75.649 ms
testdb=#

查询锁信息,锁定了所有分区

testdb=# select relation::reGClass,locktype,virtualxid,transactionid,virtualtransaction,pid,mode,granted,fastpath from pg_locks where pid <> pg_backend_pid();
          relation          |   locktype    | virtualxid | transactionid | virtualtransaction | pid  |       mode       | granted | fastpath 
----------------------------+---------------+------------+---------------+--------------------+------+------------------+---------+----------
 t_hash_manypartitions_15   | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_14   | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_13   | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_12   | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_11   | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_10   | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_9    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_8    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_7    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_6    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_5    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_4    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_3    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_2    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions_1    | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
 t_hash_manypartitions      | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | t
                            | virtualxid    | 3/8202     |               | 3/8202             | 4855 | ExclusiveLock    | t       | t
 t_hash_manypartitions_1077 | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | f
 t_hash_manypartitions_3140 | relation      |            |               | 3/8202             | 4855 | RowExclusiveLock | t       | f
 ...
 testdb=# select count(*) from pg_locks where pid <> pg_backend_pid();
 count 
-------
  8194
(1 row)

PG 12
启动事务,插入一行

[local]:5432 pg12@testdb=# begin;
BEGIN
Time: 2.418 ms
[local]:5432 pg12@testdb=#* 
[local]:5432 pg12@testdb=#* insert into t_hash_manypartitions(c1,c2,c3) values(1,'c2-1','c3-1');
INSERT 0 1
Time: 46.988 ms
[local]:5432 pg12@testdb=#*

查询锁信息,只锁定一个分区

[local]:5432 pg12@testdb=# select relation::regclass,locktype,virtualxid,transactionid,virtualtransaction,pid,mode,granted,fastpath from pg_locks where pid <> pg_backend_pid();
          relation          |   locktype    | virtualxid | transactionid | virtualtransaction | pid  |       mode       | granted | fastpath 
----------------------------+---------------+------------+---------------+--------------------+------+------------------+---------+----------
 t_hash_manypartitions_4956 | relation      |            |               | 3/8202             | 3230 | RowExclusiveLock | t       | t
 t_hash_manypartitions      | relation      |            |               | 3/8202             | 3230 | AccessshareLock  | t       | t
 t_hash_manypartitions      | relation      |            |               | 3/8202             | 3230 | RowExclusiveLock | t       | t
                            | virtualxid    | 3/8202     |               | 3/8202             | 3230 | ExclusiveLock    | t       | t
                            | transactionid |            |        176799 | 3/8202             | 3230 | ExclusiveLock    | t       | f
(5 rows)
Time: 1.596 ms

到此,关于“PostgreSQL怎么创建分区表”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

您可能感兴趣的文档:

--结束END--

本文标题: PostgreSQL怎么创建分区表

本文链接: https://www.lsjlt.com/news/63616.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • PostgreSQL怎么创建分区表
    这篇文章主要介绍“PostgreSQL怎么创建分区表”,在日常操作中,相信很多人在PostgreSQL怎么创建分区表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Postgr...
    99+
    2024-04-02
  • PostgreSQL如何创建分区表
    这篇文章主要介绍“PostgreSQL如何创建分区表”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“PostgreSQL如何创建分区表”文章能帮助大家解决问题。前言PG 假如我们想像Hive那也创建动...
    99+
    2023-07-02
  • mysql中怎么创建表分区
    今天就跟大家聊聊有关mysql中怎么创建表分区,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。 1.RANGE 分区: CREATE TABLE ...
    99+
    2024-04-02
  • postgresql怎么创建表空间
    要在PostgreSQL中创建表空间,可以按照以下步骤操作: 打开PostgreSQL客户端工具,如psql或pgAdmin。 ...
    99+
    2024-04-17
    postgresql
  • oracle怎么创建列表分区
    要在Oracle中创建列表分区,可以按照以下步骤进行操作:1. 首先,确保你有创建分区的权限。如果没有,可以联系数据库管理员进行授权...
    99+
    2023-08-18
    oracle
  • 怎么理解PostgreSQL的分区表
    本篇内容主要讲解“怎么理解PostgreSQL的分区表”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解PostgreSQL的分区表”吧!在PG中,分区表通...
    99+
    2024-04-02
  • oracle怎么在已有表上创建分区
    要在已有的表上创建分区,需要使用ALTER TABLE语句并指定分区关键字。以下是一个示例: ALTER TABLE table_n...
    99+
    2024-04-09
    oracle
  • mysql如何创建分区表
    MySQL可以通过以下步骤来创建分区表:1. 创建一个普通的表,定义它的列和其他属性。2. 使用`ALTER TABLE`语句来添加...
    99+
    2023-10-12
    mysql
  • PostgreSQL中怎么创建一个新的表
    在 PostgreSQL 中,要创建一个新的表,你可以使用 CREATE TABLE 语句。以下是一个示例: CREATE TABL...
    99+
    2024-04-09
    PostgreSQL
  • linux分区怎么创建
    在Linux系统中,可以使用命令行工具或图形界面工具来创建分区。下面是两种常见的方法:1. 使用命令行工具(例如fdisk或part...
    99+
    2023-08-25
    linux
  • 如何在PostgreSQL中创建表
    要在PostgreSQL中创建表,您可以使用CREATE TABLE语句。以下是一个示例: CREATE TABLE table_n...
    99+
    2024-04-09
    PostgreSQL
  • CentOS中怎么创建分区
    CentOS中怎么创建分区,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1.磁盘分区基础知识:硬盘分类,MBR,磁盘分区,磁盘的扇面、磁道、扇区 2.Linux下创建...
    99+
    2023-06-10
  • hive如何创建外部分区表
    这篇文章主要为大家展示了“hive如何创建外部分区表”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“hive如何创建外部分区表”这篇文章吧。drop table ...
    99+
    2024-04-02
  • MySQL中如何创建Key分区表
    本篇文章为大家展示了MySQL中如何创建Key分区表,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。 按照KEY进行分区类似于按照HAS...
    99+
    2024-04-02
  • mybatis创建表分区的方法是什么
    在MyBatis中创建表分区可以通过在SQL语句中使用分区关键字来实现。具体方法如下: 在创建表时指定分区关键字,例如: CRE...
    99+
    2024-04-02
  • PostgreSQL中怎么利用表继承实现分区表
    这篇文章将为大家详细讲解有关PostgreSQL中怎么利用表继承实现分区表,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。概述分区指的是将逻辑上一的一个大表分...
    99+
    2024-04-02
  • Linux怎么创建主分区与逻辑分区
    在Linux中,可以使用fdisk命令或者parted命令来创建主分区和逻辑分区。1. 创建主分区:- 打开终端,输入`sudo f...
    99+
    2023-10-12
    linux
  • linux交换分区怎么创建
    这篇文章主要介绍“linux交换分区怎么创建”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“linux交换分区怎么创建”文章能帮助大家解决问题。linux交换分区就是linux的SWAP分区,它是LI...
    99+
    2023-07-04
  • MySQL怎么创建分区索引
    要在MySQL中创建分区索引,首先需要确保表已经被分区。对于已经分区的表,可以使用ALTER TABLE语句来添加分区索引。 以下是...
    99+
    2024-04-23
    mysql
  • Linux逻辑分区怎么创建
    要在Linux上创建逻辑分区,可以按照以下步骤进行:1. 打开终端,并以管理员身份登录。2. 运行命令`fdisk -l`,查看已经...
    99+
    2023-10-12
    Linux
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作