提交 634d28e6 编写于 作者: K kennyj

Fix #8414. Performance problem with postgresql adapter primary_key function.

上级 d3f4625a
## Rails 4.0.0 (unreleased) ##
* Fix performance problem with primary_key method in PostgreSQL adapter when having many schemas.
Uses pg_constraint table instead of pg_depend table which has many records in general.
Fix #8414
*kennyj*
* Do not instantiate intermediate Active Record objects when eager loading.
These records caused `after_find` to run more than expected.
Fix #3313
......
......@@ -305,12 +305,11 @@ def pk_and_sequence_for(table) #:nodoc:
# Returns just a table's primary key
def primary_key(table)
row = exec_query(<<-end_sql, 'SCHEMA').rows.first
SELECT DISTINCT(attr.attname)
SELECT attr.attname
FROM pg_attribute attr
INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
WHERE cons.contype = 'p'
AND dep.refobjid = '#{quote_table_name(table)}'::regclass
AND cons.conrelid = '#{quote_table_name(table)}'::regclass
end_sql
row && row.first
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册