`Project#create_labels` runs `Label.templates` which is very slow
This code does the following:
def create_labels
Label.templates.each do |label|
params = label.attributes.except('id', 'template', 'created_at', 'updated_at')
Labels::FindOrCreateService.new(nil, self, params).execute(skip_authorization: true)
end
end
There are a few problems here:
-
labels.templatedefaults tofalse, but allows NULL values (this should not be allowed) -
labels.templateis never set totruefor GitLab.com, wasting time querying this data -
labels.templateshould have a partial index with clauseWHERE (template IS TRUE)for PostgreSQL (a regular index is less space efficient)
Since for GitLab.com we probably never use this I also propose adding a method called Label.has_templates? which returns true or false based on the template count, caching the result in Redis. We then modify Project#create_labels to skip its work if no templates exist.