Partitioned Index - Alibaba Cloud RDS PostgreSQL Best Practices

简介: When should you partition a table in your database? Learn how to split tables with partial index.

DataWarehouse_FriendlyDatabaseDesign

Background

When you have a very large table, you may want to partition it. For example, a user table can be split into many tables by user ID (hash) or by range.

In another example, a behavior data table can be partitioned by time, and be split into multiple tables.

Advantages of table partitioning:

  1. Table partitioning allows tables to be stored in different table partitions that correlate to block devices. For example, historical data, which usually contains a huge amount of data with low page views can be stored in a table partition on your HDD. However, active data can be stored in table partitions on an SSD.
  2. Tables are easier to maintain after partitioning. For example, you can just use Drop Table to delete historical data without using REDO.

In fact, indexes can also be partitioned, e.g. partitioning by user ID hash or by time. Aside from having the same advantages as table partitions, index partitions also feature the following advantages:

  1. You do not need to create indexes for data that you do not search for. 1.Taking a user table as an example, we only search for active users and do not search for inactive users, so we can create indexes only for active users.
  2. For data with different structures, you can use different index interfaces. 2.For example, when data distribution in a table is uneven, some values appear frequently, while other values appear less frequently. We can use bitmap or gin indexes for values that appear frequently, and use B-tree indexes for values that do not appear frequently.

Let’s move to details on how to implement index partitioning through PostgreSQL.

Global Index

We usually create a global index. This implementation is relatively easy, but it can make our database less efficient if we do not use partitions.

create table test(id int, crt_time timestamp, info text);  
  
create index idx_test_id on test(id);  

Primary Partition Index

We can add primary partition indexes to split our table into multiple parts. In this example, we split the table based on crt_time.

create table test(id int, crt_time timestamp, info text);  
  
Partitioned indexes are as follows  
  
create index idx_test_id_1 on test(id) where crt_time between '2017-01-01' and '2017-02-01';  
create index idx_test_id_2 on test(id) where crt_time between '2017-02-01' and '2017-03-01';  
...  
create index idx_test_id_12 on test(id) where crt_time between '2017-12-01' and '2018-01-01';  

Multilayer Partition Index

We can further divide the partitioned tables into smaller ones by adding another layer of index. In this example, we add the province_code index to the crt_time index to create a multilayer partition index. Now we have created 6 partitions from the original table.

create table test(id int, crt_time timestamp, province_code int, info text);  
  
Partitioned indexes are as follows  
  
create index idx_test_id_1_1 on test(id) where crt_time between '2017-01-01' and '2017-02-01' and province_code=1;  
create index idx_test_id_1_2 on test(id) where crt_time between '2017-02-01' and '2017-03-01' and province_code=1;  
...  
create index idx_test_id_1_12 on test(id) where crt_time between '2017-12-01' and '2018-01-01' and province_code=1;  
  
....  
  
create index idx_test_id_2_1 on test(id) where crt_time between '2017-01-01' and '2017-02-01' and province_code=2;  
create index idx_test_id_2_2 on test(id) where crt_time between '2017-02-01' and '2017-03-01' and province_code=2;  
...  
create index idx_test_id_2_12 on test(id) where crt_time between '2017-12-01' and '2018-01-01' and province_code=2;  

Example of Partitioning Unevenly Distributed Data

We can also apply gin and B-tree indexes to speed up the operation of our table partitions.

create table test(uid int, crt_time timestamp, province_code int, info text);  
  
create index idx_test_1 on test using gin(uid) where uid<1000;     -- This section contains a large number of repeated values (high-frequency values), so we can use gin index to accelerate the operation  
create index idx_test_1 on test using btree(uid) where uid>=1000;  -- This section contains low-frequency values, so we can use btree index to accelerate the operation  

Summary

1.When searching for data, you can use index partitioning conditions, index fields and the corresponding operators to search with partitioned indexes.

2.Partitioned indexes are generally used in searches with multiple conditions, and uses the partitioning condition as one of the search conditions. Of course, it can also be used when searching a single column

3.PostgreSQL supports not only partitioned indexes, but also expression indexes and functional indexes.

Welcome to Alibaba Cloud RDS PostgreSQL to learn more.

目录
相关文章
|
12月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1785 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1222 156
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
1719 213
|
12月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
存储 关系型数据库 MySQL
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB区别,适用场景
一个项目用5款数据库?MySQL、PostgreSQL、ClickHouse、MongoDB——特点、性能、扩展性、安全性、适用场景比较
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
SQL 人工智能 关系型数据库
【PG锦囊】阿里云 RDS PostgreSQL 版插件—AI 插件(rds_ai)
本文介绍了AI 插件(rds_ai)的核心优势、适用场景等,帮助您更好地了解 rds_ai 插件。想了解更多 RDS 插件信息和讨论交流,欢迎加入 RDS PG 插件用户专项服务群(103525002795)
|
关系型数据库 MySQL 数据库
市场领先者MySQL的挑战者:PostgreSQL的崛起
PostgreSQL(简称PG)是世界上最先进的开源对象关系型数据库,起源于1986年的加州大学伯克利分校POSTGRES项目。它以其丰富的功能、强大的扩展性和数据完整性著称,支持复杂数据类型、MVCC、全文检索和地理空间数据处理等特性。尽管市场份额略低于MySQL,但PG在全球范围内广泛应用,受到Google、AWS、Microsoft等知名公司支持。常用的客户端工具包括PgAdmin、Navicat和DBeaver。
1480 4
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
416 5
|
存储 关系型数据库 MySQL
MySQL vs. PostgreSQL:选择适合你的开源数据库
在众多开源数据库中,MySQL和PostgreSQL无疑是最受欢迎的两个。它们都有着强大的功能、广泛的社区支持和丰富的生态系统。然而,它们在设计理念、性能特点、功能特性等方面存在着显著的差异。本文将从这三个方面对MySQL和PostgreSQL进行比较,以帮助您选择更适合您需求的开源数据库。
903 4

热门文章

最新文章

推荐镜像

更多