How can PHP developers optimize the performance of regex queries in MySQL databases?

PHP developers can optimize the performance of regex queries in MySQL databases by using the REGEXP operator in their SQL queries instead of fetching all data and then filtering it in PHP. This allows the database to efficiently handle the regex matching process and return only the relevant results.

$search_term = 'example';
$query = "SELECT * FROM table_name WHERE column_name REGEXP '$search_term'";
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($result)) {
    // Process the retrieved data
}