<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: darwinpasco25</title>
    <description>The latest articles on DEV Community by darwinpasco25 (@darwinpasco25).</description>
    <link>https://dev.to/darwinpasco25</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F810115%2F36e6312a-91c3-4468-9042-833124715478.png</url>
      <title>DEV Community: darwinpasco25</title>
      <link>https://dev.to/darwinpasco25</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/darwinpasco25"/>
    <language>en</language>
    <item>
      <title>How to Insert Thousands of Rows from an Excel / CSV File into a database with a single database call</title>
      <dc:creator>darwinpasco25</dc:creator>
      <pubDate>Sun, 06 Feb 2022 07:29:22 +0000</pubDate>
      <link>https://dev.to/darwinpasco25/how-to-insert-thousands-of-rows-of-from-an-excel-csv-file-into-a-database-with-a-single-database-call-1abh</link>
      <guid>https://dev.to/darwinpasco25/how-to-insert-thousands-of-rows-of-from-an-excel-csv-file-into-a-database-with-a-single-database-call-1abh</guid>
      <description>&lt;p&gt;Most of the procedures I see programmers do when uploading data from a excel or csv file is &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loop through the file per row&lt;/li&gt;
&lt;li&gt;Submit the row to the database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This method does not only takes so many hours to finish but also takes a lot of resources from the server. So these tasks are usually done during off-peak hours.&lt;/p&gt;

&lt;p&gt;But there is a way to do this without taking so much time and resources. You can actually insert tens of thousands of records from a file in a single database call for your application.&lt;/p&gt;

&lt;p&gt;This is done by using the ff procedures.&lt;br&gt;
From the app:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the excel / csv file as a DataTable&lt;/li&gt;
&lt;li&gt;Serialize the DataTable to JSON string&lt;/li&gt;
&lt;li&gt;Submit the JSON String as parameter (JSON / NVARCHAR datatype) to a stored procedure&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the MySQL / SQL Server / PostgreSQL stored procedure&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the JSON string as a table using JSON_TABLE (MySQL) OPEN_JSON (SQL Server) or JSON_EACH(PostgreSQL) table function&lt;/li&gt;
&lt;li&gt;Insert the converted JSON table to a table in the database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Depending on the data transformations you will do prior to actually inserting the data, the total operation can take as little as 1 second to insert 10,000 rows or more. I regularly insert / update 50,000 rows from an Excel file to an SQL Server database in RDS and I am able to do so in 1-2 seconds, network latency including. Most I did is 200,000 rows without any problem during peak hours and the users didn't even notice it.&lt;/p&gt;

&lt;p&gt;Sample Code&lt;br&gt;
&lt;a href="https://github.com/darwinpasco25/JsonFileUploadApp"&gt;https://github.com/darwinpasco25/JsonFileUploadApp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sample Excel file with 10,000 rows of records&lt;br&gt;
&lt;a href="https://github.com/darwinpasco25/JsonFileUploadApp/blob/main/JsonFileUploadApp/JsonFileUploadApp/sales.xlsx"&gt;https://github.com/darwinpasco25/JsonFileUploadApp/blob/main/JsonFileUploadApp/JsonFileUploadApp/sales.xlsx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sample MySQL database&lt;br&gt;
&lt;a href="https://github.com/darwinpasco25/JsonFileUploadDB"&gt;https://github.com/darwinpasco25/JsonFileUploadDB&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>json</category>
      <category>storedprocedure</category>
    </item>
  </channel>
</rss>
