Skip to content

Feature #7675 - EXPLAIN statement and RDB$SQL package.#7697

Merged
asfernandes merged 9 commits into
masterfrom
work/gh-7675-explain-plan
Sep 19, 2023
Merged

Feature #7675 - EXPLAIN statement and RDB$SQL package.#7697
asfernandes merged 9 commits into
masterfrom
work/gh-7675-explain-plan

Conversation

@asfernandes
Copy link
Copy Markdown
Member

No description provided.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 4, 2023

Plan output in explain format is broken. Although RDB$SQL.EXPLAIN itself determines the level correctly, the indentation in the explain format is not correct.

Firebird 4.0

SQL> set planonly;
SQL> select count(*)
CON> from horse h
CON> join color on color.code_color = h.code_color
CON> where color.NAME_DE = '';

Select Expression
    -> Aggregate
        -> Nested Loop Join (inner)
            -> Filter
                -> Table "COLOR" Full Scan
            -> Filter
                -> Table "HORSE" as "H" Access By ID
                    -> Bitmap
                        -> Index "FK_HORSE_COLOR" Range Scan (full match)

Firebird 5.0 with explain feature

SQL> explain select count(*)
CON> from horse h
CON> join color on color.code_color = h.code_color
CON> where color.NAME_DE = '';

Select Expression
    -> Aggregate
        -> Nested Loop Join (inner)
            -> Filter
                -> Table "COLOR" Full Scan
            -> Filter
                -> Table "HORSE" as "H" Access By ID
    -> Bitmap
        -> Index "FK_HORSE_COLOR" Range Scan (full match)

The likely reason for this is the contents of the ACCESS_PATH column

-> Table "HORSE" as "H" Access By ID
     -> Bitmap
         -> Index "FK_HORSE_COLOR" Range Scan (full match)

The indentation for the first line is correct, but the indentation for subsequent lines is not added to the indentation.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 4, 2023

In addition, it would be nice to reserve columns for the future
CARDINALITY [BIG]INT,
COST DOUBLE PRECISION

@dyemanov
Copy link
Copy Markdown
Member

dyemanov commented Aug 4, 2023

Cardinality may already be returned.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 4, 2023

Here is another question, why?
-> Bitmap
And
-> Index "FK_HORSE_COLOR" Range Scan (full match)
are not separate entries?

I understand that here the plan is parsed in terms of sources of records. However, when parsing the plan itself, it would be convenient to see the indexes used as entity names, rather than parsing inside the ACCESS_PATH.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 4, 2023

It would also be nice to add an OBJECT_TYPE column: (table, procedure ...)

@asfernandes
Copy link
Copy Markdown
Member Author

Here is another question, why? -> Bitmap And -> Index "FK_HORSE_COLOR" Range Scan (full match) are not separate entries?

I understand that here the plan is parsed in terms of sources of records. However, when parsing the plan itself, it would be convenient to see the indexes used as entity names, rather than parsing inside the ACCESS_PATH.

Every record returned matches a RecordSource, that is the bases also of the profiler. If we broke this rule, it will not be good to have different representations in these two places.

See topic "Detailed plan" that I started in fb-devel about the indentations without levels.

@asfernandes
Copy link
Copy Markdown
Member Author

Should we also add a columns for the internal RecordSource class name?

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 4, 2023

The line indentation for BitmapTableScan is fairly easy to fix. It is enough to add the current indent not to the entire ACCESS_PATH line, but to the line itself and after each line break.
Here is another problem. The use of some index is important information, in the current form it is necessary to parse ACCESS_PATH to get it.

@asfernandes asfernandes linked an issue Aug 5, 2023 that may be closed by this pull request
@asfernandes
Copy link
Copy Markdown
Member Author

It would also be nice to add an OBJECT_TYPE column: (table, procedure ...)

I added it, using same code as MON$COMPILED_STATEMENT.MON$OBJECT_TYPE.

I do not like it too much (in most cases a string string is better for such things IMO), but did to be consistent.

Profiler uses a string for STATEMENT_TYPE, but there it could also be BLOCK.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 15, 2023

In some cases, the output parameter ACCESS_PATH VARCHAR(255) may not be long enough. This will happen when bitmasks are combined. Example:

SELECT *
FROM RDB$SQL.EXPLAIN(Q'{
SELECT *
FROM RDB$RELATIONS
WHERE RDB$RELATIONS.RDB$RELATION_NAME IN ('RDB$PAGES', 'RDB$DATABASE', 'RDB$FIELDS')
}');
Arithmetic overflow or division by zero has occurred.
arithmetic exception, numeric overflow, or string truncation.
string right truncation.
expected length 255, actual 342.
-------------------------------------------------------------
SQLCODE: -802
SQLSTATE: 22001
GDSCODE: 335544321

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Aug 15, 2023

In addition, the padding for Bitmap And is displayed incorrectly and the Legacy plan breaks (extra comma).

select *
from RDB$INDICES
where RDB$FOREIGN_KEY = 'RDB$RELATION_ID'
  and RDB$RELATION_NAME = 'RDB$RELATIONS'

Legacy plan:

PLAN (RDB$INDICES INDEX (, RDB$INDEX_31, RDB$INDEX_41))

Explain plan:

Select Expression
    -> Filter
        -> Table "RDB$INDICES" Access By ID
            -> Bitmap And
                -> Bitmap
                    -> Index "RDB$INDEX_31" Range Scan (full match)
                        -> Bitmap
                            -> Index "RDB$INDEX_41" Range Scan (full match)

@asfernandes
Copy link
Copy Markdown
Member Author

In some cases, the output parameter ACCESS_PATH VARCHAR(255) may not be long enough. This will happen when bitmasks are combined.

Any opinions if we enlarge it or just use text blob?

@asfernandes
Copy link
Copy Markdown
Member Author

In addition, the padding for Bitmap And is displayed incorrectly and the Legacy plan breaks (extra comma).

Let's wait merge of #7709 to fix, it will create conflicts with this.

@AlexPeshkoff
Copy link
Copy Markdown
Member

AlexPeshkoff commented Aug 15, 2023 via email

@asfernandes asfernandes force-pushed the work/gh-7675-explain-plan branch from 6004982 to a2d8e64 Compare September 19, 2023 00:40
@asfernandes asfernandes merged commit 39b0195 into master Sep 19, 2023
@asfernandes asfernandes deleted the work/gh-7675-explain-plan branch September 19, 2023 00:52
@mrotteveel mrotteveel added the rlsnotes60: yes Already added to the Firebird 6.0 release notes. (Do not add this to signal it should be added.) label Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix-version: 6.0 Alpha 1 rlsnotes60: yes Already added to the Firebird 6.0 release notes. (Do not add this to signal it should be added.) type: new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EXPLAIN statement and RDB$SQL package

5 participants