Class: Gitlab::Database::Count::TablesampleCountStrategy

Inherits:
ReltuplesCountStrategy show all
Defined in:
lib/gitlab/database/count/tablesample_count_strategy.rb

Overview

A tablesample count executes in two phases:

  • Estimate table sizes based on reltuples.

  • Based on the estimate:

    • If the table is considered 'small', execute an exact relation count.

    • Otherwise, count on a sample of the table using TABLESAMPLE.

The size of the sample is chosen in a way that we always roughly scan the same amount of rows (see TABLESAMPLE_ROW_TARGET).

There are no guarantees with respect to the accuracy of the result or runtime.

Constant Summary collapse

EXACT_COUNT_THRESHOLD =
10_000
TABLESAMPLE_ROW_TARGET =
10_000

Instance Attribute Summary

Attributes inherited from ReltuplesCountStrategy

#models

Instance Method Summary collapse

Methods inherited from ReltuplesCountStrategy

#initialize

Constructor Details

This class inherits a constructor from Gitlab::Database::Count::ReltuplesCountStrategy

Instance Method Details

#countObject


20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/database/count/tablesample_count_strategy.rb', line 20

def count
  estimates = size_estimates(check_statistics: false)

  models.each_with_object({}) do |model, count_by_model|
    count = perform_count(model, estimates[model])
    count_by_model[model] = count if count
  end
rescue *CONNECTION_ERRORS
  {}
end