Skip to content
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

Worst plan sort created to execute an indexed tables #7398

Closed
n3rv0u5 opened this issue Nov 25, 2022 · 3 comments
Closed

Worst plan sort created to execute an indexed tables #7398

n3rv0u5 opened this issue Nov 25, 2022 · 3 comments

Comments

@n3rv0u5
Copy link

n3rv0u5 commented Nov 25, 2022

In attachment I created a sample database to execute the follow query:

SELECT R.ID_OP, R.ID_REC, SUM(M.Q_MAT * CUS.CUS_MED)
FROM PCP_TIN_REC R
JOIN PCP_TIN_REC_MAT M ON (R.ID_REC = M.ID_REC)
JOIN INV_ETQ_MAT CUS ON (CUS.ID_MAT = M.ID_MAT) AND (CUS.ANOMES = R.AM_BAI)
JOIN INV_ETQ_NAT NAT ON (NAT.ID_NAT = CUS.ID_NAT)
WHERE (NAT.CML_STAT = 1) AND (R.ID_OP = 216262)
GROUP BY R.ID_OP, R.ID_REC

Executing this query without specified a plan sort, Firebird 4.0 use no indexed read.

On the other hand, if a I used a specific plan sort, like:

SELECT R.ID_OP, R.ID_REC, SUM(M.Q_MAT * CUS.CUS_MED)
FROM PCP_TIN_REC R
JOIN PCP_TIN_REC_MAT M ON (R.ID_REC = M.ID_REC)
JOIN INV_ETQ_MAT CUS ON (CUS.ID_MAT = M.ID_MAT) AND (CUS.ANOMES = R.AM_BAI)
JOIN INV_ETQ_NAT NAT ON (NAT.ID_NAT = CUS.ID_NAT)
WHERE (NAT.CML_STAT = 1) AND (R.ID_OP = 216262)
GROUP BY R.ID_OP, R.ID_REC
PLAN SORT (JOIN (R INDEX (FK_PCP_TIN_REC_OP), M INDEX (FK_PCP_TIN_REC_MAT_REC), CUS INDEX (IDX_INV_ETQ_MAT_ANOMES), NAT INDEX (PK_INV_ETQ_NAT)))
TEST.zip

Using this form, the query is very faster.

By the way, on FB 2.5 this is the plan sort generated automatically

Settings: FB 4.0 | ServerMode: SuperClassic | Windows 10 Pro 64bits

Sincerelly,
Rodrigo.

@dyemanov
Copy link
Member

dyemanov commented Nov 25, 2022

What exactly FB v4.0 version (up to build number) do you use? Because with 4.0.3.2840 I see:

PLAN SORT (JOIN (R INDEX (FK_PCP_TIN_REC_OP), M INDEX (FK_PCP_TIN_REC_MAT_REC), CUS INDEX (IDX_INV_ETQ_MAT_ANOMES), NAT INDEX (PK_INV_ETQ_NAT)))

which is exactly your "fast" option.

@n3rv0u5
Copy link
Author

n3rv0u5 commented Nov 28, 2022 via email

@dyemanov
Copy link
Member

Confirmed the worse plan for v4.0.2, it's already fixed in the upcoming v4.0.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants