Current Location: Home> Latest Articles> How to use microtime() to test the specific time-consuming process of querying and reading data in mysqli_result?

How to use microtime() to test the specific time-consuming process of querying and reading data in mysqli_result?

M66 2025-05-28

How to use microtime() to test the specific time-consuming of mysqli_result query and read data?

In PHP, we usually need to test the execution time of database query operations. To optimize the performance of the system, it is important to understand the time-consuming of each operation. The microtime() function is a very useful tool that returns the current Unix timestamp, containing the microseconds part, helping us make precise time measurements. This article will show how to use microtime() to test the specific time-consuming of mysqli_result query and read data.

1. Connect to the MySQL database

First, use mysqli extension to connect to the database. Here, let's assume that you already have a database server running and can connect through the following code:

 <?php
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'testdb';

$conn = new mysqli($host, $username, $password, $database);

// Check if the connection is successful
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

2. Use microtime() to measure the query start time

The microtime() function returns the current Unix timestamp, precise to microseconds. We use it to record the time when the query starts.

 <?php
// Get the time when the query started
$start_time = microtime(true);
?>

3. Execute database query

Here we execute a simple SQL query to get data from the database. It can be any query, here assumes it is a query that selects all data from the users table:

 <?php
$query = "SELECT * FROM users";
$result = $conn->query($query);

if (!$result) {
    die("Query failed: " . $conn->error);
}
?>

4. Read the query results

Once the query is executed successfully, we will start reading the results and measure the time it takes to read the data. We use microtime() to record the time when the read data starts.

 <?php
// Get the time when the read data starts
$read_start_time = microtime(true);

// Read all results
while ($row = $result->fetch_assoc()) {
    // Here you can process each row of data
    // Example:echo $row['name'];
}

// Get the time when the read data ends
$read_end_time = microtime(true);
?>

5. Time-consuming measurement query and time-consuming reading

Finally, we can calculate the time-consuming process from the beginning of the query to the completion of reading the data. Here is the code for how to calculate:

 <?php
// Time-consuming inquiry
$query_time = $read_start_time - $start_time;

// Time to read
$read_time = $read_end_time - $read_start_time;

// Output result
echo "Time-consuming inquiry: " . round($query_time, 5) . " Second<br>";
echo "Time to read data: " . round($read_time, 5) . " Second<br>";
?>

6. Close the database connection

After completing the query and data reading, do not forget to close the database connection.

 <?php
$conn->close();
?>

7. Complete sample code

Putting the previous steps together, here is a complete code example:

 <?php
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'testdb';

$conn = new mysqli($host, $username, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get the time when the query started
$start_time = microtime(true);

// Execute a query
$query = "SELECT * FROM users";
$result = $conn->query($query);

if (!$result) {
    die("Query failed: " . $conn->error);
}

// Get the time when the read data starts
$read_start_time = microtime(true);

// Read all results
while ($row = $result->fetch_assoc()) {
    // Here you can process each row of data
    // Example:echo $row['name'];
}

// Get the time when the read data ends
$read_end_time = microtime(true);

// Time-consuming inquiry
$query_time = $read_start_time - $start_time;

// Time to read
$read_time = $read_end_time - $read_start_time;

// Output result
echo "Time-consuming inquiry: " . round($query_time, 5) . " Second<br>";
echo "Time to read data: " . round($read_time, 5) . " Second<br>";

// Close the database connection
$conn->close();
?>

Summarize

By using the microtime() function, you can accurately measure the execution time of MySQL query and the time of data reading. This is very helpful for optimizing database queries and improving application performance. You can modify the query statement as needed, or optimize it accordingly for different database operations.