Today I need to fetch data from *.csv (which has nearly 15,000 records) file and insert such data into our database. At that time, I need to consider how to retrieve data from this file and how to insert into our database. So, I need to consider which method I need to use between "looping in array" and "ListToArray". That's why I've wrote with "looping in array" first.
Here is simple list data including in CSV file.
2008|MIKE|MALE|ANDROID DEVELOPER
3009|RAYMON|MALE|APPLE DEVELOPER
looping in array
2 <cfloop index="ind" list="#myindex#" delimiters="|">
3 <cfoutput>- #ind# -<br></cfoutput>
4 </cfloop>
5 <hr>
6</cfloop>
After rending above coding, it can output correct data but executing time is 16ms.
Ok, no problem. Let's test with "ListToArray".
ListToArray
2 <cfset ind = ListToArray(myindex, "|", true, false)>
3 <cfoutput>- #ind[1]# -<br></cfoutput>
4 <cfoutput>- #ind[2]# -<br></cfoutput>
5 <cfoutput>- #ind[3]# -<br></cfoutput>
6 <cfoutput>- #ind[4]# -<br></cfoutput>
7 <hr>
8</cfloop>
After rending above coding either, it can output correct data but executing time is 15ms.
To compare both coding, 1ms is different for 3 records. To consider 15,000 records, I cannot estimate how much milliseconds will be different.

Android
Top of Page
#1 by James Moberg on 9/30/11 - 7:55 PM
#2 by ppshein on 9/30/11 - 10:33 PM
#3 by Jon Briccetti on 10/2/11 - 11:06 AM
#4 by ppshein on 10/2/11 - 8:21 PM
#5 by Jon Briccetti on 10/3/11 - 7:14 AM
also , check out cfhttp - you can "slurp" a csv file directly into a query object using the name attribute. here is another blog entry on doing that from Ben http://www.bennadel.com/blog/1903-Parsing-CSV-Data...
but hey, your way is great too and i know you were simply exploring the benefits of converting the list to an array for performance reasons... so i'm only usggesting these alternative methods so you can find out which way is faster!!!
good luck
Jon
#6 by ppshein on 10/3/11 - 7:58 AM
#7 by James Moberg on 10/3/11 - 10:40 AM
If your CSV file has formatting issues, OpenCSV will allow you to read it. It doesn't need to reside in the webpath (like CFHTTP.) I've previously used it with CF7 and highly recommend it. If you are able to install it on the server, it's a better solution when reading client-provided CSV data where you have no control over the format.
http://www.bennadel.com/blog/1903-Parsing-CSV-Data...
When timing performance, I usually use GetTickCount() (to save the execution time as a variable) or use CFTimer.
http://cfdocs.org/cftimer
#8 by James Moberg on 10/3/11 - 10:48 AM
Another import technique I've been using using is SQL BULK IMPORT. I have to load multiple large tab-delimited files on a schedule and Microsoft SQL Server actually handled the import fast, better and used less code than ColdFusion. (CFFile read and looping was causing timeouts and memory issues.) I had to create a temporary VIEW in order to import the un-typed data safely.
http://cfsearching.blogspot.com/2008/08/ms-sql-sel...
Here's some sample code:
CREATE VIEW TempImport AS SELECT #ColumnHeaderstoImport# FROM MyDatabaseTable
BULK INSERT TempImport FROM 'c:\importfile.txt' WITH(CODEPAGE='65001', FIELDTERMINATOR='\t', ROWTERMINATOR='\n', KEEPNULLS, FIRSTROW =2)
DROP VIEW TempImport
#9 by ppshein on 10/3/11 - 8:58 PM
Thanks. The first post will be kinda useful for me using with cfhttp. I haven't noticed before using cfhttp to parse CSV file will be faster than normal looping. I'll try to use soon.
For second post, I cannot use SQL BULK IMPORT because we're using Oracle instead. Anyway, thanks.