Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
df6166f4
Commit
df6166f4
authored
Nov 23, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor estimate query
parent
332fe82e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
19 deletions
+18
-19
lib/gitlab/database/count.rb
lib/gitlab/database/count.rb
+18
-19
No files found.
lib/gitlab/database/count.rb
View file @
df6166f4
...
...
@@ -75,6 +75,10 @@ module Gitlab
end
end
class
PgClass
<
ActiveRecord
::
Base
self
.
table_name
=
'pg_class'
end
# This strategy counts based on PostgreSQL's statistics in pg_stat_user_tables.
#
# Specifically, it relies on the column reltuples in said table. An additional
...
...
@@ -110,20 +114,15 @@ module Gitlab
end
def
size_estimates
(
check_statistics:
true
)
query
=
postgresql_estimate_query
(
table_names
,
check_statistics:
check_statistics
)
rows
=
[]
table_to_model
=
models
.
each_with_object
({})
{
|
model
,
h
|
h
[
model
.
table_name
]
=
model
}
# Querying tuple stats only works on the primary. Due to load
# easiest way to do this is to start a transaction.
ActiveRecord
::
Base
.
transaction
do
rows
=
ActiveRecord
::
Base
.
connection
.
select_all
(
query
)
end
table_to_model
=
models
.
each_with_object
({})
{
|
model
,
h
|
h
[
model
.
table_name
]
=
model
}
rows
.
each_with_object
({})
do
|
row
,
data
|
model
=
table_to_model
[
row
[
'table_name'
]]
data
[
model
]
=
row
[
'estimate'
].
to_i
get_statistics
(
table_names
,
check_statistics:
check_statistics
).
each_with_object
({})
do
|
row
,
data
|
model
=
table_to_model
[
row
.
table_name
]
data
[
model
]
=
row
.
estimate
end
end
end
...
...
@@ -132,19 +131,19 @@ module Gitlab
#
# @param [Array] table names
# @returns [Hash] Table name to count mapping (e.g. { 'projects' => 5, 'users' => 100 })
def
postgresql_estimate_query
(
table_names
,
check_statistics:
true
)
def
get_statistics
(
table_names
,
check_statistics:
true
)
time
=
"to_timestamp(
#{
1
.
hour
.
ago
.
to_i
}
)"
base_query
=
<<~
SQL
SELECT pg_class.relname AS table_name, reltuples::bigint AS estimate FROM pg_class
LEFT JOIN pg_stat_user_tables ON pg_class.relname = pg_stat_user_tables.relname
WHERE pg_class.relname IN (
#{
table_names
.
map
{
|
table
|
"'
#{
table
}
'"
}
.join(',')})
SQL
query
=
PgClass
.
joins
(
"LEFT JOIN pg_stat_user_tables USING (relname)"
)
.
where
(
relname:
table_names
)
.
select
(
'pg_class.relname AS table_name, reltuples::bigint AS estimate'
)
if
check_statistics
base_query
+
"AND (last_vacuum >
#{
time
}
OR last_autovacuum >
#{
time
}
OR last_analyze >
#{
time
}
OR last_autoanalyze >
#{
time
}
)"
else
base_query
query
=
query
.
where
(
'last_vacuum > ? OR last_autovacuum > ? OR last_analyze > ? OR last_autoanalyze > ?'
,
time
,
time
,
time
,
time
)
end
query
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment