Sunday, April 29, 2012

SharePoint Performance - List.Items.Count Vs List.ItemCount

There are some common coding mistakes which severely hits the SharePoint performance. Let me take an example on List.Items.Count Vs List.ItemCount code.

Int itemCount = SPContext.Current.List.Items.Count;

The above code returns number of items from the list, to get the number of items it retrieves all items of the list from the content database and then returns the count. This code might not be a problem for a small list, normally during development we don’t have large data in the list so it doesn’t reflect the performance hit but when you have a large list, this code will become performance overhead.

There is a different property on the SPList which can be used in such scenario and will be better for performance.Here is the alternative to the above code

Int itemCount = SPContext.Current.List.ItemCount;

In above code, SharePoint just query a single record from the List table from the content database. There is a redundant column for number of items in the list which stores the count, so the above code just fetch the number of items without querying the entire data.

So the best practice is to use SPContext.Current.List.ItemCount if you want to fetch item count.

No comments:

Post a Comment