Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
LIKE with trailing wildcard in Postgres doesn't use the index when the prefix is a result of a subquery

I have the following table

CREATE TABLE test(id bigint primary key, path varchar);

with the additional index on path column

CREATE INDEX test_path_idx ON test(path varchar_pattern_ops);

With the query like this

SELECT * FROM test WHERE path LIKE 'prefix%';

the index on path is used. It's still used with

SELECT * FROM test WHERE path LIKE 'prefix' || '%';

Index Only Scan using test_path_idx on test  (cost=0.41..8.43 rows=1 width=511)
  Index Cond: ((path ~>=~ 'prefix'::text) AND (path ~<~ 'prefiy'::text))
  Filter: ((path)::text ~~ 'prefix%'::text)

However, if the prefix is a result of a subquery

SELECT * FROM test WHERE path LIKE (SELECT 'prefix')::varchar || '%';

Seq Scan on test  (cost=0.01..359.93 rows=20 width=511)
  Filter: ((path)::text ~~ ((($0)::character varying)::text || '%'::text))
  InitPlan 1 (returns $0)
    ->  Result  (cost=0.00..0.01 rows=1 width=32)

the index is not used. Of course, in reality the subquery is more complicated but the above suffices to make the optimizer "ignore" the index.

I assume it happens because the optimizer cannot be sure there are no wildcards in the subquery result so it takes the "worst case" path. Is there a way to optimize this? Unfortunately, executing the inner select in a separate query and plugging in the result is not an option.

P.S. I have tested this on 9.6.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda