title: LDAP Search function very slow
body: <p>I'm trying to use my ldap search, it's sort of working, however each search query takes like 10-20 seconds to give me back results. Although I have to work with like 50.000 entries (users) in my active directory and you would say no wonder it takes so much time, since ldap_search is O(N), I won't believe that it really needs so much time.</p>
  
  <p>I have a textbox where I can type names. After the third typed letter the search function triggers and passes the textbox.value as parameter to the PHP file below (via AJAX).</p>
  
  <pre><code>&lt;?php
  
  error_reporting(E_ERROR | E_PARSE);
  
  if(filter_input_array(INPUT_POST))
  {
  $term = filter_input(INPUT_POST, 'term');
  $username = 'username';
  $password = "password";
  $ldap_host = array('host1', 'host2', 'host3');
  $ldap_base_dn = "baseDN";
  
  foreach ($ldap_host as $host)
  {
      $connect = ldap_connect($host);
  
      if($connect)
      {
          break;
      }
  
      if(!$connect &amp;&amp; $host == end($ldap_host))
      {
          exit("&gt;&gt; Could not connect to any of the given LDAP servers. &lt;&lt;");
      }
  }
  
  ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
  ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
  ldap_set_option($connect, LDAP_OPT_SIZELIMIT, 20);
  
  $bind = ldap_bind($connect, $username, $password);
  $search_filter = '(&amp;(objectClass=person)(cn=*'.$term.'*))';
  $attributes = array();
  $attributes[] = 'givenname';
  $attributes[] = 'mail';
  $attributes[] = 'samaccountname';
  $attributes[] = 'sn';
  $attributes[] = 'cn';
  $result = ldap_search($connect, $ldap_base_dn, $search_filter, $attributes);
  $ArrayOfHumanoids = array();
  
  if (FALSE !== $result)
  {
  $entries = ldap_get_entries($connect, $result);
  
      for ($i = 0; $i &lt; $entries['count']; $i++)
  {
          if (!empty($entries[$i]['givenname'][0]) &amp;&amp;
          !empty($entries[$i]['mail'][0]) &amp;&amp;
          !empty($entries[$i]['samaccountname'][0]) &amp;&amp;
          !empty($entries[$i]['sn'][0]) &amp;&amp;
          'Shop' !== $entries[$i]['sn'][0] &amp;&amp;
          'Account' !== $entries[$i]['sn'][0])
          {
      $ad_users[strtoupper(trim($entries[$i]['samaccountname'][0]))] = array('email' =&gt; strtolower(trim
  
  ($entries[$i]['mail'][0])), 'first_name' =&gt; trim($entries[$i]['givenname'][0]), 'last_name' =&gt; trim($entries[$i]
  
  ['sn'][0]));
  
              array_push($ArrayOfHumanoids, $entries[$i]['cn'][0] . "+");
          }
  }
  }
  
  if(count($ArrayOfHumanoids) == 0)
  {
      echo "&lt;div&gt;Sorry, no match found!&lt;br&gt;&lt;/div&gt;";
  }
  else
  {
      foreach($ArrayOfHumanoids as $userVar)
      {
          echo $userVar;
      }
  }
  
  ldap_unbind($connect);
  }
  
  ?&gt;
  </code></pre>
  
  <p>$term is the searching parameter that I pass at each keystroke. In the AJAX callback function, as you see I return with ArrayOfHumanoids, which json callback gets and splits by + string. I have no problem with that part. I just don't get it why it is so slow. I'm new to LDAP though, or recursion. Thank you in advance, I appreciate any kind of tips!</p>