related tags: optimize calculate_from_sample.
calculate_from_sample is called when clicking the General / Artist / Copyright / Characters buttons during tag editing. Currently it's slow because it counts up the tags in ruby, and because it makes repeated calls to memcache in the inner loop when filtering tags by category. This changes it to do the counting and filtering entirely in SQL using a GROUP BY tag / COUNT(*) aggregation. This is faster and makes larger sample sizes more feasible.
This commit is contained in:
@@ -1593,6 +1593,17 @@ class Post < ActiveRecord::Base
|
||||
where("md5 >= ?", key).reorder("md5 asc").first
|
||||
end
|
||||
|
||||
def sample(query, sample_size)
|
||||
CurrentUser.without_safe_mode do
|
||||
tag_match(query).reorder(:md5).limit(sample_size)
|
||||
end
|
||||
end
|
||||
|
||||
# unflattens the tag_string into one tag per row.
|
||||
def with_unflattened_tags
|
||||
joins("CROSS JOIN unnest(string_to_array(tag_string, ' ')) AS tag")
|
||||
end
|
||||
|
||||
def pending
|
||||
where("is_pending = ?", true)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user