Skip to content

Commit

Permalink
Use result.rows as fastest alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Jun 8, 2020
1 parent cfb7c16 commit 2ad2425
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Expand Up @@ -505,12 +505,12 @@ def table_structure_with_collation(table_name, basic_structure)
# Result will have following sample string
# CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
# "password_digest" varchar COLLATE "NOCASE");
result = exec_query(sql, "SCHEMA").first
result = query_value(sql, "SCHEMA")

if result
# Splitting with left parentheses and discarding the first part will return all
# columns separated with comma(,).
columns_string = result["sql"].split("(", 2).last
columns_string = result.split("(", 2).last

columns_string.split(",").each do |column_string|
# This regex will match the column name and collation type and will save
Expand Down
8 changes: 3 additions & 5 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -367,14 +367,12 @@ def compute_cache_version(timestamp_column) # :nodoc:
arel = query.arel
end

result = connection.select_one(arel, nil)
size, timestamp = connection.select_rows(arel, nil).first

if result
if size
column_type = klass.type_for_attribute(timestamp_column)
timestamp = column_type.deserialize(result["timestamp"])
size = result["size"]
timestamp = column_type.deserialize(timestamp)
else
timestamp = nil
size = 0
end
end
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -316,7 +316,7 @@ def exists?(conditions = :none)

relation = construct_relation_for_exists(conditions)

skip_query_cache_if_necessary { connection.select_one(relation.arel, "#{name} Exists?") } ? true : false
skip_query_cache_if_necessary { connection.select_rows(relation.arel, "#{name} Exists?").size == 1 }
end

# This method is called whenever no records are found with either a single
Expand Down Expand Up @@ -416,8 +416,8 @@ def limited_ids_for(relation)

relation = relation.except(:select).select(values).distinct!

id_rows = skip_query_cache_if_necessary { @klass.connection.select_all(relation.arel, "SQL") }
id_rows.map { |row| row[primary_key] }
id_rows = skip_query_cache_if_necessary { @klass.connection.select_rows(relation.arel, "SQL") }
id_rows.map(&:last)
end

def using_limitable_reflections?(reflections)
Expand Down

0 comments on commit 2ad2425

Please sign in to comment.