将10GB CSV导入SSMS 18时遇System Out of Memory错误求助
10GB CSV文件导入SQL Server触发内存不足错误的解决建议
尝试将约10GB的Excel CSV文件导入SQL数据库,使用Microsoft SQL Server Management Studio 18的平面文件导入向导时,导入进行10-15秒后触发System.OutOfMemoryException错误,详细错误输出如下:
详细错误输出: 向表中插入数据时出错。(Microsoft.SqlServer.Import.Wizard) ------------------------------ 程序位置: 在 Microsoft.SqlServer.Import.Wizard.InsertData.ResultCheck(Result result) 在 Microsoft.SqlServer.Import.Wizard.InsertData.DoWork() 在 Microsoft.SqlServer.Management.TaskForms.SimpleWorkItem.Run() =================================== 向表中插入数据时出错。(Microsoft.SqlServer.Prose.Import.SDS) ------------------------------ 程序位置: 在 Microsoft.SqlServer.Prose.Import.BcpTextSynthesis.InsertIntoDB(String inputFilePath, String tableName, String schemaName, IReadOnlyList`1 columnInfo, SqlConnection connection, Int32 batchSize, SqlTransaction transaction, IList`1 allFinalTransformations, IList`1 allFinalTransformationColumns, IList`1 allFinalColNames) 在 Microsoft.SqlServer.Prose.Import.BcpProcess.CreateTableAndInsertDataIntoDb(String connectionString, Int32 batchSize, String azureAccessToken) =================================== 引发了类型为'System.OutOfMemoryException'的异常。(mscorlib) ------------------------------ 程序位置: 在 System.Text.StringBuilder.ToString() 在 Microsoft.ProgramSynthesis.Read.FlatFile.Semantics.Semantics.<SplitLines>d__17.MoveNext() 在 Microsoft.ProgramSynthesis.Read.FlatFile.Semantics.Semantics.<Csv>d__9.MoveNext() 在 System.Linq.Enumerable.<SkipIterator>d__31`1.MoveNext() 在 System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() 在 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 在 Microsoft.SqlServer.Prose.Import.BcpTextSynthesis.InsertIntoDB(String inputFilePath, String tableName, String schemaName, IReadOnlyList`1 columnInfo, SqlConnection connection, Int32 batchSize, SqlTransaction transaction, IList`1 allFinalTransformations, IList`1 allFinalTransformationColumns, IList`1 allFinalColNames)
- 导入向导界面截图
- 内存不足错误截图
解决建议
改用bcp命令行工具:SSMS导入向导是GUI工具,内存管理效率远低于命令行工具。使用bcp批量导入,示例命令:
bcp YourDatabase.dbo.YourTable in "D:\YourLargeFile.csv" -S YourServerInstance -U YourLogin -P YourPassword -c -t, -r\n -b 10000参数说明:
-b 10000表示每批次导入10000行,可根据系统内存调整;-c使用字符数据格式;-t,指定逗号为字段分隔符;-r\n指定换行符为行结束符。拆分大CSV文件:将10GB文件拆分为多个小文件(比如每个1GB)后分别导入。可用PowerShell脚本实现,示例:
$inputFile = "D:\LargeFile.csv" $outputDir = "D:\SplitCSV\" $linesPerFile = 500000 # 每个文件50万行,按需调整 $fileIndex = 1 # 创建输出目录 if (-not (Test-Path $outputDir)) { New-Item -ItemType Directory -Path $outputDir | Out-Null } Get-Content $inputFile -ReadCount $linesPerFile | ForEach-Object { $outputFile = Join-Path $outputDir "Split_$fileIndex.csv" $_ | Out-File $outputFile -Encoding UTF8 $fileIndex++ }调整导入向导批次大小:若坚持使用SSMS向导,在"指定表复制或查询"步骤后进入"编辑映射",点击"高级"选项,减小批次大小(默认值可能过大,建议改成10000或更小),避免一次性加载过多数据到内存。
使用64位SSMS:确认当前使用的是64位版本的SSMS 18(32位进程内存上限约2GB,64位可利用更多系统内存),若为32位则切换到64位版本。
预创建目标表:提前在SQL Server中创建匹配CSV结构的表,指定合适的数据类型(如用
VARCHAR(50)替代VARCHAR(MAX)),减少导入时的类型转换开销和内存占用。
内容的提问来源于stack exchange,提问作者jchian
相关产品推荐
相关产品推荐

