3 liens privés
Saviez-vous que la pagination par offset est très complexe mais facile à éviter ?
La clause offset demande à la base de données d'ignorer les N premiers résultats d'une requête. Cependant, la base de données doit toujours récupérer ces lignes du disque avant de pouvoir renvoyer les lignes suivantes.
Ce n'est pas un problème provenant de l'implémentation, c'est à la base du concept même d'offset :
… les lignes sont tout d'abord triées suivant la <clause order by>, puis sont filtrées en supprimant le nombre de lignes indiqué dans la <clause offset> à partir du début…
SQL:2023, Part 2, §4.17.3 Derived tables
Autrement dit, les gros décalages imposent un long travail à la base de données, qu'elle soit SQL ou NoSQL.
sqldef
sqldef is a CLI tool for diffing two SQL schemas. You can use it to manage the migration of RDBMSs using regular SQL DDLs.
Supported databases: MySQL, MariaDB, TiDB, PostgreSQL, SQL Server, and SQLite3.
Online Demo
PostgreSQL conditional upserts
So in order to do an "upsert" we use an INSERT command with a CONFLICT statement. Then if we need to do some conditional setting in the case of existing data then we can use the "temp table" EXCLUDED that PostgreSQL provide to us. They have a nice example how to use that in their docs and here's mine example:
implementation d’une hashchain in sqlite
The Unreasonable Effectiveness of SKIP LOCKED in PostgreSQL
Using SKIP LOCKED to distribute work across machines in a distributed system is a popular pattern. We explore why, the alternatives, and a real world example from our own codebase.
Nadeesha Cabral on 17-12-2024
When building distributed systems that need to process jobs or tasks concurrently, one of the most challenging problems is ensuring that work is distributed efficiently without duplication. PostgreSQL's SELECT FOR UPDATE SKIP LOCKED feature provides an elegant and performant solution to this problem that almost feels too good to be true. We're big believers in the just use postgres ethos, as our source code might tell you.