Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Índice esencial de PostgreSQL

Tengo una tabla con dos fechas como dateTo y dateFrom, me gustaría usar el enfoque de intervalo de fechas en las consultas y un índice esencial, pero parece que no funciona. La tabla se parece a:

 CREATE TABLE test ( id bigeserial, begin_date date, end_date date ); CREATE INDEX "idx1" ON test USING gist (daterange(begin_date, end_date));

Luego, cuando trato de explicar una consulta como:

 SELECT t.* FROM test t WHERE daterange(t.begin_date,t.end_date,'[]') && daterange('2015-12-30 00:00:00.0','2016-10-28 00:00:00.0','[]')

obtengo un Seq Scan.

¿Este uso del índice esencial es incorrecto o este escenario no es factible?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tiene un índice en la expresión daterange(begin_date, end_date) , pero consulta su tabla con daterange(begin_date, end_date, '[]') && ... . PostgreSQL no hará matemáticas en tu lugar. Para reformular su problema, es como si estuviera indexando (int_col + 2) y consultando WHERE int_col + 1 > 2 . Debido a que las dos expresiones son diferentes, el índice no se utilizará en ninguna circunstancia. Pero como puede ver, puede hacer los cálculos (es decir, reformular la fórmula) a veces.

Necesitarás:

 CREATE INDEX idx1 ON test USING gist (daterange(begin_date, end_date, '[]'));

O:

 CREATE INDEX idx2 ON test USING gist (daterange(begin_date, end_date + 1));

Nota : ambos crean un rango que incluye end_date . El último utiliza el hecho de que el intervalo de daterange es discreto.

Y use los siguientes predicados para cada uno de los índices anteriores:

 WHERE daterange(begin_date, end_date, '[]') && daterange(?, ?, ?)

O:

 WHERE daterange(begin_date, end_date + 1) && daterange(?, ?, ?)

Nota : el tercer parámetro del constructor de rango en el lado derecho de && no importa (en el contexto del uso del índice).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!