Answer the question
In order to leave comments, you need to log in
How to speed up fetching with pagination in PostgreSQL?
I want to search for a song by title with pagination. There are two tables: one table with the name of the song, the second - with the name of the artist. The first table has 20 million rows, the second table has 1.5 million rows.
Approximately look like this:
create table recording (
"id" serial primary key,
"name" varchar,
"artist_credit" serial REFERENCES artist_credit (id),
);
create table artist_credit (
"id" serial primary key,
"name" varchar
);
self.searchGlobal = function (obj) {
return db.query(
"SELECT
recording.id AS \"recordingId\", artist_credit.name AS \"artistName\", recording.name AS \"trackName\",
FROM recording
INNER JOIN artist_credit ON recording.artist_credit = artist_credit.id
WHERE recording.name ~ ${regex} AND recording.id > ${index}
ORDER BY recording.id
LIMIT ${limit};", obj);
};
WHERE id NOT IN [array]
Answer the question
In order to leave comments, you need to log in
so you don’t have sorting or pagination slowing down, but searching by regexp. which is not indexed. replace it with full-text search (tsvector and so on) or indeed with some external indexer, such as solra or sphinx, translate.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question