Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Created Issue: Query: String parameter data type (length) cause query optimization issues [900]

$
0
0
Running the following LINQ query (model is included at the bottom of this issue):

```
var name = "My Blog";

var blog = (from b in db.Blogs
where b.Name == name
select b).FirstOrDefault();
```

Causes the following query to be executed:

```
exec sp_executesql N'SELECT TOP (1)
[Extent1].[BlogId] AS [BlogId],
[Extent1].[Name] AS [Name],
[Extent1].[Url] AS [Url]
FROM [dbo].[Blogs] AS [Extent1]
WHERE [Extent1].[Name] = @p__linq__0',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'My Blog'
```

The issue is that Name is a nvarchar(100) field but the parameter to match on name is typed as nvarchar(4000).

One example of the issues this can cause is in partitioned databases. If the column in question is used for partitioning then the type mismatch will cause SQL Server to not access just the single partition.

```
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
}

public class Blog
{
public int BlogId { get; set; }

[MaxLength(100)]
public string Name { get; set; }
public string Url { get; set; }
}
```

Viewing all articles
Browse latest Browse all 10318

Trending Articles



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