Reputation: 5827
for better pagination performance, instead of doing this in postgresql:
SELECT * FROM message ORDER BY created_at DESC limit 30 offset 30;
Can I store the IDs in redis sorted set and then simply get the IDs from redis by:
ZREVRANGE message_ids 30 39
And then query those IDs in the postgresql to get values.
The reason why I don't store value directly in Redis is because the value can be big and RAM is expensive. (I don't have enough RAM).
If this is recommened or OK to do to improve the performance, how can I query the values from postgresql by IDs properly?
I found the following ways and not sure if this is the best approach:
SELECT m.*
FROM unnest('{17579, 17580, 17582}'::int[]) id
JOIN message m USING (id);
OR simply:
SELECT * FROM message WHERE id IN (17579, 17580, 17582);
The above query is from this link
Btw, I am also not sure if the above command will give me the correct order based on the order of id list and whether or not I need a ORDER BY id
in the end.
In summary, will this redis+postgresql solution be much faster than only postgresql solution?
Upvotes: 2
Views: 1441
Reputation: 3120
In summary, will this redis+postgresql solution be much faster than only postgresql solution?
In my opinion, the end result will not be worth the effort / complexity that you would introduce into the system.
Databases offer very good performance for such queries, provided you index the fields you are querying on and select only the fields you need.
In your proposed solution following would happen while the application is trying to serve the page (its a web application right?):
In comparison, there is only one sql query being sent to the database if we choose the straight forward way.
Meanwhile, also consider the development effort involved in ensuring that the ids fed into redis stays consistent with the database server.
That said, if your database server is under heavy load and you need to take some of the burden off its shoulders you may consider including elasticsearch / solr into your application stack.
You may also consider setting up replication for postgresql, to distribute the load that only one database server may not be able to handle.
Upvotes: 3