How to know total number of the filtered items when the indexed pagination is demanded? #29510
QuestionAssume that besides the pagination, there is the filtering (
Now, how to get the second number with the best performance? |
Answered by
kuishou68
Apr 27, 2026
Replies: 1 comment 1 reply
|
The standard approach is to use const where = { /* your filter conditions */ };
const page = 1;
const pageSize = 20;
const [items, total] = await prisma.$transaction([
prisma.post.findMany({
where,
skip: (page - 1) * pageSize,
take: pageSize,
orderBy: { createdAt: 'desc' },
}),
prisma.post.count({ where }),
]);
return {
items,
total, // filtered total (your "second number")
totalPages: Math.ceil(total / pageSize),
};
This avoids a double round-trip because |
1 reply
Answer selected by
TokugawaTakeshi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The standard approach is to use
$transactionto run both queries in parallel — one for the paginated items, one for the filtered total count — in a single round-trip:prisma.post.count({ where })gives you exactly the filtered item count across all pages — not the tota…