【发布时间】:2010-09-19 01:07:56
【问题描述】:
我做了这个测试,结果似乎计数函数是线性缩放的。我有另一个函数非常依赖效率来知道是否有任何数据,所以我想知道如何用另一个更有效的(可能是常数?)查询或数据结构替换这个 select count(*)。
psql -d testdb -U postgres -f truncate_and_insert_1000_rows.sql > NUL
psql -d testdb -U postgres -f count_data.sql
----------------------------------------------- ----------------------------------
聚合(成本=36.75..36.76 行=1 宽度=0)(实际时间=0.762..0.763 行=1 循环=1) -> Seq Scan on datos (cost=0.00..31.40 rows=2140 width=0) (实际时间=0.02 8..0.468 行=1000 次循环=1) 总运行时间:0.846 毫秒 (3 条)
psql -d testdb -U postgres -f truncate_and_insert_10000_rows.sql > NUL
psql -d testdb -U postgres -f count_data.sql
----------------------------------------------- ----------------------------------
聚合(成本=197.84..197.85 行=1 宽度=0)(实际时间=6.191..6.191 行= 1 个循环=1) -> Seq Scan on datos (cost=0.00..173.07 rows=9907 width=0) (实际时间=0.0 09..3.407 行=10000 循环=1) 总运行时间:6.271 毫秒 (3 条)
psql -d testdb -U postgres -f truncate_and_insert_100000_rows.sql > NUL
psql -d testdb -U postgres -f count_data.sql
----------------------------------------------- ----------------------------------
聚合(成本=2051.60..2051.61 行=1 宽度=0)(实际时间=74.075..74.076 r ows=1 循环=1) -> Seq Scan on datos (cost=0.00..1788.48 rows=105248 width=0) (实际时间= 0.032..46.024 行=100000 循环=1) 总运行时间:74.164 毫秒 (3 条)
psql -d prueba -U postgres -f truncate_and_insert_1000000_rows.sql > NUL
psql -d testdb -U postgres -f count_data.sql
----------------------------------------------- ----------------------------------
聚合(成本=19720.00..19720.01 行=1 宽度=0)(实际时间=637.486..637.4 87 行=1 循环=1) -> Seq Scan on datos (cost=0.00..17246.60 rows=989360 width=0) (实际时间 =0.028..358.831 行=1000000 循环=1) 总运行时间:637.582 毫秒 (3 条)
数据的定义是
CREATE TABLE data
(
id INTEGER NOT NULL,
text VARCHAR(100),
CONSTRAINT pk3 PRIMARY KEY (id)
);
【问题讨论】:
-
我也试过限制结果集:EXPLAIN ANALYZE select count(*) from data LIMIT 1;但响应时间非常相似......
-
那是因为你需要测试 SELECT * FROM
LIMIT 1.
标签: postgresql