SQLite Forum

bad plan for a query with an OR condition
Login
PostgreSQL 13 shows the behaviour below. Of course, the data type had to be a proper BOOLEAN, since an INT cannot be checked for truth. And I added an ORDER BY 1 to allow for easier comparison.

~~~
CREATE TABLE t(x INT,c1 boolean,c2 boolean);
INSERT INTO t VALUES
  (1,false,false),
  (2,true,false),
  (3,false,true),
  (4,true,true);

SELECT x FROM t 
WHERE c1 OR c2 
ORDER BY 1;
┌───┐
│ x │
├───┤
│ 2 │
│ 3 │
│ 4 │
└───┘


SELECT x fROM t WHERE c1
UNION ALL 
SELECT x FROM t WHERE c2
ORDER BY 1;

┌───┐
│ x │
├───┤
│ 2 │
│ 3 │
│ 4 │
│ 4 │
└───┘


SELECT x fROM t WHERE c1
UNION
SELECT x FROM t WHERE c2
ORDER BY 1;


┌───┐
│ x │
├───┤
│ 2 │
│ 3 │
│ 4 │
└───┘
~~~