Platform SDK: Active Directory, ADSI, and Directory Services
What Makes a Fast Query?
There are several "performance tweaking" concepts you should consider when executing a query. The following list identifies the most important ones.
If possible, search on indexed attributes only (see Schema Attribute Categories in the Active Directory reference section under Schema to see which attributes are indexed).Try to use index attributes which you expect will generate the fewest number of hits.
Search on objectCategory instead of objectClass since objectClass is not an indexed property.
Be aware of referrals. Consider searching the global catalog if your attributes are listed as GC replicated.
Stay away from searching for text in the middle and at the end of a string. For example, "cn=*hille*" or "cn=*larouse".
Assume a subtree search will return a large result set. Use paging when performing subtree searches. The server will then be able to stream a large result set in chunks reducing the server-side memory resources. This effectively flattens out network usage and reduces the need for sending extremely large chunks of data over the network.
Properly scope your searches so as to not retrieve more than is necessary.
Perform a complex search on multiple attributes, because it is less performance intensive than performing multiple searches. One search for an object that reads two attributes is cheaper than two searches for the same object, each returning one attribute.
For reading group memberships, learn to use range limits to minimize the search size so that you can read a few thousand members at a time.
Bind to an object once and hang onto the binding handle for the rest of your session. Do not bind and unbind for each call. If you are using ADO or OLE DB, do not create many connection objects.
Read the rootDSE once and remember its contents for the rest of your session.
Allow persistent references to objects such as GUIDs rather than DNs, so that you can maintain your ability to rename and delete objects. See Binding for more information about binding to a GUID.