Skip to content

Commit 191510a

Browse files
aidanharanalbus522
andauthored
Improve performance of view default function lookup (#1074)
* Improve performance of view default function lookup (#1073) * Update CHANGELOG.md --------- Co-authored-by: David Genord II <[email protected]>
1 parent e58ff7a commit 191510a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Changed
4+
5+
- [#1073](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1073) Improve performance of view default function lookup
6+
17
## v7.0.3.0
28

39
#### Fixed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ def column_definitions(table_name)
371371
view_exists = view_exists?(table_name)
372372
view_tblnm = view_table_name(table_name) if view_exists
373373

374+
if view_exists
375+
results = sp_executesql %{
376+
SELECT c.COLUMN_NAME AS [name], c.COLUMN_DEFAULT AS [default]
377+
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
378+
WHERE c.TABLE_NAME = #{quote(view_tblnm)}
379+
}.squish, "SCHEMA", []
380+
default_functions = results.each.with_object({}) {|row, out| out[row["name"]] = row["default"] }.compact
381+
end
382+
374383
sql = column_definitions_sql(database, identifier)
375384

376385
binds = []
@@ -402,13 +411,8 @@ def column_definitions(table_name)
402411
ci[:default_function] = begin
403412
default = ci[:default_value]
404413
if default.nil? && view_exists
405-
default = select_value %{
406-
SELECT c.COLUMN_DEFAULT
407-
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
408-
WHERE
409-
c.TABLE_NAME = '#{view_tblnm}'
410-
AND c.COLUMN_NAME = '#{views_real_column_name(table_name, ci[:name])}'
411-
}.squish, "SCHEMA"
414+
view_column = views_real_column_name(table_name, ci[:name])
415+
default = default_functions[view_column] if view_column.present?
412416
end
413417
case default
414418
when nil

0 commit comments

Comments
 (0)