title: SharePoint Library Query running very slow
body: <p>We have a SharePoint query running on a library of maybe 500 documents to pull back the highest version of the published documents that are flagged as active (have active=true in the Active column).</p>
  
  <p>This is taking way too long to run (about 3-5 seconds), which is frustrating the users.</p>
  
  <p>What can be done to the query below to speed it up (I would hope for this to be virtually instantaneous!)</p>
  
  <pre><code>using (var site = new SPSite(Helpers.ConfigurationFile.SharePointUrl().ToString()))
  {
      using (var web = site.RootWeb)
      {
          return
              web.Folders["Templates"].Files.OfType&lt;SPFile&gt;()
              .Where(file =&gt; file.Item.HasPublishedVersion)
              .Where(file =&gt; file.Item.Properties["Active"].ToString() == "true")
              .Where(file =&gt; file.Item.Versions.OfType&lt;SPListItemVersion&gt;()
                  .Any(x =&gt; x.Level == SPFileLevel.Published))
              .Select(file =&gt; new LibraryItem(
                                        file.Item.UniqueId,
                                        file.Item.ID, 
                                        file.Item.Title, 
                                        Helpers.ConfigurationFile.SharePointUrl()
                                            .ToString().AddPathSegment(file.Url), 
                                        true, 
                                        float.Parse(file.MajorVersion.ToString() 
                                        + "." + file.MinorVersion.ToString())
                                        )
  
                      )
              .ToList();
      }
  }
  </code></pre>