views:

235

answers:

1

I have a very simple linq to sql query in C#:

int acctNum = 12345;                    

var query = from p in db.table
            where p.ACCT_NO == acctNum
            select p;

This generates the following SQL:

exec sp_executesql N'SELECT [t0].field1, [t0].field2, [t0].ACCT_NO
FROM [dbo].[table] AS [t0]
WHERE [t0].[ACCT_NO] = @p0', N'@p0 decimal(29,0)', @p0 = 12345

For some reason, this is taking an incredibly long time to run (several minutes). If I run an equivalent query in management studio (select * from table where acct_no = 12345), it takes less than a second on a very large table (~7MM rows). After some digging with the SQL profiler, I found that linq is passing the acctNum parameter as a Decimal(29,0) while the field is stored in the database as a Numeric(18,0). If I take the generated SQL and just change the parameter type from decimal to numeric, it runs in less than a second. In the profiler, I can see that the linq version uses almost 2 million reads versus about 400 for the numeric parameter query. How can I force linq to pass this parameter as numeric instead of decimal?

+5  A: 

Most likely the problem lies with the type of p.ACCT_NO (in other words it was probably generated as a floating-point numeric type). Make sure that this property is also typed as an int and it should work.

Andrew Hare
Do you mean the SQL data type of the underlying database field? Its a numeric(18,0).
maxpower47
No, I mean the type of the `ACCT_NO` property on whatever type `p` is.
Andrew Hare
In VS, p.ACCT_NO shows as Decimal. How can I change that?
maxpower47
Found it. Just had to change the type of the property in the LINQ designer for that field to int. Works like a charm.
maxpower47
This means the int is changed to decimal(18,0) in SQL of course, even if it works
gbn
No, the field in SQL is a `numeric(18,0)` which is why the performance increases when the OP updated the type of the property.
Andrew Hare
OK: does the int get sent to SQL? Or would LINQ now generate decimal(18,0). If the first, the int would be converted to match the column because of datatype precedence and LINQ still does not use the correct datatype.
gbn