I have a D7 site with around 300,000 nodes. I am passing this function an array of 300-500 nids. Sometimes the function runs in under a second. Sometimes it takes 40 seconds or more! I can't figure out what causes the slow runs. How can I get the query consistent and fast?

    function _load_field_values_for_nids($field_name, $nids)
    {
      $field_info = field_info_field($field_name);
      $field_id = $field_info['id'];
    
      $map = array();
      $nodes_without_required_field = [];
    
      // Load up the properties from the node table.
      $sql = 'SELECT * FROM {node} WHERE nid IN (:nids)';
      $nodes = db_query($sql, array(':nids' => $nids))->fetchAllAssoc('nid');
    
      // Attach the single field to all nodes.
      field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' => $field_id));
    
      foreach ($nodes as $nid => $node) {
    
        if (isset($node->{$field_name}['und'])) {
          $map[$node->nid] = $node->{$field_name}['und'][0]['value'];
        } else {
          $nodes_without_required_field[] = $node->nid;
        }
      }
    
      $absent_count = count($nodes_without_required_field);
      if ($absent_count > 0) {
        watchdog("vwyw_load_fields", $absent_count." nodes did not have field, \"".$field_name."\" ");
      }
    
      return $map;
    }


I noticed this SIX-SECOND query on the main /admin/content page (which took 12+seconds to load):

[![Six Second Query to Count Available Content][1]][1]

Here is the expanded XHProf output regarding that Count query:

[![enter image description here][2]][2]

I think this might be a mysql configuration issue rather that the particulars of a query. For instance, I think this query should run very quickly but it takes a second and a half:

[![enter image description here][3]][3]

The next time (same page load) it took 120ms and the time after that, 4.8sec!

**UPDATE**

Here is the XHProf output sorted by duration for a simple admin page that calls the function:

[![enter image description here][4]][4]


  [1]: https://i.sstatic.net/TVId2.png
  [2]: https://i.sstatic.net/7WaqN.png
  [3]: https://i.sstatic.net/1sHQU.png
  [4]: https://i.sstatic.net/JOcEf.png