1

How much % performance is increased or decreased on using buffered data vs unbuffered data in mysql using mysqli prepared statements.,

1. Buffered Data. e.g.

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

        if ($stmt = mysqli_prepare($link, $query)) {

        /* execute statement */
        mysqli_stmt_execute($stmt);

        /* bind result variables */
        mysqli_stmt_bind_result($stmt, $row['Name'], $row['CountryCode']);

        /* store result */
        mysqli_stmt_store_result($stmt);

        /* fetch values */
        while (mysqli_stmt_fetch($stmt)) {
            echo $row['Name'].'-'. $row['CountryCode'];
        }

        /* free result */
        mysqli_stmt_free_result($stmt);

        /* close statement */
        mysqli_stmt_close($stmt);
    }

/* close connection */
mysqli_close($link);    

2. Unbuffered data. e.g.

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

        if ($stmt = mysqli_prepare($link, $query)) {

        /* execute statement */
        mysqli_stmt_execute($stmt);

        /* bind result variables */
        mysqli_stmt_bind_result($stmt, $row['Name'], $row['CountryCode']);

        /* fetch values */
        while (mysqli_stmt_fetch($stmt)) {
            echo $row['Name'].'-'. $row['CountryCode']);
        }

        /* close statement */
        mysqli_stmt_close($stmt);
    }

/* close connection */
mysqli_close($link);    

the first example uses

mysqli_stmt_store_result($stmt);

which stores result data in buffer for later fetching

while another example fetches data directly to the client without storing in buffer .

You must call mysqli_stmt_store_result() for every query that successfully produces a result set (SELECT, SHOW, DESCRIBE, EXPLAIN), if and only if you want to buffer the complete result set by the client, so that the subsequent mysqli_stmt_fetch() call returns buffered data.

ref: http://php.net/manual/en/mysqli-stmt.store-result.php

so my question is how much performace increased if mysqli_stmt_store_result is used ?

4
  • Without access to your data and specific setup, only you can answer that question precisely. You can setup a timer and run the queries in a loop and you'll see how much of a difference it makes. Oct 14, 2014 at 18:20
  • @Pier-LucGendreau thanks, will surely do it.
    – user1642018
    Oct 14, 2014 at 18:23
  • If you do, it would also be helpful if you posted your methodology and findings as an answer. Oct 14, 2014 at 18:25
  • Why would you want to buffer the data in this use case? Really the main use case for using mysqli_store_result() is in a case where you want to arbitrarily access rows from the result set (by using mysqli_data_seek() for example). For your specific use case, have you tried to run some tests to see performance difference at all? The best way to determine performance for your application and your data is to test it.
    – Mike Brant
    Oct 14, 2014 at 18:49

2 Answers 2

1
+50

Although I haven't ever had a need for stored results I still found this question interesting and would like to know the same thing. I have created a database with junk data in it and ran some tests against it to see timing differences using unbuffered and buffered results. I've included my tests and findings in case it helps you (or anyone else for that matter).

Database Setup

The database consists of a single table with four different fields. Here is the schema:

ID          | int(5)    | primary_key, auto_increment
Name        | tinytext  | not_null
CountryCode | int(3)    | not_null
Description | tinytext  | not_null

The table has over 10,000 rows all with junk data. ID is filled with its auto-increment value, CountryCode is a random number between 11 and 999, and the last two fields, Name and Description, contain md5 hashes of numerical data.

The Tests

In total six different tests are done. The first 5 tests are comparisons of processing times between unbuffered and buffered results. The overall goal of the tests are to see benchmarks of different sized results sets starting at the beginning of the table and in the middle. The final test is only a benchmark of buffered results being accessed randomly versus sequentially. Unbuffered results cannot be arbitrarily through mysqli_stmt_data_seek() so a comparison would be difficult to craft and be considered fair.

Benchmark times are calculated using microtime(). Ticks are started right before the MySQLi statement is prepared and ended as soon as the statement is closed.

Here is a breakdown of the 6 tests:

  • Test 1: Compares unbuffered/buffered processing times of a result set of 100 rows selected from the beginning of the table.
  • Test 2: Compares unbuffered/buffered processing times of a result set of 100 rows selected from the middle of the table.
  • Test 3: Compares unbuffered/buffered processing times of a result set of 1000 rows selected from the beginning of the table.
  • Test 4: Compares unbuffered/buffered processing times of a result set of 1000 rows selected from the middle of the table.
  • Test 5: Compares unbuffered/buffered processing times of a result set of 5000 rows selected from the beginning of the table and repeated three times.
  • Test 6: Benchmark of the processing time of a result set of 10000 rows, accessed randomly, and repeated three times.

The Results

PHP produced the following output for the tests described above.

Test 1
Took 0.002000093460083 seconds to process unbuffered result of 100 rows from the beginning of the table
Took 0.0019998550415039 seconds to process buffered result of 100 rows from the beginning of the table


Test 2
Took 0.012001037597656 seconds to process unbuffered result of 100 rows from the middle of the table
Took 0.011001110076904 seconds to process buffered result of 100 rows from the middle of the table


Test 3
Took 0.013001918792725 seconds to process unbuffered result of 1000 rows from the beginning of the table
Took 0.012001037597656 seconds to process buffered result of 1000 rows from the beginning of the table


Test 4
Took 0.023001909255981 seconds to process unbuffered result of 1000 rows from the middle of the table
Took 0.020002126693726 seconds to process buffered result of 1000 rows from the middle of the table


Test 5
Took 0.19601988792419 seconds to process unbuffered result of 5000 rows sequentially, three times
Took 0.085008144378662 seconds to process buffered result of 5000 rows sequentially, three times


Test 6
Took 4.2634270191193 seconds to process buffered result of 10000 rows randomly, three times

Conclusion

Tests 1-4 show that the performance gain is negligible for the most part. Gains from buffering the results are minimal even when dealing with a lot of rows or taking records from various offsets in the table. Offsetting the location added some overhead (about a hundredth of second to advance 5000 rows).

That said, buffering still has its place. In test five, where a result set with several thousand rows is iterated multiple times, there is a clear benefit from using buffered results. Both the buffered and unbuffered versions of that test effectively process 15,000 rows. But because the buffered version does not have to retrieve the results again it is able to do its work in less than half of the time the unbuffered version does.

As pointed out already by others in this question, buffering is very helpful when having to access rows arbitrarily/randomly. Test six simply shows how long a buffered set of 10,000 rows can be accessed randomly and then repeated an additional two times. Test six effectively fetches 30,000 rows completely out of order.

Of Course, The Code

Here is the code I used to create this test. It is all procedural so it isn't the prettiest to look at, but if I ever find myself making a class out of this or modifying the code to clean it up, I'll be sure to update it here!

<?php

//tell PHP not to mind how long it is running
set_time_limit(0);
//control output for test results
ob_start();
//array to hold time values from the tests
$times = array();

//Connect to the database
$connection = mysqli_connect("localhost", "root", "", "blah");

/***********************************************************************
 * TEST 1: Small result set of 100 rows from the beginning of the table
 **********************************************************************/

$times['Test 1'] = array();

//UNBUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 100");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 1'][] = "Took $time seconds to process unbuffered result of 100 rows from the beginning of the table";

//BUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 100");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
mysqli_stmt_store_result($stmt);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 1'][] = "Took $time seconds to process buffered result of 100 rows from the beginning of the table";
echo "\n ** END TEST 1** \n\n";

/*******************************************************************
 * TEST 2: Small result set of 100 rows from the middle of the table
 ******************************************************************/

$times['Test 2'] = array();

//UNBUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 5000, 100");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 2'][] = "Took $time seconds to process unbuffered result of 100 rows from the middle of the table";

//BUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 5000, 100");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
mysqli_stmt_store_result($stmt);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 2'][] = "Took $time seconds to process buffered result of 100 rows from the middle of the table";
echo "\n ** END TEST 2** \n\n";

/***********************************************************************
 * TEST 3: Large result set of 1000 rows from the beginning of the table
 **********************************************************************/

$times['Test 3'] = array();

//UNBUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 1000");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 3'][] = "Took $time seconds to process unbuffered result of 1000 rows from the beginning of the table";

//BUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 1000");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
mysqli_stmt_store_result($stmt);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 3'][] = "Took $time seconds to process buffered result of 1000 rows from the beginning of the table";
echo "\n ** END TEST 3** \n\n";

/********************************************************************
 * TEST 4: Large result set of 1000 rows from the middle of the table
 *******************************************************************/

$times['Test 4'] = array();

//UNBUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 5000, 1000");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 4'][] = "Took $time seconds to process unbuffered result of 1000 rows from the middle of the table";

//BUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 5000, 1000");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
mysqli_stmt_store_result($stmt);

while (mysqli_stmt_fetch($stmt))
{
    printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 4'][] = "Took $time seconds to process buffered result of 1000 rows from the middle of the table";
echo "\n ** END TEST 4** \n\n";

/******************************************************************************
 * TEST 5: Work with larger result set, 5000 rows, multiple times, sequentially
 *****************************************************************************/

$times['Test 5'] = array();

//UNBUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 5000");

for ($i = 0; $i < 3; $i++)
{
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
    while (mysqli_stmt_fetch($stmt))
    {
        printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
    }
}
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 5'][] = "Took $time seconds to process unbuffered result of 5000 rows sequentially, three times";

//BUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 5000");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
mysqli_stmt_store_result($stmt);

for ($i = 0; $i < 3; $i++)
{
    mysqli_stmt_data_seek($stmt, 0);
    while (mysqli_stmt_fetch($stmt))
    {
        printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
    }
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 5'][] = "Took $time seconds to process buffered result of 5000 rows sequentially, three times";
echo "\n ** END TEST 5** \n\n";

/***************************************************************************
 * TEST 6: Work with larger result set, 10000 rows, multiple times, randomly
 **************************************************************************/

$times['Test 6'] = array();

//UNBUFFERED VERSION
//Can't test unbuffered result sets randomly as mysqli_stmt_data_seek
//only works on buffered results.

//BUFFERED VERSION
$benchmarkStart = microtime(true);
$stmt = mysqli_prepare($connection, "SELECT * FROM City LIMIT 10000");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name, $code, $desc);
mysqli_stmt_store_result($stmt);

for ($i = 0; $i < 3; $i++)
{
    $rows = range(0, (mysqli_stmt_num_rows($stmt) - 1));
    shuffle($rows);
    for ($j = 0; $j < 10000; $j++)
    {
        mysqli_stmt_fetch($stmt);
        printf("%d, %s, %d, %s \n", $id, $name, $code, $desc);
        $row = $rows[0];
        mysqli_stmt_data_seek($stmt, $row);
        array_shift($rows);
    }
}
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
$benchmarkEnd = microtime(true);

$time = $benchmarkEnd - $benchmarkStart;
$times['Test 6'][] = "Took $time seconds to process buffered result of 10000 rows randomly, three times";
echo "\n ** END TEST 6** \n\n";

/*******************
 * Print the results
 ******************/
$output = ob_get_clean();
foreach ($times as $tests => $results)
{
    echo $tests . "\n";
    foreach ($results as $result)
    {
        echo $result . "\n";
    }
    echo "\n\n";
}

//Dumps all of those rows that have been getting printed out to the browser.
//This kicked out a little north of 64,000 lines in my browser.
echo $output; 

?>
1
  • @AMB You are welcome! I learned something out of it too. Thanks for the bounty also! Oct 17, 2014 at 3:26
0

I think 90% of the time you won't see any difference since:

1- If you have a small amount of data everything will be fast enough it wont make much a difference.

2- If you have a big amount of data those data will be put in a buffer client side, which will save you time only if you use the same result set more then once.

Unfortunately, I cannot give you a definitive answer since only you know the amount of data the query will produce and if those will be needed more than once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.