Todos os produtos
Search
Central de documentação

ApsaraDB RDS:Cardinality estimation (hll)

Última atualização: Oct 10, 2026

A extensão hll fornece o tipo de dados HyperLogLog. Esse tipo de dados permite estimar rapidamente métricas de negócios, como visualizações de página (PV) e visitantes únicos (UV).

Pré-requisitos

  • A versão principal da sua instância do RDS é o PostgreSQL 11 ou superior.

  • Se a versão principal da sua instância atender aos requisitos, mas o recurso não for compatível, atualize a versão secundária do mecanismo. Para mais informações, consulte Upgrade the minor engine version.

Informações de contexto

A extensão hll oferece suporte ao HyperLogLog (hll), um tipo de dados de comprimento variável e comportamento semelhante a um conjunto. Ele retorna valores distintos aproximados com um grau de precisão especificado. Por exemplo, um tipo de dados hll de 1280 bytes pode estimar dezenas de bilhões de valores distintos com alta precisão. A extensão hll é ideal para setores que exigem esse tipo de estimativa, como a análise de publicidade na internet. Ela permite estimar rapidamente métricas de negócios, como PV e UV.

Para mais informações sobre como usar a extensão hll, consulte postgresql-hll.

Para uma descrição detalhada do algoritmo, consulte o artigo HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm.

Criar a extensão hll

Conecte-se à sua instância e execute o seguinte comando para criar a extensão hll:

CREATE EXTENSION hll;

Operações básicas

  • Crie uma tabela que contenha um campo hll. Execute o seguinte comando:

    create table agg (id int primary key,userids hll);
  • Transforme o tipo de dados int em hll_hashval. Execute o seguinte comando:

    select 1::hll_hashval;

Operadores básicos

  • O tipo hll aceita os seguintes operadores:

    • =

    • !=

    • <>

    • ||

    • #

    Exemplo:

    select hll_add_agg(1::hll_hashval) = hll_add_agg(2::hll_hashval);
    select hll_add_agg(1::hll_hashval) || hll_add_agg(2::hll_hashval);
    select #hll_add_agg(1::hll_hashval);
  • O tipo hll_hashval aceita os seguintes operadores:

    • =

    • !=

    • <>

    Exemplo:

    select 1::hll_hashval = 2::hll_hashval;
    select 1::hll_hashval <> 2::hll_hashval;

Funções básicas

  • A extensão fornece funções de hash como hll_hash_boolean, hll_hash_smallint e hll_hash_bigint. Exemplo:

    select hll_hash_boolean(true);
    select hll_hash_integer(1);
  • A extensão fornece a função hll_add_agg para transformar o tipo de dados int no formato hll. Exemplo:

    select hll_add_agg(1::hll_hashval);
  • A extensão fornece a função hll_union para retornar a união de valores hll. Exemplo:

    select hll_union(hll_add_agg(1::hll_hashval),hll_add_agg(2::hll_hashval));
  • A extensão fornece a função hll_set_defaults. Defina a precisão. Exemplo:

    select hll_set_defaults(15,5,-1,1);
  • A extensão fornece a função hll_print para imprimir informações de depuração. Exemplo:

    select hll_print(hll_add_agg(1::hll_hashval));

Exemplo

create table access_date (acc_date date unique, userids hll);
insert into access_date select current_date, hll_add_agg(hll_hash_integer(user_id)) from generate_series(1,10000) t(user_id);
insert into access_date select current_date-1, hll_add_agg(hll_hash_integer(user_id)) from generate_series(5000,20000) t(user_id);
insert into access_date select current_date-2, hll_add_agg(hll_hash_integer(user_id)) from generate_series(9000,40000) t(user_id);
postgres=# select #userids from access_date where acc_date=current_date;
     ?column?
------------------
 9725.85273370708
(1 row)
postgres=# select #userids from access_date where acc_date=current_date-1;
     ?column?
------------------
 14968.6596883279
(1 row)
postgres=# select #userids from access_date where acc_date=current_date-2;
     ?column?
------------------
 29361.5209149911
(1 row)
This feature is supported in RDS PostgreSQL 11 and later versions. If you are prompted that this feature is not supported, upgrade the minor engine version. For more information, see Upgrade a minor engine version.