postgresql – 最优数据库表优化方法

我有一个需要优化的数据库表变得太大(几亿行),但在我进入分区之前,我想我会问一些建议.

这是用法:

0.表包含大约10列长度,每列大约20个字节.

> INSERTS以每秒数百次的速度执行.
> SELECT语句基于列’a'(其中a =’xxxx’)每小时执行几次.
> DELETE语句基于DATE列执行. (删除超过1年的日期)通常每天一次.

关键要求是加速INSERT和SELECT语句,并能够保留1年的历史数据,而不会在删除时锁定整个表.

我猜我必须有两个索引,一个用于列’a’,另一个用于日期字段.或者是否可以优化两者?

在选择速度和删除速度之间是否需要进行权衡?

分区是唯一的解决方案吗?分区这样的表有什么好的策略?

我正在使用PostgreSQL 8.4数据库.

解决方法

你有没有看过 PostgreSQL partitioning,而不是保持一个物理表?从8.1版开始支持它.

分区可以帮助您避免在快速INSERT与快速DELETE性能之间进行选择的问题.您始终可以按年/月对表进行分区,只需删除不再需要的分区即可.丢弃分区非常快,插入小分区也非常快.

从手册:

Partitioning refers to splitting what is logically one large table into
smaller physical pieces. Partitioning
can provide several benefits:

  
  
  
  >某些情况下,查询性能可以得到显着提高
  各种查询.
  >更新性能也可以提高,因为每一部分
  表的索引小于
  整个数据集的索引将是.
  当一个指数不再适合
  内存,读写操作
  在索引上逐步采取更多
  磁盘访问.
  >可以通过简单地删除其中一个来完成批量删除
  分区,如果有要求的话
  计划进入分区设计.
  DROP TABLE比批量快得多
  删除,更不用说随之而来的了
  VACUUM开销.
  >很少使用的数据可以迁移到更便宜和更慢的存储
  媒体.
  
  
  

The benefits will normally be worthwhile only when a table would
otherwise be very large. The exact
point at which a table will benefit
from partitioning depends on the
application,although a rule of thumb
is that the size of the table should
exceed the physical memory of the
database server.

Currently,PostgreSQL supports partitioning via table inheritance. Each partition must be created as a child table of a single parent table. The parent table itself is normally empty; it exists just to represent the entire data set. You should be familiar with inheritance (see Section 5.8) before attempting to implement partitioning.

相关文章

文章浏览阅读601次。Oracle的数据导入导出是一项基本的技能,...
文章浏览阅读553次。开头还是介绍一下群,如果感兴趣polardb...
文章浏览阅读3.5k次,点赞3次,收藏7次。折腾了两个小时多才...
文章浏览阅读2.7k次。JSON 代表 JavaScript Object Notation...
文章浏览阅读2.9k次,点赞2次,收藏6次。navicat 连接postgr...
文章浏览阅读1.4k次。postgre进阶sql,包含分组排序、JSON解...