AP 22 - Avoid UNION for multiple filter values

Severity

Warning Caution

Type

Performance

Readability

Problem

Instead of specifying an IN operator to filter on multiple values a UNION (ALL) query is created to scan the table for each filter value

SELECT *
  FROM tablex
 WHERE col1=3
UNION ALL
SELECT *
  FROM tablex
 WHERE col1=5;
SELECT *
  FROM tablex
 WHERE col1<3
UNION ALL
SELECT *
  FROM tablex
 WHERE col1>5;

Solution

Use the IN operator to filter on multiple values

SELECT *
  FROM tablex
 WHERE col1 IN(3
          ,5)
SELECT *
  FROM tablex
 WHERE col1<3
    OR col1>5;

Legitimate use of the anti pattern

None