Performance : Caml query taking long time to load or something else
-
10-12-2019 - |
Question
I have a CAML query which provides a result, if a SPfolder exists or not. When i do the item.Count it takes around 2sec for the line to execute . What is going wrong and taking long time to execute the query ?
SPQuery query = new SPQuery();
query.Query = "<Where><And><Eq><FieldRef Name='LinkTitle'/><Value Type='Text'>" + folderName + "</Value></Eq><Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>1</Value></Eq></And></Where>";
query.ViewAttributes = "Scope=\"RecursiveAll\"";
//Retrieve the items based on Query
SPListItemCollection items = list.GetItems(query);
//if (!folderExists)
if (items.Count == 0)
{
//Operation
}
Solution
How many fields are in here? Is it a standard document library, or did you add some extra metadata? Even if it is normal, do you need to pull all of the fields in question? Try modifying the ViewFields attribute of your query to return only the stuff that you need. Otherwise, you're traversing all of the folders (the "RecursiveAll" scope attribute); if you have a large library, particularly one with lots of levels, that may well take a while. If on the other hand you have a good idea of what folder the item might be in, you might consider cutting the search down to a single subfolder.