performance - 基准 : bigint vs int on PostgreSQL

标签 performance postgresql int sqldatatypes bigint

我想提高我的数据库性能。在一个项目中,所有表都从 intbigint,我认为这不仅在存储方面是一个糟糕的选择,因为 int 需要 4 字节bigint 需要8 字节;但也与性能有关。 所以我创建了一个包含 1000 万 条目的小表,使用 Python 脚本:

import uuid

rows=10000000

output='insert_description_bigint.sql'
f = open(output, 'w')

set_schema="SET search_path = norma;\n"
f.write(set_schema)

for i in range(1,rows):
    random_string=uuid.uuid4()
    query="insert into description_bigint (description_id, description) values (%d, '%s'); \n"
    f.write(query % (i,random_string))

这就是我创建两个表的方式:

-- BIGINT

DROP TABLE IF EXISTS description_bigint;

CREATE TABLE description_bigint
(
  description_id BIGINT PRIMARY KEY NOT NULL,
  description VARCHAR(200),
  constraint description_id_positive CHECK (description_id >= 0)
);

select count(1) from description_bigint;
select * from description_bigint;
select * from description_bigint where description_id = 9999999;

-- INT

DROP TABLE IF EXISTS description_int;

CREATE TABLE description_int
(
  description_id INT PRIMARY KEY NOT NULL,
  description VARCHAR(200),
  constraint description_id_positive CHECK (description_id >= 0)
);

插入所有这些数据后,我对两个表都进行了查询,以衡量它们之间的差异。令我惊讶的是,它们都具有相同的性能:

select * from description_bigint; -- 11m55s
select * from description_int; -- 11m55s

我的基准测试有问题吗? int 不应该比 bigint 快吗?特别是,当 primary key 定义为 index 时,这意味着为 bigint 创建索引会更慢 而不是为具有相同数据量的 int 创建索引,对吗?

我知道这不仅仅是一件会对我的数据库性能产生巨大影响的小事,但我想确保我们使用最佳实践并专注于这里的性能。

最佳答案

在 64 位系统中,这两个表几乎相同。 description_int 中的列 description_id 包含 8 个字节(4 个用于整数,4 个用于对齐)。试试这个测试:

select 
    pg_relation_size('description_int')/10000000 as table_int, 
    pg_relation_size('description_bigint')/10000000 as table_bigint,
    pg_relation_size('description_int_pkey')/10000000 as index_int,
    pg_relation_size('description_bigint_pkey')/10000000 as index_bigint;

两个表的平均行大小几乎相同。这是因为整数列占用 8 个字节(4 个字节用于值和 4 个字节的对齐)与 bigint 完全一样(8 个字节用于没有填充的值)。这同样适用于索引条目。然而,这是一个特例。如果我们在第一个表中再添加一个整数列:

CREATE TABLE two_integers
(
  description_id INT PRIMARY KEY NOT NULL,
  one_more_int INT,
  description VARCHAR(200),
  constraint description_id_positive CHECK (description_id >= 0)
);

平均行大小应保持不变,因为前 8 个字节将用于两个整数(无填充符)。

Calculating and saving space in PostgreSQL 中查找更多详细信息.

关于performance - 基准 : bigint vs int on PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38053596/

相关文章:

c - 逐行读取文件,将整数存储在数组中

java - 在 java 中克隆和编辑 int[][] - 无法更改 int[][]

c# - ASP.NET 启动性能分析 web

performance - Haskell 计算性能

sql - 优化 BETWEEN 日期语句

python - psycopg2.InternalError : how can I get more useful information?

java - 具有队列基​​本功能的最快 Java 集合是什么?

c# - 在 Property Setter 中,仅在值不同时设置是否有益?

sql - 挣扎于简单的 JOIN

java - 从文件中读取数组。 (java)