Quantcast
Channel: EntityFramework Reverse POCO Code First Generator
Viewing all articles
Browse latest Browse all 1642

Closed Unassigned: Fix for slow foreign key information retrieval for SQL Server [81]

$
0
0
It takes long time to get FK information on my database, about a 100 second for ~270 foreign keys (SQL 2012). I replaced the query (declared as private const string ForeignKeySQL in EF.Reverse.POCO.Core.ttinclude) with another query based on sys system views, and it now takes only 120 ms :):

```
private const string ForeignKeySQL = @"
select
FK_Table = o2.name,
FK_Column = c2.name,
PK_Table = o1.name,
PK_Column = c1.name,
Constraint_Name = object_name(f.object_id),
fkSchema = schema_name(o2.schema_id),
pkSchema = schema_name(o1.schema_id),
primaryKey = c1.name
--PK_NAME = i.name
from
sys.objects o1,
sys.objects o2,
sys.columns c1,
sys.columns c2,
sys.foreign_keys f inner join
sys.foreign_key_columns k on (k.constraint_object_id = f.object_id) inner join
sys.indexes i on (f.referenced_object_id = i.object_id and f.key_index_id = i.index_id)
where
o1.object_id = f.referenced_object_id and
o2.object_id = f.parent_object_id and
c1.object_id = f.referenced_object_id and
c2.object_id = f.parent_object_id and
c1.column_id = k.referenced_column_id and
c2.column_id = k.parent_column_id
order by 1, 2, 3, 4
";
```
other queries can probably also be made faster, but this one took the longest time to execute anyway.

Viewing all articles
Browse latest Browse all 1642

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>