Archive for the tag 'SPQuery'

Be selective about how you get items from an SPList

Just read an interesting article from Waldek Mastykarz called ‘Performance of various methods to retrieve one list item’. It’s well worth a look – go and read it.

So, the interesting thing I took away from it was just how slow the GetItemById() method was compared to an SPQuery. Why? Wouldn’t it make sense to use and SPQuery within the GetItemById() method?

Well a bit of Reflector digging (again) shows that it does! But it isn’t just a query for the item with that Id – it also specifies that the

SPQuery.ViewAttributes = "Scope=\"RecursiveAll\" ModerationType=\"Moderator\" ";

Hmm. So it’s probably a more complicated query. Depending on your solution, you might not need “RecursiveAll” – I try to avoid folders in lists, preferring views to break down my content.

So, the lesson that I took away was that if you are doing a LOT of getting items from lists, it is worth considering how you want to. There isn’t a right answer, unfortunately, and testing is key. The problem is, there are many factors:

  • Can you use caching?
  • Do you know the list you’re getting items from?
  • Are you getting items from more than one list?
  • Do you have folders?
  • What other parameters might your query have?

It’s difficult to know, hence I think Waldek is right – give it a test so you have some idea!

 Subscribe in a reader