Get all items from a list performance

Running on a list with 4,000 items. Option 1 is on average 1 second slower than Option 2.

Option 1:

using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb currentWeb = currentSite.OpenWeb(“crm”))
{
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
string result = “”;
SPList myList_TrainingRecords = currentWeb.Lists[“Personnel”];
SPListItemCollection listItemCollection = myList_TrainingRecords.Items;
foreach (SPListItem item in listItemCollection)
{
result = Convert.ToString(item[“Surname”]);
}
dt2 = DateTime.Now;
TimeSpan duration = dt2 – dt1;
lblTesting.Text = “Result: ” + result + ” ” + duration.TotalMilliseconds;
}
}

 

Option 2:

using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb currentWeb = currentSite.OpenWeb(“crm”))
{DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
string result = “”;
SPList myList_TrainingRecords = currentWeb.Lists[“Personnel”];
SPQuery qry = new SPQuery();
qry.RowLimit = 1999;
do
{
SPListItemCollection listItemCollection = myList_TrainingRecords.GetItems(qry);
foreach (SPListItem item in listItemCollection)
{
result = Convert.ToString(item[“Surname”]);
}
qry.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;
} while (qry.ListItemCollectionPosition != null);
dt2 = DateTime.Now;
TimeSpan duration = dt2 – dt1;
lblTesting2.Text = “Result: ” + result + ” ” + duration.TotalMilliseconds; }
}

 

Advertisements
This entry was posted in SharePoint 2010, Visual Studio 2010, SharePoint 2016. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s