【发布时间】:2011-07-09 05:52:04
【问题描述】:
假设 PostgreSQL 在 64 位服务器上运行,int4(32 位)和 int8(64 位)列之间的性能差异是什么?手册上说int4比int8效率高,但是如果底层服务器是64位的,有没有实际的性能差异(就(1)cpu、(2)内存和(3)存储而言)?
【问题讨论】:
标签: performance postgresql integer
假设 PostgreSQL 在 64 位服务器上运行,int4(32 位)和 int8(64 位)列之间的性能差异是什么?手册上说int4比int8效率高,但是如果底层服务器是64位的,有没有实际的性能差异(就(1)cpu、(2)内存和(3)存储而言)?
【问题讨论】:
标签: performance postgresql integer
在 (1) cpu、(2) 内存和 (3) 存储方面
说白了:
64 位是 32 位的两倍。
64 位是 32 位的两倍。
64 位是 32 位的两倍。
我记得 wp-hackers 中的一个线程做了一些基准测试。创建一个表,填写一百万行。然后查找、添加、分组、加入等。具体我不记得了,但是使用 int8 确实比使用 int4 慢。
test=# create table int4_test (id int primary key);
CREATE TABLE
test=# create table int8_test (id bigint primary key);
CREATE TABLE
test=# insert into int4_test select i from generate_series(1,1000000) i;
INSERT 0 1000000
test=# insert into int8_test select i from generate_series(1,1000000) i;
INSERT 0 1000000
test=# vacuum analyze;
VACUUM
test=# \timing on
Timing is on.
test=# select sum(i.id) from int4_test i natural join int4_test j where i.id % 19 = 0;
sum
-------------
26315710524
(1 row)
Time: 1364.925 ms
test=# select sum(i.id) from int4_test i natural join int4_test j where i.id % 19 = 0;
sum
-------------
26315710524
(1 row)
Time: 1286.810 ms
test=# select sum(i.id) from int8_test i natural join int8_test j where i.id % 19 = 0;
sum
-------------
26315710524
(1 row)
Time: 1610.638 ms
test=# select sum(i.id) from int8_test i natural join int8_test j where i.id % 19 = 0;
sum
-------------
26315710524
(1 row)
Time: 1554.066 ms
test=# select count(*) from int4_test i natural join int4_test j where i.id % 19 = 0;
count
-------
52631
(1 row)
Time: 1244.654 ms
test=# select count(*) from int4_test i natural join int4_test j where i.id % 19 = 0;
count
-------
52631
(1 row)
Time: 1247.114 ms
test=# select count(*) from int8_test i natural join int8_test j where i.id % 19 = 0;
count
-------
52631
(1 row)
Time: 1541.751 ms
test=# select count(*) from int8_test i natural join int8_test j where i.id % 19 = 0;
count
-------
52631
(1 row)
Time: 1519.986 ms
【讨论】:
在存储和内存方面,答案很明显:INT8 是 INT4 的两倍,因此它使用两倍的存储和两倍的内存。
就计算 (CPU) 性能而言,我怀疑它在 64 位机器上根本没有区别,并且在某些情况下,INT4 在 32 位机器上可能更高效。尽管除非您对这些 INT 进行复杂的数学运算(而不仅仅是将它们用作串行等),否则计算差异可能为零,或几乎为零。
一旦你开始用你的 INT 做复杂的事情,它就不再是一个真正的数据库性能问题了。
【讨论】: