views:

126

answers:

3

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.

A: 

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.

Dave Swersky
+2  A: 

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 { ... };
NickLarsen
beat me to it.... ;)
GordonB
Heh. Same here.
Thomas
Membership is from Microsoft:http://msdn.microsoft.com/en-us/library/yh26yfzy.aspx
userb00
A: 

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.

Thomas
You are right, maybe it is not easy to avoid when dealing with this size of data. On the other hand, I am trying NOT to have aspnet_Membership and aspnet_Users in DataContext, I feel that it is bad practice in case I need to switch Membership Provider such as Active Directory, then I don't have to rewrite any code.
userb00