使用会员资格时的 LINQ 和 SQL 性能问题
我正在使用包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
什么是
会员资格
?在我看来,就像您从两个不同的数据源中提取数据一样,这会将它们加载到内存中并在内存中的集合上进行工作。您还在看似相当大的数据集上大量使用了强制转换。尝试更多类似这样的事情:
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:
一个显着的区别是您流式传输的是 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.
我想说是演员造成了速度减慢,而不是查询。在没有演员的情况下尝试一下,并返回原始实体,看看是否可以加快速度。
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.