1

I've created a dynamic paging model to present data in pages for my projects.

In my controller I have two calls for the DB :

    public async Task<IActionResult> Index(int? page)
    {
        ...
        var count = await _context.Agent.CountAsync();
        var items = await _context.Agent.Skip((_page - 1) * pageSize).Take(pageSize).ToListAsync();
        ...

The paging works fine. I was just wondering if that's the best way in terms of performances.

As you can see, I'm making two calls to the DB, one for count to know the amount of pages, and one to get the actual data.

I don't know enough about this DB calls to know if they're being processed in the DB it self, or in the server side. If both are being processed in the DB, then everything is fine as we're saving resources , but if they're being processed on the server, I think it's better to use one call and store it in a DbSet , and manipulate the object.

So, what's better?

Tseng
  • 61,549
  • 15
  • 193
  • 205
sagi
  • 40,026
  • 6
  • 59
  • 84
  • Just a whole other kind of advise; make it: `Index(int? page, int? pageSize)` – Stefan Oct 03 '17 at 09:48
  • Is it related https://stackoverflow.com/a/7771298/1050927? – Prisoner Oct 03 '17 at 09:50
  • @Prisoner Yes, as I said that's sort of what I want to do in case the DB calls are being processed in the server side , but if they're being processed in the DB it is better for me. I just don't know what happens . – sagi Oct 03 '17 at 09:52
  • You can check queries been executed in DB and at least you will know where those 2 calls lands (in DB or server) – Renatas M. Oct 03 '17 at 09:52
  • How do I do that? :) @Reniuz . And shouldn't there be information about this? Does it count the records in the DB(SELECT COUNT()..) or on the server – sagi Oct 03 '17 at 10:03
  • I am sure it makes 2 calls for DB. Just wanted you to know there is a way to see it in action with tool something like this: https://github.com/OleksiiKovalov/expressprofiler – Renatas M. Oct 03 '17 at 10:10
  • Depending on the provider used, but if you use SqlServer, use SSDT to debug the queries – Tseng Oct 03 '17 at 10:56

0 Answers0