Are there any performance considerations to keep in mind when querying row counts in MySQL tables using PHP?

When querying row counts in MySQL tables using PHP, it is important to consider performance implications, especially for large tables. Using the COUNT() function in MySQL can be resource-intensive, especially if the table has a large number of rows. To improve performance, you can use the SQL_CALC_FOUND_ROWS option in your query to get the total row count without fetching all the data.

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Query to get row count using SQL_CALC_FOUND_ROWS
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table_name";
$result = $conn->query($sql);

// Get total row count
$count_result = $conn->query("SELECT FOUND_ROWS()");
$count = $count_result->fetch_assoc()['FOUND_ROWS()'];

echo "Total rows: " . $count;

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