Eu fiz isso testa e os resultados parece a escala função de contagem de forma linear. Eu tenho outra função depender fortemente na eficiência de saber se existem quaisquer dados, então eu gostaria de saber como substituir este SELECT COUNT (*) com outra consulta ou dados estrutura mais eficiente (talvez constante?).
psql -d testdb -U postgres -f truncate_and_insert_1000_rows.sql> NUL
psql -d TestDB -U postgres -f count_data.sql
-------------------------------------------------- ------------------------------
Agregado (custo = 36.75..36.76 linhas = 1 width = 0) (tempo real = 0.762..0.763 linhas = 1 laços = 1) -> Seq Scan no datos (custo = 0.00..31.40 linhas = 2,140 width = 0) (tempo real = 0,02 8..0.468 fileiras = 1000 lacetes = 1) tempo total: 0,846 ms (3 filas)
psql -d testdb -U postgres -f truncate_and_insert_10000_rows.sql> NUL
psql -d TestDB -U postgres -f count_data.sql
-------------------------------------------------- ------------------------------
Agregado (custo = 197.84..197.85 linhas = 1 width = 0) (tempo real = 6.191..6.191 linhas = 1 laços = 1) -> Seq Scan no datos (custo = 0.00..173.07 linhas = 9907 width = 0) (tempo real = 0,0 09..3.407 fileiras = 10000 voltas = 1) tempo total: 6.271 ms (3 filas)
psql -d testdb -U postgres -f truncate_and_insert_100000_rows.sql> NUL
psql -d TestDB -U postgres -f count_data.sql
-------------------------------------------------- ------------------------------
Agregado (custo = 2051.60..2051.61 linhas 1 = largura = 0) (tempo real = 74.075..74.076 r ows = 1 laços = 1) -> Seq Digitalizar no datos (custo = 0.00..1788.48 linhas = 105248 largura = 0 ) (tempo real = 0.032..46.024 fileiras = 100000 lacetes = 1) tempo total: 74.164 ms (3 filas)
psql -d prueba -U postgres -f truncate_and_insert_1000000_rows.sql> NUL
psql -d TestDB -U postgres -f count_data.sql
-------------------------------------------------- ------------------------------
Agregado (custo = 19720.00..19720.01 linhas 1 = largura = 0) (tempo real = 637.486..637.4 87 fileiras = 1 lacetes = 1) -> Seq digitalização na datos (custo = 0.00..17246.60 linhas = 989,360 largura = 0 ) (tempo real = 0.028..358.831 fileiras = 1000000 lacetes = 1) tempo total: 637.582 ms (3 filas)
a definição de dados é
CREATE TABLE data
(
id INTEGER NOT NULL,
text VARCHAR(100),
CONSTRAINT pk3 PRIMARY KEY (id)
);













