How can one optimize SQL queries for better performance when using wildcard characters?

When using wildcard characters in SQL queries, such as '%', it can significantly impact performance as it can force a full table scan. To optimize these queries, it is recommended to avoid leading wildcards whenever possible and use indexes on the columns being searched. Additionally, consider using full-text search capabilities if available to improve query performance.

// Example of optimizing SQL query with wildcard characters
$searchTerm = 'example';

// Avoid leading wildcard by appending the search term with '%'
$escapedSearchTerm = '%' . $searchTerm;

// Use prepared statements to prevent SQL injection
$stmt = $pdo->prepare("SELECT * FROM table_name WHERE column_name LIKE :searchTerm");
$stmt->bindParam(':searchTerm', $escapedSearchTerm);
$stmt->execute();
$results = $stmt->fetchAll();