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

opt: reduce statistics allocations for avg size #86460

Merged
merged 1 commit into from
Aug 22, 2022

Conversation

mgartner
Copy link
Collaborator

Prior to the commit, a column's average size in bytes was included in
column statistics. To fetch this average size, the coster requested an
individual column statistic each scanned column. For scans and joins
involving many columns, this caused many allocations of column
statistics and column sets.

Because we only use a column's average size when costing scans and
lookup joins, there was no need to include it in column statistics.
Average size doesn't propagate up an expression tree like other
statistics do.

This commit removes average size from column statistics and instead
builds a map in props.Statistics that maps column IDs to average size.
This significantly reduces allocations in some cases.

The only downside to this change is that we no longer set a columns
average size to zero if it has all NULL values, according to statistics.
I believe this is a pretty rare edge case that is unlikely to
significantly affect query plans, so I think the trade-off is worth it.

Fixes #80186

Release justification: This is a minor change that improves optimizer
performance.

Release note: None

@mgartner mgartner requested review from rytaft and michae2 August 19, 2022 16:19
@mgartner mgartner requested a review from a team as a code owner August 19, 2022 16:19
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner
Copy link
Collaborator Author

Every commit except for the last is from #86452. Here are some benchmarks showing the improvement for the last commit compared to the tip of #86452:

BenchmarkPhases/many-columns-and-indexes-*:

name                                                                    old time/op    new time/op    delta
Phases/many-columns-and-indexes-a/Simple/Parse-24                         13.4µs ± 0%    13.4µs ± 0%     ~     (p=0.548 n=5+5)
Phases/many-columns-and-indexes-a/Simple/OptBuildNoNorm-24                 305µs ± 0%     303µs ± 0%   -0.88%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/OptBuildNorm-24                   310µs ± 0%     308µs ± 0%   -0.82%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/Explore-24                        474µs ± 0%     456µs ± 1%   -3.69%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/ExecBuild-24                      480µs ± 0%     461µs ± 0%   -3.99%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNoNorm-24     273µs ± 0%     271µs ± 0%   -0.87%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNorm-24       273µs ± 0%     271µs ± 0%   -0.91%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/Explore-24                      431µs ± 0%     414µs ± 0%   -3.97%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/ExecBuild-24                    435µs ± 0%     418µs ± 0%   -3.80%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Parse-24                         24.8µs ± 0%    24.8µs ± 0%     ~     (p=0.508 n=5+5)
Phases/many-columns-and-indexes-b/Simple/OptBuildNoNorm-24                 332µs ± 0%     329µs ± 0%   -0.81%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/OptBuildNorm-24                   345µs ± 0%     342µs ± 1%   -0.86%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Explore-24                        515µs ± 0%     497µs ± 0%   -3.41%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/ExecBuild-24                      525µs ± 0%     507µs ± 1%   -3.37%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNoNorm-24     281µs ± 0%     278µs ± 0%   -0.87%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNorm-24       281µs ± 0%     278µs ± 0%   -0.92%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/Explore-24                      442µs ± 0%     426µs ± 0%   -3.66%  (p=0.016 n=5+4)
Phases/many-columns-and-indexes-b/Prepared/ExecBuild-24                    448µs ± 0%     432µs ± 0%   -3.57%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Parse-24                          143µs ± 0%     143µs ± 0%     ~     (p=0.548 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNoNorm-24                11.5ms ± 0%    11.5ms ± 0%   -0.18%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNorm-24                  12.9ms ± 0%    12.9ms ± 0%     ~     (p=0.095 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Explore-24                       48.8ms ± 0%    44.7ms ± 0%   -8.46%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/ExecBuild-24                     48.9ms ± 0%    45.1ms ± 0%   -7.85%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNoNorm-24    10.6ms ± 1%    10.6ms ± 0%   -0.53%  (p=0.016 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNorm-24      10.6ms ± 0%    10.6ms ± 0%   -0.48%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/Explore-24                     46.5ms ± 0%    42.4ms ± 0%   -8.75%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/ExecBuild-24                   46.6ms ± 0%    43.1ms ± 2%   -7.55%  (p=0.008 n=5+5)

name                                                                    old alloc/op   new alloc/op   delta
Phases/many-columns-and-indexes-a/Simple/Parse-24                         2.48kB ± 0%    2.48kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNoNorm-24                14.2kB ± 0%    14.2kB ± 0%   -0.11%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/OptBuildNorm-24                  16.4kB ± 0%    16.4kB ± 0%   -0.10%  (p=0.029 n=4+4)
Phases/many-columns-and-indexes-a/Simple/Explore-24                       22.9kB ± 0%    17.8kB ± 0%  -22.10%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/ExecBuild-24                     23.6kB ± 0%    18.5kB ± 0%  -21.46%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNoNorm-24    3.81kB ± 0%    3.79kB ± 0%   -0.42%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNorm-24      3.81kB ± 0%    3.79kB ± 0%   -0.42%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/Explore-24                     10.3kB ± 0%     5.3kB ± 0%  -48.96%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/ExecBuild-24                   11.0kB ± 0%     6.0kB ± 0%  -45.95%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Parse-24                         3.71kB ± 0%    3.71kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/OptBuildNoNorm-24                20.2kB ± 0%    20.1kB ± 0%   -0.40%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/OptBuildNorm-24                  23.6kB ± 0%    23.5kB ± 0%   -0.48%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Explore-24                       29.8kB ± 0%    25.0kB ± 0%  -16.07%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/ExecBuild-24                     30.8kB ± 0%    26.0kB ± 0%  -15.54%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNoNorm-24    6.05kB ± 0%    5.97kB ± 0%   -1.33%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNorm-24      6.05kB ± 0%    5.97kB ± 0%   -1.32%  (p=0.000 n=5+4)
Phases/many-columns-and-indexes-b/Prepared/Explore-24                     12.2kB ± 0%     7.5kB ± 0%  -38.93%  (p=0.016 n=5+4)
Phases/many-columns-and-indexes-b/Prepared/ExecBuild-24                   13.2kB ± 0%     8.5kB ± 0%  -35.96%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Parse-24                         20.2kB ± 0%    20.2kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Simple/OptBuildNoNorm-24                4.27MB ± 0%    4.27MB ± 0%   -0.09%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNorm-24                  4.75MB ± 0%    4.75MB ± 0%   -0.02%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Explore-24                       26.3MB ± 0%    26.2MB ± 0%   -0.38%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/ExecBuild-24                     26.4MB ± 0%    26.3MB ± 0%   -0.38%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNoNorm-24    3.65MB ± 0%    3.65MB ± 0%   -0.02%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNorm-24      3.65MB ± 0%    3.65MB ± 0%   -0.02%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/Explore-24                     25.2MB ± 0%    25.1MB ± 0%   -0.39%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/ExecBuild-24                   25.3MB ± 0%    25.2MB ± 0%   -0.39%  (p=0.008 n=5+5)

name                                                                    old allocs/op  new allocs/op  delta
Phases/many-columns-and-indexes-a/Simple/Parse-24                           21.0 ± 0%      21.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNoNorm-24                  62.0 ± 0%      62.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNorm-24                    69.0 ± 0%      69.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/Explore-24                         87.0 ± 0%      75.0 ± 0%  -13.79%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/ExecBuild-24                       96.0 ± 0%      84.0 ± 0%  -12.50%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNoNorm-24      21.0 ± 0%      21.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNorm-24        21.0 ± 0%      21.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Prepared/Explore-24                       39.0 ± 0%      27.0 ± 0%  -30.77%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/ExecBuild-24                     48.0 ± 0%      36.0 ± 0%  -25.00%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Parse-24                           30.0 ± 0%      30.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/OptBuildNoNorm-24                   109 ± 0%       109 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/OptBuildNorm-24                     128 ± 0%       128 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/Explore-24                          144 ± 0%       134 ± 0%     ~     (p=0.079 n=4+5)
Phases/many-columns-and-indexes-b/Simple/ExecBuild-24                        157 ± 0%       147 ± 0%   -6.37%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNoNorm-24      36.0 ± 0%      36.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNorm-24        36.0 ± 0%      36.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Prepared/Explore-24                       51.0 ± 0%      42.0 ± 0%  -17.65%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/ExecBuild-24                     64.0 ± 0%      55.0 ± 0%  -14.06%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Parse-24                            118 ± 0%       118 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Simple/OptBuildNoNorm-24                 56.8k ± 0%     56.8k ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Simple/OptBuildNorm-24                   61.7k ± 0%     61.7k ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Simple/Explore-24                         284k ± 0%      283k ± 0%   -0.28%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/ExecBuild-24                       284k ± 0%      284k ± 0%   -0.28%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNoNorm-24     56.4k ± 0%     56.4k ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNorm-24       56.4k ± 0%     56.4k ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Prepared/Explore-24                       279k ± 0%      278k ± 0%   -0.29%  (p=0.016 n=5+4)
Phases/many-columns-and-indexes-c/Prepared/ExecBuild-24                     279k ± 0%      278k ± 0%   -0.29%  (p=0.008 n=5+5)

BenchmarkSlowQueries:

name                         old time/op    new time/op    delta
SlowQueries/slow-query-1-24    35.3ms ± 1%    34.9ms ± 0%  -1.12%  (p=0.008 n=5+5)
SlowQueries/slow-query-2-24     544ms ± 1%     535ms ± 1%  -1.50%  (p=0.008 n=5+5)

name                         old alloc/op   new alloc/op   delta
SlowQueries/slow-query-1-24    12.3MB ± 0%    12.2MB ± 0%  -0.99%  (p=0.008 n=5+5)
SlowQueries/slow-query-2-24     181MB ± 0%     178MB ± 0%  -1.54%  (p=0.008 n=5+5)

name                         old allocs/op  new allocs/op  delta
SlowQueries/slow-query-1-24      131k ± 0%      130k ± 0%  -0.67%  (p=0.008 n=5+5)
SlowQueries/slow-query-2-24     1.65M ± 0%     1.65M ± 0%    ~     (p=0.151 n=5+5)

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Smart! This makes a lot of sense.

Reviewed 1 of 85 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner and @rytaft)


pkg/sql/opt/memo/statistics_builder.go line 646 at r4 (raw file):

			if stat.ColumnCount() == 1 && stat.AvgSize() != 0 {
				if stats.AvgColSizes == nil {
					stats.AvgColSizes = make(map[opt.ColumnID]uint64)

Is there an initial capacity that would make sense? Maybe tab.ColumnCount()?

Or instead of a map, would a slice of length tab.ColumnCount() work?


pkg/sql/opt/memo/statistics_builder.go line 648 at r4 (raw file):

					stats.AvgColSizes = make(map[opt.ColumnID]uint64)
				}
				stats.AvgColSizes[cols.SingleColumn()] = stat.AvgSize()

Could we add the all-NULL logic here?


pkg/sql/opt/props/statistics.go line 145 at r4 (raw file):

		fmt.Fprintf(&buf, ", distinct%s=%.6g", col.Cols.String(), col.DistinctCount)
		fmt.Fprintf(&buf, ", null%s=%.6g", col.Cols.String(), col.NullCount)
		fmt.Fprintf(&buf, ", avgsize%s=%.6g", col.Cols.String(), col.AvgSize)

If s.AvgColSizes != nil and there's an entry for this column, could we still print an avgsize? Would be useful to notice if are any gargantuan columns in a scan.


pkg/sql/opt/xform/coster.go line 1391 at r4 (raw file):

		}
		colSet := opt.MakeColSet(colID)
		colStat, ok := c.mem.RequestColStatTable(tabID, colSet)

Was this the only use of RequestColStatTable? Should we delete it?

Copy link
Collaborator

@michae2 michae2 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 @mgartner and @rytaft)


pkg/sql/opt/memo/statistics_builder.go line 648 at r4 (raw file):
Actually, I'm not quite sure about this:

The only downside to this change is that we no longer set a columns average size to zero if it has all NULL values, according to statistics.

If the column only has NULLs, then system.table_statistics.avgSize should be zero, as should stat.AvgSize(). So it seems like this logic is still in place. Was there another reason you wrote this?

While we're here, though, we could fix an existing issue with average size calculation. The value in system.table_statistics.avgSize and returned by stat.AvgSize() is only for non-NULL values. We should probably weight it by the number of non-NULL rows, so that it is an average over all rows, instead of only being an average over non-NULL rows. I.e. it should be something like stats.AvgColSizes[cols.SingleColumn()] = stat.AvgSize() * (stat.RowCount() - stat.NullCount()) / stat.RowCount().

Copy link
Collaborator Author

@mgartner mgartner 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 @mgartner, @michae2, and @rytaft)


pkg/sql/opt/memo/statistics_builder.go line 646 at r4 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Is there an initial capacity that would make sense? Maybe tab.ColumnCount()?

Or instead of a map, would a slice of length tab.ColumnCount() work?

Great catch! If we use column ordinals instead of column IDs we can use a slice! I'll try that out.


pkg/sql/opt/memo/statistics_builder.go line 648 at r4 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Actually, I'm not quite sure about this:

The only downside to this change is that we no longer set a columns average size to zero if it has all NULL values, according to statistics.

If the column only has NULLs, then system.table_statistics.avgSize should be zero, as should stat.AvgSize(). So it seems like this logic is still in place. Was there another reason you wrote this?

While we're here, though, we could fix an existing issue with average size calculation. The value in system.table_statistics.avgSize and returned by stat.AvgSize() is only for non-NULL values. We should probably weight it by the number of non-NULL rows, so that it is an average over all rows, instead of only being an average over non-NULL rows. I.e. it should be something like stats.AvgColSizes[cols.SingleColumn()] = stat.AvgSize() * (stat.RowCount() - stat.NullCount()) / stat.RowCount().

Heh, I misunderstood this comment. Now I see it was referring to this logic which does something different than what I thought - it sets the avg size to the default size if there are known to be some non-null values of the column.

I think I can mimic this logic based on your suggestion.

Copy link
Collaborator Author

@mgartner mgartner 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 @michae2 and @rytaft)


pkg/sql/opt/memo/statistics_builder.go line 646 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Great catch! If we use column ordinals instead of column IDs we can use a slice! I'll try that out.

Done.


pkg/sql/opt/memo/statistics_builder.go line 648 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

Heh, I misunderstood this comment. Now I see it was referring to this logic which does something different than what I thought - it sets the avg size to the default size if there are known to be some non-null values of the column.

I think I can mimic this logic based on your suggestion.

I actually don't think anything needs to change - if AvgSize is zero, it won't be added to the AvgColSizes slice (or it's entry will remain zero), and colAvgSize will return the default.

I created a separate issue to track weighting the avg size by non-null columns. #86567


pkg/sql/opt/props/statistics.go line 145 at r4 (raw file):

Previously, michae2 (Michael Erickson) wrote…

If s.AvgColSizes != nil and there's an entry for this column, could we still print an avgsize? Would be useful to notice if are any gargantuan columns in a scan.

AvgColSizes will only be set for statistics built in makeTableStatistics. These stats are never printed in optimizer plan trees - the stats of relation expressions are printed.


pkg/sql/opt/xform/coster.go line 1391 at r4 (raw file):

Previously, michae2 (Michael Erickson) wrote…

Was this the only use of RequestColStatTable? Should we delete it?

Good catch. Done.

Prior to the commit, a column's average size in bytes was included in
column statistics. To fetch this average size, the coster requested an
individual column statistic each scanned column. For scans and joins
involving many columns, this caused many allocations of column
statistics and column sets.

Because we only use a column's average size when costing scans and
lookup joins, there was no need to include it in column statistics.
Average size doesn't propagate up an expression tree like other
statistics do.

This commit removes average size from column statistics and instead
builds a map in `props.Statistics` that maps column IDs to average size.
This significantly reduces allocations in some cases.

The only downside to this change is that we no longer set a columns
average size to zero if it has all NULL values, according to statistics.
I believe this is a pretty rare edge case that is unlikely to
significantly affect query plans, so I think the trade-off is worth it.

Fixes cockroachdb#80186

Release justification: This is a minor change that improves optimizer
performance.

Release note: None
@mgartner mgartner force-pushed the 80186-avg-size-allocs branch from 5e54d75 to 38cc71e Compare August 22, 2022 15:46
@mgartner
Copy link
Collaborator Author

Looks like we're getting similar improvements with the slice instead of the map 👍 👍

name                                                                    old time/op    new time/op    delta
Phases/many-columns-and-indexes-a/Simple/Parse-24                         12.6µs ± 0%    12.6µs ± 0%     ~     (p=0.095 n=5+5)
Phases/many-columns-and-indexes-a/Simple/OptBuildNoNorm-24                 307µs ± 0%     307µs ± 0%     ~     (p=1.000 n=5+5)
Phases/many-columns-and-indexes-a/Simple/OptBuildNorm-24                   312µs ± 0%     312µs ± 0%     ~     (p=0.690 n=5+5)
Phases/many-columns-and-indexes-a/Simple/Explore-24                        475µs ± 0%     464µs ± 0%   -2.47%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/ExecBuild-24                      482µs ± 0%     469µs ± 0%   -2.71%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNoNorm-24     275µs ± 0%     275µs ± 0%     ~     (p=1.000 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNorm-24       275µs ± 0%     275µs ± 0%     ~     (p=1.000 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/Explore-24                      432µs ± 0%     421µs ± 0%   -2.54%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/ExecBuild-24                    437µs ± 0%     425µs ± 0%   -2.62%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Parse-24                         23.3µs ± 0%    23.2µs ± 0%   -0.31%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/OptBuildNoNorm-24                 334µs ± 1%     334µs ± 0%     ~     (p=0.690 n=5+5)
Phases/many-columns-and-indexes-b/Simple/OptBuildNorm-24                   347µs ± 0%     347µs ± 1%     ~     (p=0.222 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Explore-24                        517µs ± 0%     504µs ± 0%   -2.54%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/ExecBuild-24                      527µs ± 0%     512µs ± 0%   -2.87%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNoNorm-24     282µs ± 0%     282µs ± 0%     ~     (p=1.000 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNorm-24       283µs ± 0%     283µs ± 0%     ~     (p=0.548 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/Explore-24                      444µs ± 0%     434µs ± 0%   -2.37%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/ExecBuild-24                    451µs ± 0%     439µs ± 0%   -2.73%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Parse-24                          133µs ± 0%     133µs ± 0%     ~     (p=0.222 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNoNorm-24                11.7ms ± 0%    11.6ms ± 0%   -1.36%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNorm-24                  13.1ms ± 1%    12.9ms ± 0%   -1.08%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Explore-24                       50.3ms ± 0%    45.7ms ± 0%   -9.16%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/ExecBuild-24                     50.4ms ± 0%    46.0ms ± 0%   -8.83%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNoNorm-24    10.7ms ± 0%    10.6ms ± 0%   -0.75%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNorm-24      10.7ms ± 0%    10.6ms ± 0%   -0.64%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/Explore-24                     47.8ms ± 0%    43.4ms ± 0%   -9.29%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/ExecBuild-24                   48.1ms ± 0%    43.6ms ± 0%   -9.49%  (p=0.008 n=5+5)

name                                                                    old alloc/op   new alloc/op   delta
Phases/many-columns-and-indexes-a/Simple/Parse-24                         2.48kB ± 0%    2.48kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNoNorm-24                14.2kB ± 0%    14.2kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNorm-24                  16.4kB ± 0%    16.4kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/Explore-24                       22.9kB ± 0%    17.8kB ± 0%  -22.03%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/ExecBuild-24                     23.6kB ± 0%    18.5kB ± 0%  -21.40%  (p=0.016 n=5+4)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNoNorm-24    3.81kB ± 0%    3.81kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNorm-24      3.81kB ± 0%    3.81kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Prepared/Explore-24                     10.3kB ± 0%     5.3kB ± 0%  -48.82%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/ExecBuild-24                   11.0kB ± 0%     6.0kB ± 0%  -45.80%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Parse-24                         3.71kB ± 0%    3.71kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/OptBuildNoNorm-24                20.2kB ± 0%    20.1kB ± 0%   -0.32%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/OptBuildNorm-24                  23.6kB ± 0%    23.5kB ± 0%   -0.41%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Explore-24                       29.8kB ± 0%    25.0kB ± 0%  -16.02%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/ExecBuild-24                     30.8kB ± 0%    26.0kB ± 0%  -15.49%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNoNorm-24    6.05kB ± 0%    5.99kB ± 0%   -1.06%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNorm-24      6.05kB ± 0%    5.99kB ± 0%   -1.06%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/Explore-24                     12.2kB ± 0%     7.5kB ± 0%  -38.81%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/ExecBuild-24                   13.2kB ± 0%     8.5kB ± 0%  -35.83%  (p=0.016 n=5+4)
Phases/many-columns-and-indexes-c/Simple/Parse-24                         20.2kB ± 0%    20.2kB ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Simple/OptBuildNoNorm-24                4.27MB ± 0%    4.27MB ± 0%   -0.08%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNorm-24                  4.75MB ± 0%    4.75MB ± 0%   -0.02%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Explore-24                       26.3MB ± 0%    26.2MB ± 0%   -0.38%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/ExecBuild-24                     26.4MB ± 0%    26.3MB ± 0%   -0.38%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNoNorm-24    3.65MB ± 0%    3.65MB ± 0%   -0.01%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNorm-24      3.65MB ± 0%    3.65MB ± 0%   -0.01%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/Explore-24                     25.2MB ± 0%    25.1MB ± 0%   -0.39%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/ExecBuild-24                   25.3MB ± 0%    25.2MB ± 0%   -0.39%  (p=0.008 n=5+5)

name                                                                    old allocs/op  new allocs/op  delta
Phases/many-columns-and-indexes-a/Simple/Parse-24                           21.0 ± 0%      21.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNoNorm-24                  62.0 ± 0%      62.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/OptBuildNorm-24                    69.0 ± 0%      69.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Simple/Explore-24                         87.0 ± 0%      75.0 ± 0%  -13.79%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Simple/ExecBuild-24                       96.0 ± 0%      84.0 ± 0%  -12.50%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNoNorm-24      21.0 ± 0%      21.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Prepared/AssignPlaceholdersNorm-24        21.0 ± 0%      21.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-a/Prepared/Explore-24                       39.0 ± 0%      27.0 ± 0%  -30.77%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-a/Prepared/ExecBuild-24                     48.0 ± 0%      36.0 ± 0%  -25.00%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/Parse-24                           30.0 ± 0%      30.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/OptBuildNoNorm-24                   109 ± 0%       109 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/OptBuildNorm-24                     128 ± 0%       128 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Simple/Explore-24                          144 ± 0%       134 ± 0%   -6.94%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Simple/ExecBuild-24                        157 ± 0%       147 ± 0%   -6.13%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNoNorm-24      36.0 ± 0%      36.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Prepared/AssignPlaceholdersNorm-24        36.0 ± 0%      36.0 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-b/Prepared/Explore-24                       51.0 ± 0%      42.0 ± 0%  -17.65%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-b/Prepared/ExecBuild-24                     64.0 ± 0%      55.0 ± 0%  -14.06%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/Parse-24                            118 ± 0%       118 ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Simple/OptBuildNoNorm-24                 56.8k ± 0%     56.8k ± 0%     ~     (p=0.921 n=5+5)
Phases/many-columns-and-indexes-c/Simple/OptBuildNorm-24                   61.7k ± 0%     61.7k ± 0%     ~     (p=0.556 n=4+5)
Phases/many-columns-and-indexes-c/Simple/Explore-24                         284k ± 0%      283k ± 0%   -0.28%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Simple/ExecBuild-24                       284k ± 0%      284k ± 0%   -0.28%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNoNorm-24     56.4k ± 0%     56.4k ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Prepared/AssignPlaceholdersNorm-24       56.4k ± 0%     56.4k ± 0%     ~     (all equal)
Phases/many-columns-and-indexes-c/Prepared/Explore-24                       279k ± 0%      278k ± 0%   -0.29%  (p=0.008 n=5+5)
Phases/many-columns-and-indexes-c/Prepared/ExecBuild-24                     279k ± 0%      278k ± 0%   -0.29%  (p=0.008 n=5+5)

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

Excellent! :lgtm:

Reviewed 73 of 85 files at r4, 20 of 45 files at r5, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rytaft)


pkg/sql/opt/memo/statistics_builder.go line 648 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

I actually don't think anything needs to change - if AvgSize is zero, it won't be added to the AvgColSizes slice (or it's entry will remain zero), and colAvgSize will return the default.

I created a separate issue to track weighting the avg size by non-null columns. #86567

Cool!


pkg/sql/opt/props/statistics.go line 145 at r4 (raw file):

Previously, mgartner (Marcus Gartner) wrote…

AvgColSizes will only be set for statistics built in makeTableStatistics. These stats are never printed in optimizer plan trees - the stats of relation expressions are printed.

Ah, ok.

@mgartner
Copy link
Collaborator Author

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Aug 22, 2022

Build succeeded:

@craig craig bot merged commit 27764b6 into cockroachdb:master Aug 22, 2022
@mgartner mgartner deleted the 80186-avg-size-allocs branch August 23, 2022 00:02
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.

Reduce stats allocations in optimizer
3 participants