-
-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different invariants optimization between views and CTEs #7388
Comments
Does the performance still differ after replacing |
No, the performance does not differ, when I replace (select max() ...) with a constant. |
So it appears that the subquery is treated as invariant (and evaluated only once) in one case and as variant in another case, thus being re-evaluated per every outer row and thus badly affecting the performance. |
If i look at it correctly we have discussed this on the support list, so i link it here for the reference only, maybe it is somehow helpful. |
Test case based on system tables only: create view v1
as select r.rdb$relation_id as id, r.rdb$relation_name as name
from rdb$relations r
inner join rdb$relation_fields rf on r.rdb$relation_name = rf.rdb$relation_name
left join rdb$security_classes sc on r.rdb$security_class = sc.rdb$security_class
;
select *
from v1
where id = (select max(id) from v1)
;
-- Fetches = 11801
with sub as (
select r.rdb$relation_id as id, r.rdb$relation_name as name
from rdb$relations r
inner join rdb$relation_fields rf on r.rdb$relation_name = rf.rdb$relation_name
left join rdb$security_classes sc on r.rdb$security_class = sc.rdb$security_class
)
select * from sub
where sub.id = (select max(id) from sub)
;
-- Fetches = 2401 |
Fix invariants optimization involving views (#7388)
Hi,
I am using Firebird Version: 4.0.2 (issue is there also with Firebird 3.0.7)
Due to performance issues I was facing with an sql query on a database, I generated a simple reproducible example, based on the "employee" sample database of firebird.
The issue in short: The query in question uses a view. When I replace the view, that is referenced in the query, with a common table expression (CTE), using the same SQL for the CTE as for the view, the query executes in fact very differently. See the comparison of the performance analysis. Interestingly enough, the execution plans of both queries are the same, though execute differently.
In the following example, Query A references the view "EMP_DEPT_TEAM". Query B uses the content of the view's query as CTE. Query A and B produce the same execution plans. I would expect query A and B performing just the same, but query A performs less good than query B.
How to reproduce (use the database "employee.fdb"):
Create a view:
Query A (using the view):
Query B (not using the view, but a CTE):
Execution plan for Query A (see also attached screenshots):
Details:
Execution plan for Query B (see also attached screenshots):
Details:
Performance Analysis:
Compare number of reads!
Query A (View):
Query B (CTE):
The text was updated successfully, but these errors were encountered: