Raw SQL Query Question
I have a raw SQL query that I need to execute against a database that's external to my application. The query is expected to return >5,000,000 records. Therefore, returning the records in memory (list) causes the application to crash due to running out of memory, even on a server with 122GB.
Is it possible to enumerate over the records instead, so that I don't have to deal with them all in memory?
13 Replies
Can you really not paginate this data?
The actual query itself takes ~15 minutes to run via SSMS, and there's nothing I can do about that because it's a 3rd party system
I can show you if you'd like, but not really. It's merging a bunch of tables together
you have access to the database ?
Yes
I believe read-only for this client though
you can't do a view and paginate the view ?
You could try
.AsEnumerable()
instead of .ToList()
, this will give you the ability to process each record individually without loading them all: <>I'd have to look into that. If I can, how would that change the C# execution? Just run the execute as normal?
I did try to use
AsEnumerable
, but I received a message that my dbContext changed or was no longer validcause you don't inject your dbcontext
using (DbContext tempContext = new DbContext(con)) {
^
can you put the query results into a temporary table then query that in pages?
Yeah, the data controller and the data processor are separate
I could probably do that, would just have to figure out how to set it up
I fixed the context issue. It's just sort of an odd way to do it because I was trying to keep DB logic separate from app logic. But since this is only 1 function required for the abstract for a specific use-case, it'll be fine