question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce db roundtrips

See original GitHub issue

Hi,

Is it possible to reduce db roundtrips by sending requests by batch ( Like NHibernate Future<T>() ) ?

For the paging example, it’s ok to have two requests, but could we send them togheter in one batch and parse the results one time ? :

public static List<Product> Search(string searchFor, int currentPage, int pageSize, out int totalRecords)
{
  using (var db = new DbNorthwind())
  {
    var products = from p in db.Product 
                   select p;

    if (searchFor != null)
    {
      products = from p in products 
                 where p.Name.Contains(searchFor) 
                 select p;
    }

    totalRecords = products.Count();

    return products.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
  }
}

Thank you,

Yassine

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
sdanylivcommented, Feb 25, 2019

Hi, @loudadyassine, not yet. But we had thoughts about that. Not as it is done in NHibernate but in our way.

But, I’ve spent 20 minutes for writing for you simple class which should help in your situation if database supports Window functions.

public static class QueryPaginator
{
	private class Envelope<T>
	{
		public int TotalCount { get; set; }
		public T Data { get; set; }
	}

	public static List<T> Paginate<T>(IQueryable<T> query, int page, int pageSize, out int totalRecords)
	{
		var withCount = from q in query
			select new Envelope<T>
			{
				TotalCount = (int)Sql.Ext.Count().Over().ToValue(),
				Data = q
			};

		var items = withCount.Skip((page - 1) * pageSize).Take(pageSize).AsEnumerable();

		using (var enumerator = items.GetEnumerator())
		{
			List<T> result;
			if (!enumerator.MoveNext())
			{
				totalRecords = 0;
				result = new List<T>();
			}
			else
			{
				totalRecords = enumerator.Current.TotalCount;
				result = new List<T>(pageSize);
				do
				{
					result.Add(enumerator.Current.Data);
				} while (enumerator.MoveNext());
			}

			return result;
		}

	}
}

And usage:

var items = QueryPaginator.Paginate(products, currentPage, pageSize, out totalRecords);
0reactions
Powerzcommented, Sep 3, 2021

Hi, @loudadyassine, not yet. But we had thoughts about that. Not as it is done in NHibernate but in our way.

But, I’ve spent 20 minutes for writing for you simple class which should help in your situation if database supports Window functions.

This is awesome, thank you 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reducing Round Trips
One of the best ways to reduce the load on your server and increase application responsiveness is to reduce the number of "round...
Read more >
c# - Reducing roundtrips to the server/database
Round trips are more relevant to latency than the total quantity of data being moved, so it really does make sense to optimize...
Read more >
Tip: Reduce round trips to the database server
Round trips to the server are more important than the amount of data being passed to the server. In many cases, the number...
Read more >
Reducing Database Round-Trips with Boring Queries
If your application relies on a substantial amount of database requests, strategies like reusing what's already available in the application ...
Read more >
High Performance Web - Reducing Database Round Trips
In an optimal situation the things we'll talk about will help to reduce the amount of time required to serve a result that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found