I’m experimenting with a SearchVectorField in order to speed up searching in the Django Admin for a very large table. I’ve added a SearchVectorField and a GinIndex to the model, along with a trigger to automatically update the field when the relevant columns change.
My question is about the query generated when searching this table.
Using the Django Debug Toolbar, if I search for a string foo in the admin, I see this query:
SELECT COUNT(*) AS "__count"
FROM "mymodel"
WHERE "mymodel"."search_vector"::text LIKE '%foo%';
PostgreSQL documentation for queries using tsvector column types look more like this:
SELECT count(*)
FROM mymodel
WHERE search_vector @@ to_tsquery('foo');
I also tried running these queries in psql and the difference between the two is staggering. The Django-generated takes about 2.1 seconds but the query using @@ takes about 3 milliseconds.
Running explain, I see a similar difference in magnitude in the cost reported for each query.
I’m curious to know if anybody who has worked with PostgreSQL full text search, and particularly SearchVectorFields, has explored this and found better results.
Thanks
(Django 4.2 LTS, PostgreSQL 15.13, python 3.12.11, psycopg2 2.9.3)