db_insert a lot of records
You could define your query fields first, then set your field values in a loop before executing the query.

Example (not tested):

$query = db_insert('HDD')-&gt;fields(array(
  'HDDDAYS',
  'AL',
  'AR',
  ...
));

for ($s = 0; $s &lt;= $cnt; $s++) {
  $query-&gt;values(array(
    'HDDDAYS' =&gt; $time,
    'AL' =&gt; $output1[$sts[0]][$dts[$s]],
    'AR' =&gt; $output1[$sts[1]][$dts[$s]],
    ...
  ));
}

$query-&gt;execute();