In PHP, what are the implications of using UNION in SELECT queries and how can developers optimize their queries to avoid performance issues?

Using UNION in SELECT queries can lead to performance issues as it combines the result sets of multiple SELECT statements. To optimize queries and avoid these issues, developers should consider using UNION ALL instead of UNION, as it does not remove duplicate rows. Additionally, developers should ensure that the columns selected in each query within the UNION have the same data types to prevent unnecessary type conversions.

$query = "SELECT column1, column2 FROM table1
          UNION ALL
          SELECT column1, column2 FROM table2";