Groot csv-bestand parsen veroorzaakt itgenclr007: OutOfMemory

Ik wil graag een test delen waarin ik Query Tool tot zijn grenzen wilde pushen met CSV-bestanden.

Ik heb een CSV-bestand met 9,5 miljoen regels met alle Nederlandse postaddressen en postcodes. Dit bestand weegt slechts 2,5 GB.

Ik wilde het csv-parsen met Query Tool en het daarna naar SQL Server uploaden, omdat de SSMS-importtool niet echt meewerkte om dat bestand in te voeren.

Ik heb eerst wat testen gedaan op een kleiner CSV-bestand, en toen geprobeerd met het volledige bestand.

select c.* 
from read_file_text('C:\mydir\myfile.csv')@os fle
join csvtable
     ( passing fle.file_contents
       column delimiter ';'
       skip lines 1
       columns PostcodeNummers      int      position 1
       ,       PostcodeLetters      text     position 2
       ,       PostcodeVolledig     varchar2 position 3
       ,       Huisnummer           varchar2 position 4
       ,       HuisnummerLetter     varchar2 position 5
       ,       HuisnummerToevoeging varchar2 position 6
       ,       Straat               varchar2 position 7
       ,       Plaats               varchar2 position 8
       ,       Gemeente             varchar2 position 9
       ,       Provincie            varchar2 position 10
       ,       Doel                 varchar2 position 11
       ,       Oppervlakte          varchar2 position 12
       ,       VerblijfsStatus      varchar2 position 13
       ,       PandStatus           varchar2 position 14
       ,       BouwJaar             year     position 15
       ,       Latitude             varchar2 position 16
       ,       Longitude            varchar2 position 17
       ,       XCoordinaat          decimal  position 18
       ,       YCoordinaat          decimal  position 19
       ,       CreatieDatum         varchar2 position 20
       ,       NevenVestiging       varchar2 position 21
       ,       ObjectID             number   position 22
       ) c
limit  100

Bij het volledige bestand verbruikte Query Tool al het geheugen en gaf een OutOfMemoryException:

Duidelijk is dat de limit 100 pas na volledige verwerking wordt toegepast.

Dit is slechts ter informatie, geen blokkering. Ik ben uiteindelijk Excel + Power Query gaan gebruiken om mijn werk te doen en een selectie van de postcodes te maken.

There is a limitation on the following special drivers above 1 billion characters / 2 billion bytes:

  • csvtable: process text as CSV.
  • htmltable: process text as HTML tree.
  • jsontable: process text as JSON (non-NDJSON).
  • xmltable: process text as XML tree.
  • exceltable: a little different since it is binary input, but Excel files larger than 2 GB we have never encountered.

The text is read completely into memory taking 2 bytes per character (UTF-16 encoding). A 2 GB text file in UTF-8 typically converts into 4 GB of memory consumption. The .NET platform we use can not support text string larger than 2 GB, even on 64-bit.

See for another exempla System.OutOfMemoryException thrown on JSONTABLE with Twinfield BI-ON-Focus - 8 van anon44580209

For the future it is an option to make the processing streaming, consuming text by the character instead as a large text, but there are currently not sufficient use cases for it.

As a workaround, pre-process the files with a tool like the UNIX cut.

Een gerelateerd (Nederlands) probleem is Verwerken XML-bestand geeft itgenclr007 - Application is using more memory than available.

Vanaf BETA-release 22.1.56 maken een aantal optimalisaties het mogelijk tekstbestanden van meer dan 1 GB groot in de volgende formaten te verwerken:

  • JSON met jsontable
  • NDJSON met ndjsontable
  • CSV met csvtable
  • XML met xmltable

Voor meer informatie leest u alstublieft Processing very large text files in JSON, NDJSON, CSV and XML format in SQL.

Een verbetering is opgenomen in BETA-release 27.1 en later waarbij er geen bekende limieten meer zijn qua maximale bestandsomvang.