-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The database schema was lacking many integrity checks and indexes. Correct it by first applying a migration that removes all old/stale data, then creating those indexes. The driving reason for this is the very slow performance of processing (specially aggregation) on the new mezuro.org servers. It will hopefully remove (or at least heavily improve) the superlinear slowdown when the number of metrics rises, as observed in #207. Additionally, remove the timestamp columns from kalibro_modules, module_results and metric_results: they are not used in any way, and there are millions of rows containing them. It's possible and probably desirable to just look at the timestamps in the processing.
- Loading branch information
Showing
12 changed files
with
152 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class CleanInconsistencies < ActiveRecord::Migration | ||
def self.up | ||
# Unset project reference for repositories with non-existing projects | ||
execute <<-SQL | ||
UPDATE repositories AS r | ||
SET project_id = NULL | ||
WHERE project_id = 0 OR NOT EXISTS ( | ||
SELECT 1 FROM projects AS p WHERE p.id = r.project_id | ||
) | ||
SQL | ||
|
||
# Delete processings with non-existing repositories | ||
execute <<-SQL | ||
DELETE FROM processings AS p | ||
WHERE NOT EXISTS( | ||
SELECT 1 FROM repositories AS r WHERE r.id = p.repository_id | ||
) | ||
SQL | ||
|
||
# Delete process times with non-existing processings | ||
execute <<-SQL | ||
DELETE FROM process_times AS t | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM processings AS p WHERE p.id = t.processing_id | ||
) | ||
SQL | ||
|
||
# Delete module results with non-existing processings | ||
execute <<-SQL | ||
DELETE FROM module_results AS m | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM processings AS p WHERE p.id = m.processing_id | ||
) | ||
SQL | ||
|
||
# Delete kalibro modules with non-existing module results | ||
execute <<-SQL | ||
DELETE FROM kalibro_modules AS k | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM module_results AS m WHERE m.id = k.module_result_id | ||
) | ||
SQL | ||
|
||
# Delete metric results with non-existing module results | ||
execute <<-SQL | ||
DELETE FROM metric_results AS met | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM module_results AS mod WHERE mod.id = met.module_result_id | ||
) | ||
SQL | ||
|
||
# Delete duplicate metric_results. Group them by (module_result, metric_configuration), | ||
# then delete all but the one with the highest ID | ||
# The double wrapping on the inner query is necessary because window functions | ||
# cannot be used in WHERE in PostgreSQL. | ||
execute <<-SQL | ||
DELETE FROM metric_results | ||
WHERE id IN ( | ||
SELECT t.id FROM ( | ||
SELECT id, ROW_NUMBER() OVER (PARTITION BY module_result_id, metric_configuration_id ORDER BY id DESC) AS rnum | ||
FROM metric_results | ||
) AS t | ||
WHERE t.rnum > 1 | ||
) | ||
SQL | ||
end | ||
|
||
def self.down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIndexesToKalibroModules < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :kalibro_modules, :module_results, on_delete: :cascade | ||
add_index :kalibro_modules, [:long_name, :granularity] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIndexesToModuleResults < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :module_results, :module_results, column: 'parent_id' | ||
add_foreign_key :module_results, :processings, on_delete: :cascade | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddIndexesToMetricResults < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :metric_results, :module_results, on_delete: :cascade | ||
add_index :metric_results, :type | ||
add_index :metric_results, [:module_result_id, :metric_configuration_id], | ||
unique: true, name: 'metric_results_module_res_metric_cfg_uniq_idx' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIndexesToProcessings < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :processings, :repositories | ||
add_foreign_key :processings, :module_results, column: 'root_module_result_id' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddIndexesToProcessTimes < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :process_times, :processings, on_delete: :cascade | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddIndexesToRepositories < ActiveRecord::Migration | ||
def change | ||
add_foreign_key :repositories, :projects | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20160716000521_remove_timestamps_from_module_results.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RemoveTimestampsFromModuleResults < ActiveRecord::Migration | ||
def change | ||
remove_column :module_results, :created_at, :string | ||
remove_column :module_results, :updated_at, :string | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20160716000528_remove_timestamps_from_metric_results.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RemoveTimestampsFromMetricResults < ActiveRecord::Migration | ||
def change | ||
remove_column :metric_results, :created_at, :string | ||
remove_column :metric_results, :updated_at, :string | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20160716000530_remove_timestamps_from_kalibro_modules.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RemoveTimestampsFromKalibroModules < ActiveRecord::Migration | ||
def change | ||
remove_column :kalibro_modules, :created_at, :string | ||
remove_column :kalibro_modules, :updated_at, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters