简体   繁体   English

查询大型SQL表时出现System.OutOfMemoryException

[英]System.OutOfMemoryException when querying large SQL table

I've written a SQL query that looks like this: 我编写了一个如下所示的SQL查询:

SELECT * FROM MY_TABLE WHERE ID=123456789;

When I run it in the Query Analyzer in SQL Server Management Studio, the query never returns; 当我在SQL Server Management Studio的查询分析器中运行它时,查询永远不会返回; instead, after about ten minutes, I get the following error: System.OutOfMemoryException 相反,大约十分钟后,我收到以下错误: System.OutOfMemoryException

My server is Microsoft SQL Server (not sure what version). 我的服务器是Microsoft SQL Server(不确定是什么版本)。

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. 编辑:服务器突然开始工作正常,没有明显的原因,但我会将此问题留给任何想要为其他任何人提出此问题的故障排除步骤的人。

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

This issue occurs because SSMS has insufficient memory to allocate for large results. 出现此问题的原因是SSMS没有足够的内存来分配大的结果。 Note SSMS is a 32-bit process. 注意SSMS是一个32位进程。 Therefore, it is limited to 2 GB of memory. 因此,它限制为2 GB的内存。

The article suggests trying one of the following: 本文建议尝试以下方法之一:

  • Output the results as text 将结果输出为文本
  • Output the results to a file 将结果输出到文件
  • Use sqlcmd 使用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). 另一个建议是选择较小的列子集(如果表有很多列或包含大的blob列)。

If you need specific data use an appropriate WHERE clause. 如果需要特定数据,请使用适当的WHERE子句。 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. 或者编写一个使用游标操作的小应用程序,而不是尝试将其完全加载到内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试运行由SQL Server Management Studio生成的创建表脚本时出现System.OutOfMemoryException - System.OutOfMemoryException when trying to run a create table script generated by SQL Server Management studio SQL转换脚本导致System.OutOfMemoryException - SQL conversion script causes System.OutOfMemoryException Linq System.OutofMemoryException - Linq System.OutofMemoryException System.OutOfMemoryException - System.OutOfMemoryException 异常'System.OutOfMemoryException' - Exception 'System.OutOfMemoryException' system.dll中的异常“ System.OutOfMemoryException” - Exception 'System.OutOfMemoryException' in system.dll 在foreach循环中获取“ System.OutOfMemoryException” - Getting 'System.OutOfMemoryException' during foreach loop SSMS 2012 System.OutOfMemoryException(客户端)和SQL提示符(调试信息包含) - SSMS 2012 System.OutOfMemoryException (client side) and SQL Prompt (debug info incl) 引发了类型为'System.OutOfMemoryException'的异常。 使用IDataReader时使用C# - Exception of type 'System.OutOfMemoryException' was thrown. C# when using IDataReader 存储过程中使用while循环是否会导致System.OutOfMemoryException? - Is use of while loop in stored procedure causing System.OutOfMemoryException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM