Como você eficientemente determinar se uma tabela Postgres tem linhas

votos
1

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)
);
Publicado 24/10/2008 em 16:57
fonte usuário
Em outras línguas...                            


6 respostas

votos
0

Como uma contagem no campo de chave primária onde não é NULL, o que limita a consulta a 1 resposta?

Desde uma chave primária deve existir, se houver um, você tem os dados, sim?

Respondeu 24/10/2008 em 17:00
fonte usuário

votos
1

Se tudo o que importa é 1 linha ou nenhuma linha. Limitar sua consulta para a primeira linha - por que contar todas as linhas apenas para descobrir se há 1 ou mais, ou zero ...

usar o equivalente a ROWNUM = 1 ou TOP 1 ou qualquer postgres lhe dá.

Respondeu 24/10/2008 em 17:03
fonte usuário

votos
2

Você pode encontrar este útil.

Respondeu 24/10/2008 em 17:39
fonte usuário

votos
1

Tente isto:

SELECT t.primary_key IS NOT NULL FROM table t LIMIT 1;

Você receberá TRUE se há registros e NULL se não há nenhum.

Respondeu 24/10/2008 em 18:41
fonte usuário

votos
7

selecionar verdadeiro do limite da tabela 1;

Respondeu 13/11/2008 em 10:03
fonte usuário

votos
2
select exists(select * from your_table_here) as has_row
Respondeu 07/11/2009 em 02:28
fonte usuário

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more