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

138
Views
La consulta Django Trigram Similarity no es tan rápida como la consulta sin procesar con el mismo resultado

El objetivo es tener una búsqueda rápida de valores similares, en orden de similitud.

Esta es la consulta en Django: Model.objects.annotate(similarity=TrigramSimilarity('field_name', query)).filter(similarity__gt=0.3).order_by('-similarity').values('field_name')[0:30]

Lo anterior se convierte en esta consulta: SELECT "table"."field_name" FROM "table" WHERE SIMILARITY("table"."field_name", query_value) > 0.3 ORDER BY SIMILARITY("table"."field_name", query_value) DESC LIMIT 30

Eso toma alrededor de 1.3 segundos, pero si ejecuto esto en psql:

SELECT field_name FROM table WHERE field_name % 'query_value' ORDER BY similarity(field_name, 'query_value') DESC LIMIT 30

Es mucho más rápido y tiene la misma salida correcta. ¿Cómo puedo lograr esto con Django, además de poner el comando raw sql?

Otra pregunta es cómo agregar SELECT DISTINCT field_name a eso. Actualmente, cuando intento agregarlo, ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list , aunque solo uso el nombre_del_campo seleccionado en la función de similitud ORDENAR POR.

¡Muchas gracias!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Una pregunta un poco antigua, pero últimamente me estaba equivocando con los trigramas y me encontré con un problema similar.

Lo muy importante es (como se menciona aquí: https://dba.stackexchange.com/questions/103821/best-index-for-similarity-function/103823#103823 ) ¡los índices no se usan cuando usa funciones! Tienes que usar operator-so % .

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!