S
S
Sergey Volkov2015-06-03 10:01:46
PostgreSQL
Sergey Volkov, 2015-06-03 10:01:46

How to reduce planning time in postgresql?

The site server uses laravel and postgresql.
For search, full-text search (tsearch2) using Russian dictionaries is used.
The problem is that the search for Russian text takes longer than the search for Latin characters.
I'll show you an example
Request with Russian text

explain analyze
SELECT *
FROM lots 
WHERE search_index @@@ to_tsquery('рус:*') 
LIMIT 100 
OFFSET 100

We get
Planning time: 494.529 ms
Execution time: 3.871 ms

Request with English text
explain analyze
SELECT *
FROM lots 
WHERE search_index @@@ to_tsquery('eng:*') 
LIMIT 100 
OFFSET 100

We get
Planning time: 2.229 ms
Execution time: 0.700 ms

The results show that in the first case the Planning Time is almost half a second, while in the second case it is 2.229 ms.
This Planning Time would like to be reduced.
Important. Noticed one feature. I am running the above queries through pgAdmin3. I open a window for entering a sql query, enter a query, execute, Planning Time = 0.5 sec. But if, without closing the window, this query is executed a second time, then Planning Time = 0.3 ms . But on the server it turns out that each request is made as the first time, that is, with a large Planning Time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
He11ion, 2015-06-03
@format1981

I think because the plan gets into the cache.
As an option - use prepared queries or do not break the connection (pgbouncer to the rescue).

I
Igor Rusinov, 2015-06-03
@RusinovIG

It would be nice to see the full output of EXPLAIN ANALYZE, but I assume that the reason is that the first query pulls all the data from the disk, and when running the second query, postgresql already cached some of the data. Well, as He11ion said, the query plan is also cached for the session. Accordingly, the comparison is not correct. If you start the search with English words first, and then in Russian, I think the result of the experiment will be the opposite.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question