N
N
NubasLol2018-09-24 11:51:50
PostgreSQL
NubasLol, 2018-09-24 11:51:50

PostgresQl why is the second query faster than the first one?

I'm doing a full-text search on postgres, but for some reason it works very slowly, but especially the first query. For example, the word president occurs in 7,000 lines out of 120k. The first request takes about 6 seconds, and the second one takes 400ms. I have tsvector stored in a single column and create a gin index on it

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
good_beginer, 2018-09-24
@good_beginer

To increase the speed of queries, it would be necessary to index the tables, there will be a noticeable difference, but as for the time difference, in my opinion, this is just caching, that is, when it is requested again, it simply displays the last result and also looks for a difference between the current state of the table and what is in memory

M
Melkij, 2018-09-24
@melkij

See explain (analyze, buffers), highly desirable with track_io_timing enabled. Almost certainly you are reading the first request from the disk, the second - already from shared_buffers. Or from the page cache of the operating system.

L
lega, 2018-09-24
@lega

What would be fast:
1) take the required amount of data, for example, 50-100 first, instead of pumping everything out.
2) Use specialized tools (sphinxsearch/elasticsearch).

A
Andrey, 2018-09-24
@VladimirAndreev

because postgre caches the data it read to execute the request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question