Domanda

I have view like:

SELECT F1, F2
FROM T1
LEFT JOIN T2 ON T2.ID=T1.ID
LEFT JOIN T3 ON T3.ID=T1.ID

When I do a count query like:

SELECT COUNT(*) FROM MYVIEW

If T1 has 1000 records, Firebird generates 3000 accesses to records: 1000 for T1, 1000 for T2, 1000 for T3.

Is there a way to optimize it? I could use the directly the table: SELECT COUNT(*) FROM T1

but I need to use the views, I cannot use tables. Well, the problem is more complicated, but I have used this simple example to illustrate the basis problem. Firebird does not seem to optimize some queries and my question is if I can optimize the query changing something in the DDL of the view.

È stato utile?

Soluzione

A JOIN from T1 to T2 (and T3) can lead to more records than just those in T1. Say that for each record in T1 there are two records in T2, then for 1000 records in T1 the result of the count is 2000.

In theory the optimizer could know that T1.ID and T2.ID are both primary keys (or unique) and therefor only scan T1, but the optimizer in Firebird is not very advanced, so maybe it simply can't do that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top