SQLite

View Ticket
Login
2023-02-22
21:11 Fixed ticket [baa5bb76]: UNION ALL view does not use a covering index plus 3 other changes (artifact: 7377070f user: drh)
21:11
Avoid computing the values for unused result-set columns in subqueries. Performance optimization request [baa5bb76c35a124c]. (check-in: 7c2d3406 user: drh tags: trunk)
2023-02-16
17:15 Ticket [baa5bb76] UNION ALL view does not use a covering index status still Open with 6 other changes (artifact: 3bdd160b user: drh)
14:29
Do not compute result columns of subqueries that are never used. Make those columns NULL instead. This optimization potentially resolves the enhancement request described by [ticket baa5bb76c35a124c]. (check-in: 0163b697 user: drh tags: branch-3.28)
01:29
Do not compute unused result columns of subqueries. This optimization will potentially resolve the performance optimization request of [ticket baa5bb76c35a124c]. (Closed-Leaf check-in: 0c21b6a5 user: drh tags: branch-3.26)
2023-02-15
17:53
Do not compute result columns of subqueries that are never used. Make those columns NULL instead. This optimization potentially resolves the enhancement request described by [ticket baa5bb76c35a124c]. (check-in: 5dec3cc0 user: drh tags: omit-unused-subquery-columns)
2023-02-14
20:12 New ticket [baa5bb76] UNION ALL view does not use a covering index. (artifact: 8e353e53 user: drh)

Ticket Hash: baa5bb76c35a124cb9dab0853be05fc0ee6b18e8
Title: UNION ALL view does not use a covering index
Status: Fixed Type: Optimization
Severity: Important Priority: Low
Subsystem: Unknown Resolution: Fixed
Last Modified: 2023-02-22 21:11:47
Version Found In: 3.40.1
User Comments:
drh added on 2023-02-14 20:12:50:

In the SQL code below, the first query against an ordinary table runs against an index and is thus fast. (In real life, the tables are much wider and so running against the index has a big performance advantage - kind of like using a column store.) However, the query that is run against the UNION ALL view does not use the covering index. It runs much slower.

Desired result: The query planner should recognize that the t1.b and t2.c columns are never used and hence never load them, thus allowing the indexes on t1.a and t2.a to be covering indexes.

CREATE TABLE t1(a INT, b INT);
CREATE INDEX t1a ON t1(a);
CREATE TABLE t2(a INT, c INT);
CREATE INDEX t2ac ON t2(a);
CREATE VIEW tv AS SELECT * FROM t1 UNION ALL SELECT * FROM t2;
.eqp on
SELECT count(*) FROM t1 WHERE a=0;
SELECT count(*) FROM tv WHERE a=0;