I am beginner php developer. I need use in my project function similarity from pgsql.
I have table:
CREATE TABLE IF NOT EXISTS table_name
(id serial primary key,
prg_id TEXT,
code TEXT,
city TEXT,
area TEXT,
street TEXT,
numer TEXT,,
address TEXT)
This is very big table.
Now i need search in this table.
I have SQL ex:
SELECT street,
similarity('street' , 'name street') AS similarity,
row_number() OVER () AS rank
FROM table_name
where street% 'name street'
order by rank LIMIT 1;
It’s work, but slow.
Is it possible to write this question better to make it more efficient?
2
Answers
Install the extension pg_trgm, create an index on this column:
And use EXPLAIN(ANALYZE, VERBOSE, BUFFERS, SETTINGS) for your SQL statement to get the query plan. Did it improve or not?
If you rewrite the query slightly and create a GiST index, it can be fast:
Then you have to use the distance operator
<->
instead of thesimilarity
function:That will give you the row where
street
is most similar to'name street'
. You can add aWHERE
condition if you want to exclude results that are not similar enough.