From bccc4c62268cfdc34764eb0b732c0eda70cf2a8a Mon Sep 17 00:00:00 2001 From: Chris Griego Date: Wed, 27 Oct 2021 14:29:54 -0500 Subject: [PATCH] Don't `SELECT * FROM information_schema.tables` Depending on MySQL version and configuration, this is more than two orders of magnitude slower than selecting only the two columns we need for this query, which don't require reading any of the data files or recalculating innodb stats. https://dev.mysql.com/doc/refman/5.6/en/information-schema-optimization.html Performance regression was introduced in #39712, before then the query took around 40-50ms on my test environment, after it took 4-8s. With this change it's back to 40-50ms. --- .../connection_adapters/mysql/schema_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb index 7df00b4a6b..42ca9bc732 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb @@ -206,7 +206,7 @@ module ActiveRecord def data_source_sql(name = nil, type: nil) scope = quoted_scope(name, type: type) - sql = +"SELECT table_name FROM (SELECT * FROM information_schema.tables " + sql = +"SELECT table_name FROM (SELECT table_name, table_type FROM information_schema.tables " sql << " WHERE table_schema = #{scope[:schema]}) _subquery" if scope[:type] || scope[:name] conditions = []