<p>I have code looking something like this:</p>

<pre><code>$data = file_get_contents($tempFile); // perhaps 30MB of file data, now in PHP's memory
$hash = md5($data);
$query = "INSERT INTO some_table
          SET BlobData = '" . mysql_real_escape_string($data) . "',
          BlobHash = '$hash'
          ";
mysql_query($query);
</code></pre>

<p>I know this isn't very efficient as each of the '.' operators will reallocate a bigger memory block and the 30MB string will be copied several times.</p>

<p>Is there anything more efficient than the following solution?</p>

<pre><code>$data = file_get_contents($tempFile); // perhaps 30MB of file data, now in PHP's memory
$hash = md5($data);
$query = "INSERT INTO some_table SET BlobData = '%s', BlobHash = '$hash'";
mysql_query(sprintf($query, mysql_real_escape_string($data)));
</code></pre>

Have you benchmarked the output buffering trick?
```
```
ob_start();
echo 'INSERT INTO some_table SET BlobData = \'', mysql_real_escape_string( $data ), '\', BlobHash = \'', $hash, '\'';
mysql_query( ob_get_clean() );
```
```
Another thing you could do is convert to mysqli or MDB2, which support bound parameters.  That would allow you to skip the mysql_real_escape_string call
and
the string concatenations.