-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question - Can Struct/Tuples be faster for Non Tracked Queries #15223
Comments
@ackava Can you post some code showing an example of a struct that you want to map in this way, and what the code looks like to query it from the database? |
Consider following query, I need to pull ids with some status and send it to some other service. With tuple, I don't need to create class. Consider, EmailRecipient entity has lot of fields (10+) and I only need two fields var list = await db.EmailRecipients
.Where(x => x.Email.Sent == true && x.Processed = false)
// tuple syntax..
.Select( x => ( x.RecipientID, x.EmailAddress ))
.Take(1000)
.ToListAsync();
foreach(var (id, email) in list) {
..... send push notification
}
// with struct..
public struct RecipientInfo {
public string ID;
public string Email;
}
var list = await db.EmailRecipients
.Where(x => x.Email.Sent == true && x.Processed = false)
.Select( x => new RecipientInfo { ID = x.RecipientID, Email = x.EmailAddress } )
.Take(1000)
.ToListAsync();
foreach(var r in list) {
..... send push notification
} In above example, I am not using change tracking, even for couple of fields, Tuple will only hold references to string and copying few bytes on stack will be lot faster compared to allocating and releasing these objects. |
Those queries should be working already. Did you try running them? Did you run into any issues? |
I got error which is already reported at, dotnet/roslyn#12897 Sorry I thought it was related to EF, but now I understand it is roslyn issue. Struct and Tuple are working fine if I use |
For non tracked query, with small entity for fairly large result requires unnecessary allocating so many small objects in heap.
Most non tracked objects are anyway readonly, so why force use of class?
Wouldn't it be faster to use struct?
The text was updated successfully, but these errors were encountered: