etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.21k stars 129 forks source link

Change PostgresSearchBackend to optmize for UUID primary keys #303

Closed thiagoferreiraw closed 1 year ago

thiagoferreiraw commented 1 year ago

Context

After running some performance tests, I realized the way we are casting types for the model primary key vs searchentry.object_id was unefficient.

When we do a model.uuid_pk::text conversion, postgres lose the ability to use the primary key index and that makes the query way slower.

Queries and plans (testing with 100k records)

Note the primary key index was not used because of a type cast (partnershipledger.id::text)

In nearly 100k records, the execution went from 71ms to 7ms 💯

Before

Screenshot 2023-01-12 at 11 37 08

After

Screenshot 2023-01-12 at 11 38 02

Proposed sollution

If the primary key is UUID, we cast type in the watson_searchentry.id instead.

thiagoferreiraw commented 1 year ago

Hi @etianen!

Here's a performance improvement we made recently in our own codebase and we'd like to extend that to the community.

Let me know your thoughts about the proposed sollution and/or if I need to adequate the code to meet the project standards.

Thank you!

etianen commented 1 year ago

This is a great change, thank you!