使用会员资格时的 LINQ 和 SQL 性能问题

发布于 2024-08-28 20:11:07 字数 1021 浏览 6 评论 0原文

我正在使用包含 50000 条记录的 ASPNET 成员资格,并且我们有另一个名为“scm_Users”的表,该表具有精确的记录数,它们不通过任何键链接。我有一个简单的 SQL:

select * from dbo.aspnet_Users a, dbo.scm_Users b
where a.UserName = b.UserName

我可以在不到 1 秒的时间内获取 50000 条记录。

在 LINQ 中(使用实体框架),当我尝试执行相同操作时:

IEnumerable<MembershipUser> allMembershipUsers = Membership.GetAllUsers().Cast<MembershipUser>();
ObjectQuery<User> users = this.DataContext.UserSet;

var    result = (from a in allMembershipUsers
                      from b in users
                      where a.UserName == b.UserName
                 select new 
                 {
                   ..... 
                 }).AsEnumerable();

当我将结果绑定到 Grid 时,它因 50000 条记录而超时。当我调试它并将鼠标悬停在“结果视图”上时,它也超时了。当然,如果我使用 Membership.FindUsersByName() 并限制记录数,它会正常返回结果。

而且,如果我将 50000 条记录直接绑定到网格而不查询成员资格,网格也可以工作。

 var    result = (from b in users
                  select b).AsEnumerable();

我做错了什么?

注意

I am using ASPNET membership with 50000 records, and we have another table called "scm_Users" which has exactly number of records, they are NOT linked by any key. I have a simple SQL:

select * from dbo.aspnet_Users a, dbo.scm_Users b
where a.UserName = b.UserName

I can get 50000 records in less than 1 second.

In LINQ, (using Entity Framework) when I am trying to do the same:

IEnumerable<MembershipUser> allMembershipUsers = Membership.GetAllUsers().Cast<MembershipUser>();
ObjectQuery<User> users = this.DataContext.UserSet;

var    result = (from a in allMembershipUsers
                      from b in users
                      where a.UserName == b.UserName
                 select new 
                 {
                   ..... 
                 }).AsEnumerable();

When I binded the result to Grid, it got timed out for 50000 records. When I debugged it and I moused over the "result view", it got timed out too. Of course, if I use Membership.FindUsersByName() and limit the number of records, it will return the results peoperly.

And, if I bind 50000 records directly to the Grid without querying on Membership, Grid works too.

 var    result = (from b in users
                  select b).AsEnumerable();

What did I do wrong?

N.B.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-09-04 20:11:07

什么是会员资格?在我看来,就像您从两个不同的数据源中提取数据一样,这会将它们加载到内存中并在内存中的集合上进行工作。您还在看似相当大的数据集上大量使用了强制转换。

尝试更多类似这样的事情:

var result = from a in DataContext.MembershipUsers
             join b in DataContext.UsersSet on a.UserName equals b.UserName
             select new { ... };

What is Membership? It looks to me like you pulling from 2 different data sources, which will load both of them into memory and do the work on the sets in memory. You are also using casts a lot on what appear to be rather large datasets.

Try something more like this:

var result = from a in DataContext.MembershipUsers
             join b in DataContext.UsersSet on a.UserName equals b.UserName
             select new { ... };
甜味超标? 2024-09-04 20:11:07

一个显着的区别是您流式传输的是 100K 记录而不是 50K。 SQL 查询将在服务器上合并两个结果,并返回两个表之间连接的 50K 行数据。在您的示例中,您通过 GetAllUsers 拉取 50K,通过 UserSet 拉取其他 50K。问题是,除非您直接通过 DataContext 查询 aspnet Membership 表,否则您将无法避免这种情况。

One significant difference is that you are streaming 100K records instead of 50K. The SQL query will combine the two results on the server and return the 50K rows of data joined between the two tables. In your example, you are pulling down the 50K via GetAllUsers and the other 50K via UserSet. The catch is that unless you query the aspnet Membership table directly via your DataContext, you will not be able to avoid this.

夜深人未静 2024-09-04 20:11:07

我想说是演员造成了速度减慢,而不是查询。在没有演员的情况下尝试一下,并返回原始实体,看看是否可以加快速度。

I'd say it's the Cast that's causing the slowdown, not the query. Try it without the cast and get the raw entities back to see if that speeds things up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文