Skip to content

sql, ui: add index used by statement#92351

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
maryliag:idx-usage
Nov 24, 2022
Merged

sql, ui: add index used by statement#92351
craig[bot] merged 1 commit intocockroachdb:masterfrom
maryliag:idx-usage

Conversation

@maryliag
Copy link
Copy Markdown
Contributor

Part Of #82615

This commits collects the list of indexes used by the statement execution and and saves on system.statement_statistics and crdb_internal.statement_statistics under the statistics columns, inside the statistics key.
To get the information

SELECT statistics -> 'statistics' ->> 'indexes' from
crdb_internal.statement_statistics

The value is saved as an array of string with format tableId@indexId.

Release note (sql change): Add list of indexes used by the query on the statistics column on system.statement_statistics and crdb_internal.statement_statistics tables. The format is tableID@indexID.

@maryliag maryliag requested a review from a team November 22, 2022 21:35
@maryliag maryliag requested a review from a team as a code owner November 22, 2022 21:35
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Reviewed 15 of 17 files at r1, 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @maryliag)


pkg/roachpb/app_stats.proto line 121 at r2 (raw file):

  // indexes is the list of indexes used by the particular plan when executing the statement.
  repeated string indexes = 30;

nit: isn't 29 next?


pkg/sql/opt/exec/execbuilder/relational.go line 733 at r2 (raw file):

			errors.AssertionFailedf("expected inverted index scan to have an inverted constraint")
	}
	b.IndexesUsed = util.CombineUniqueString(b.IndexesUsed, []string{fmt.Sprintf("%d@%d", idx.Table().ID(), idx.ID())})

It's unfortunate that CombineUniqueString reallocates a new slice each time it is called. I think it would be more efficient to store the IndexesUsed in a map[string]struct{}or a map[int]util.FastIntSet while in the execbuilder. Then you can convert to a []string for the instrumentationHelper.

also a nit: idx.Table() -> tab


pkg/sql/opt/exec/execbuilder/relational.go line 1991 at r2 (raw file):

	// starts with the PK columns in order (#40749).
	pri := tab.Index(cat.PrimaryIndex)
	b.IndexesUsed = util.CombineUniqueString(b.IndexesUsed, []string{fmt.Sprintf("%d@%d", pri.Table().ID(), pri.ID())})

nit: you already have pri.Table() with tab


pkg/sql/opt/exec/execbuilder/relational.go line 2286 at r2 (raw file):

	tab := md.Table(join.Table)
	idx := tab.Index(join.Index)
	b.IndexesUsed = util.CombineUniqueString(b.IndexesUsed, []string{fmt.Sprintf("%d@%d", idx.Table().ID(), idx.ID())})

nit: idx.Table() -> tab


pkg/sql/opt/exec/execbuilder/relational.go line 2448 at r2 (raw file):

	tab := md.Table(join.Table)
	idx := tab.Index(join.Index)
	b.IndexesUsed = util.CombineUniqueString(b.IndexesUsed, []string{fmt.Sprintf("%d@%d", idx.Table().ID(), idx.ID())})

nit: idx.Table() -> tab


pkg/sql/opt/exec/execbuilder/relational.go line 2553 at r2 (raw file):

		[]string{fmt.Sprintf("%d@%d", leftIndex.Table().ID(), leftIndex.ID())})
	b.IndexesUsed = util.CombineUniqueString(b.IndexesUsed,
		[]string{fmt.Sprintf("%d@%d", rightIndex.Table().ID(), rightIndex.ID())})

nit: leftIndex.Table() -> leftTable, rightIndex.Table() -> rightTable


pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil/json_encoding.go line 131 at r2 (raw file):

//		        "rowsRead",
//		        "nodes",
//	         "indexes,

nit: formatting is a bit messed up

Part Of cockroachdb#82615

This commits collects the list of indexes used by the
statement execution and and saves on `system.statement_statistics`
and `crdb_internal.statement_statistics` under the statistics columns,
inside the statistics key.
To get the information
```
SELECT statistics -> 'statistics' ->> 'indexes' from
crdb_internal.statement_statistics
```

The value is saved as an array of string with format `tableId@indexId`.

Release note (sql change): Add list of indexes used by
the query on the statistics column on system.statement_statistics
and crdb_internal.statement_statistics tables. The format is
`tableID@indexID`.
Copy link
Copy Markdown
Contributor Author

@maryliag maryliag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rytaft)


pkg/roachpb/app_stats.proto line 121 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: isn't 29 next?

There is a PR introducing the 29 here, so I want to avoid the conflict and already jump to 30


pkg/sql/opt/exec/execbuilder/relational.go line 733 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

It's unfortunate that CombineUniqueString reallocates a new slice each time it is called. I think it would be more efficient to store the IndexesUsed in a map[string]struct{}or a map[int]util.FastIntSet while in the execbuilder. Then you can convert to a []string for the instrumentationHelper.

also a nit: idx.Table() -> tab

Done for the nit

For the combine unique, I decided to improve the function instead: #92396
Let me know what you think


pkg/sql/opt/exec/execbuilder/relational.go line 1991 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: you already have pri.Table() with tab

Done


pkg/sql/opt/exec/execbuilder/relational.go line 2286 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: idx.Table() -> tab

Done


pkg/sql/opt/exec/execbuilder/relational.go line 2448 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: idx.Table() -> tab

Done


pkg/sql/opt/exec/execbuilder/relational.go line 2553 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: leftIndex.Table() -> leftTable, rightIndex.Table() -> rightTable

Done


pkg/sql/sqlstats/persistedsqlstats/sqlstatsutil/json_encoding.go line 131 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

nit: formatting is a bit messed up

Done

Copy link
Copy Markdown
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @maryliag)


pkg/sql/opt/exec/execbuilder/relational.go line 733 at r2 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

Done for the nit

For the combine unique, I decided to improve the function instead: #92396
Let me know what you think

Sounds good!

@maryliag
Copy link
Copy Markdown
Contributor Author

TFTR!
bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Nov 24, 2022

Build succeeded:

@craig craig bot merged commit 28aed6e into cockroachdb:master Nov 24, 2022
@maryliag maryliag deleted the idx-usage branch November 24, 2022 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants