Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

Performance Guidance and SPList.Items.Add - no need to avoid using it

$
0
0

I've been reviewing a rather complex SharePoint application to pinpoint some performance issues and noticed that there was some lines of code which were something like:

SPListItem newItem = theList.Items.Add();

The list in question has over 44k items in it so I immediately thought that the issue was the use of Items.Add() as we have the MSDN article:

http://msdn.microsoft.com/en-us/library/bb687949(v=office.12).aspx

Telling us that using SPList.Items is "very bad", a quick google/bing will uncover a raft of blogs/articles stating the same thing, most of the articles recommend using a convention where you get the SPListItemCollection using the SPList.GetItems() method passing in some sort of dummy or empty query.

The thing was when I examined the ULS logs I did notice loads of entries with the Category Database with a message stating that a List item query elapsed time was in the region of anything between 4k - 350k milliseconds, but none of the entries were for the list that was being added to using SPList.Items.Add(). The site had lots of other lists with around 20k items in them and it was these lists that appeared to be the source of the ULS log entries (there's other code I've already pinpointed that is the source of these issues).

I then put together a basic list (just added one number column with a default value of 123) and populated the list with 25k items.

Then I wrote a simple console app which added items to the list using empty SPQuery objects and using the SPList.Items.Add() method. I did not get any performance differences using the SPList.Items.Add() method, each add operation only took in the region of 300 milliseconds.

Just to check other things I then accessed a SPListItem from the SPList.Items[Guid listItemUniqueId] accessor just to make sure I was able to see the performance issue I would expect with such large lists and sure enough using this accessor would take around 8 seconds to complete.

The console code I used is below:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace ScratchConsole
{
 class Program
 {
  static void Main(string[] args)
  {
   using(SPSite siteCollection = new SPSite("http://sharepointdevbox:1001"))
   using (SPWeb site = siteCollection.OpenWeb())
   {
    SPQuery minusOneQuery = new SPQuery();
    minusOneQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>-1</Value></Eq></Where>";
    SPQuery emptyStringQuery = new SPQuery();
    emptyStringQuery.Query = String.Empty;

    SPQuery currentItemQuery = new SPQuery();
    currentItemQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Number'>10000</Value></Eq></Where>";
   
    SPList testList = site.GetList("/Lists/Test");
    SPListItem currentItem = testList.GetItems(currentItemQuery)[0];
    Guid currentItemUniqueId = currentItem.UniqueId;
    SPListItem newItem = null;
    switch (args[0])
    {
     case "1":
      Console.WriteLine("Starting with minusOneQuery\nTime:{0}", DateTime.Now.ToString("HH:mm:ss.fffff"));
      newItem = testList.GetItems(minusOneQuery).Add();
      SPListItem currentItemAgain1 = testList.GetItemByUniqueId(currentItemUniqueId);
      break;
     case "2":
      Console.WriteLine("Starting with emptyStringQuery\nTime:{0}", DateTime.Now.ToString("HH:mm:ss.fffff"));
      newItem = testList.GetItems(emptyStringQuery).Add();
      SPListItem currentItemAgain2 = testList.GetItemByUniqueId(currentItemUniqueId);
      break;
     case "3":
      Console.WriteLine("Starting with Items\nTime:{0}", DateTime.Now.ToString("HH:mm:ss.fffff"));
      newItem = testList.Items.Add();
      //Remove this line below if all you want to do is test the adding of items.
      SPListItem currentItemAgain3 = testList.Items[currentItemUniqueId];
       break;
    }
     
    newItem["Title"] = "TitleAdded";
    newItem.Update();
    Console.WriteLine("Time:{0}", DateTime.Now.ToString("HH:mm:ss.fffff"));    
   }   
  }
 }
}
 

Anyone got any comments on this?

 


http://www.linkedin.com/in/colinrippey http://www.facebook.com/colinrippey http://twitter.com/finarne http://finarne.w

Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>