How can SQL statements be optimized to handle sorting and updating data records in PHP applications?

To optimize SQL statements for sorting and updating data records in PHP applications, you can use indexes on columns frequently used in sorting, limit the number of records being sorted, and batch updates to minimize the number of queries being executed.

// Example of optimizing SQL statements for sorting and updating data records in PHP

// Adding an index on the 'name' column for sorting
$sql = "CREATE INDEX idx_name ON table_name (name)";
$result = mysqli_query($conn, $sql);

// Limiting the number of records being sorted
$sql = "SELECT * FROM table_name ORDER BY name LIMIT 100";
$result = mysqli_query($conn, $sql);

// Batch updating data records
$sql = "UPDATE table_name SET status = 'active' WHERE id IN (1, 2, 3)";
$result = mysqli_query($conn, $sql);