Note that there are some explanatory texts on larger screens.

plurals
  1. POActiveDirectory DirectorySearcher: why is FindOne() slower than FindAll() and why are properties omitted?
    primarykey
    data
    text
    <p>I have a loop that retrieves some info from ActiveDirectory. It turned out to be a big performance bottleneck.</p> <p>This snippet (inside a loop that executed it 31 times) took 00:01:14.6562500 (1 minute and 14 seconds):</p> <pre><code>SearchResult data = searcher.FindOne(); System.Diagnostics.Trace.WriteLine(PropsDump(data)); </code></pre> <p>Replacing it with this snippet brought it down to 00:00:03.1093750 (3 secconds):</p> <pre><code>searcher.SizeLimit = 1; SearchResultCollection coll = searcher.FindAll(); foreach (SearchResult data in coll) { System.Diagnostics.Trace.WriteLine(PropsDump(data)); } </code></pre> <p>The results are exactly identical, the same properties are returned in the same order. I found some info on memory leaks in <a href="https://stackoverflow.com/questions/1135013/is-directorysearcher-sizelimit-1-for-findall-equal-to-findone-directoryse">another thread</a>, but they did not mention performance (I'm on .Net 3.5).</p> <hr> <p>The following is actually a different question but it gives some background on why I'm looping in the first place:</p> <p>I wanted to get all the properties in one single query, but I cannot get the DirectorySearcher to return all the wanted properties in one go (it omits about 30% of the properties specified in PropertiesToLoad (also tried setting it in the constructor wich makes no difference), I found someone else had the same problem and <a href="http://forums.asp.net/t/1822574.aspx/1" rel="nofollow noreferrer">this is his solution (to loop through them)</a>. When I loop through them like this, either using FindOne() or FindAll() I do get all the properties, But actually it all feels like a workaround.</p> <p>Am I missing something?</p> <hr> <p>Edit:</p> <p>Seems like the problem was with the way I got the first DirectoryEntry on which I was using the DirectorySearcher.</p> <p>This was the code that caused the DirectorySearcher only to return some of the properties:</p> <pre><code>private static DirectoryEntry GetEntry() { DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "SERVERNAME", "USERNAME", "PASSWORD"); Forest forest = Forest.GetForest(dc); DirectorySearcher searcher = forest.GlobalCatalogs[0].GetDirectorySearcher(); searcher.Filter = "OU=MyUnit"; searcher.CacheResults = true; SearchResultCollection coll = searcher.FindAll(); foreach (SearchResult m in coll) { return m.GetDirectoryEntry(); } throw new Exception("DirectoryEntry not found"); } </code></pre> <p>After replacing that big mouthfull with just this line, the DirectorySearcher returned all the properties and looping was no longer needed:</p> <pre><code>private static DirectoryEntry GetEntry2() { return new DirectoryEntry(@"LDAP://SERVERNAME/OU=MyUnit,DC=SERVERNAME,DC=local", "USERNAME", "PASSWORD"); } </code></pre> <p>Now it takes less than one 18th of a second to get all wanted properties of 31 entries. So, it seems that two different instances of the same DirectoryEntry can give different results depending on the way it was constructed... feels a bit creepy!</p> <hr> <p>Edit</p> <p>Used <a href="http://www.jetbrains.com/decompiler/" rel="nofollow noreferrer">JetBrains DotPeek</a> to look at the implementation. The FindOne function starts like this:</p> <pre><code>public SearchResult FindOne() { SearchResult searchResult1 = (SearchResult) null; SearchResultCollection all = this.FindAll(false); ... </code></pre> <p>My first reaction was Argh! no wonder... but then I noticed the argument. FindAll has a private version that accepts a boolean, this is the start of FindAll:</p> <pre><code>[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public SearchResultCollection FindAll() { return this.FindAll(true); } private SearchResultCollection FindAll(bool findMoreThanOne) { ... // other code this.SetSearchPreferences(adsSearch, findMoreThanOne); </code></pre> <p>So this gives slightly more insight, but does not really explain much.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload