Vivian River
Vivian River

Reputation: 32430

System.OutOfMemoryException when querying large SQL table

I've written a SQL query that looks like this:

SELECT * FROM MY_TABLE WHERE ID=123456789;

When I run it in the Query Analyzer in SQL Server Management Studio, the query never returns; instead, after about ten minutes, I get the following error: System.OutOfMemoryException

My server is Microsoft SQL Server (not sure what version).

SELECT * FROM MY_TABLE; -- return 44258086
SELECT * FROM MY_TABLE WHERE ID=123456789; -- return 5

The table has over forty million rows! However, I need to fetch five specific rows!

How can I work around this frustrating error?

Edit: The server suddenly started working fine for no discernable reason, but I'll leave this question open for anyone who wants to suggest troubleshooting steps for anyone else with this problem.

Upvotes: 1

Views: 1593

Answers (2)

C-Pound Guru
C-Pound Guru

Reputation: 16368

According to http://support.microsoft.com/kb/2874903:

This issue occurs because SSMS has insufficient memory to allocate for large results. Note SSMS is a 32-bit process. Therefore, it is limited to 2 GB of memory.

The article suggests trying one of the following:

  • Output the results as text
  • Output the results to a file
  • Use sqlcmd

You may also want to check the server to see if it's in need of a service restart--perhaps it has gobbled up all the available memory?

Another suggestion would be to select a smaller subset of columns (if the table has many columns or includes large blob columns).

Upvotes: 2

Sebastian
Sebastian

Reputation: 17453

If you need specific data use an appropriate WHERE clause. Add more details if you are stuck with this.

Alternatively write a small application which operates using a cursor and does not try to load it completely into memory.

Upvotes: 1

Related Questions