1

I am uploading csv file contents into mysql database. I am not sure which approach is best & efficient & nice.

1] Using the LOAD DATA INFILE SQL statement

LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3); 

2] Using a script to parse and import the file for e.g. fgetcsv() function in php

2
  • 2
    Only way to tell for certain is to benchmark the two. But my money is most definitely on LOAD DATA LOCAL INFILE for speed and ease of use. Commented Mar 14, 2012 at 19:35
  • Using fgetcsv(), you will either need to call multiple INSERT statements, or write a bunch of code to build one very long INSERT statement with a VALUES (),(),(),(),()... chain. A native CSV import in MySQL will be far easier. Commented Mar 14, 2012 at 19:37

4 Answers 4

3

If you can be sure of the content of the cvs, use method 1.

But if you are unsure you may wish the script to do some sanity checking of the contents of the file. In this case use method 2.

Sign up to request clarification or add additional context in comments.

Comments

2

Loading directly into the database will likely be more efficient than using a script - you don't have the overhead of running the script, reading/parsing, putting together queries, etc. You just move the data over.

If you need to do this manually 100 times a day, then you're going to want something more automated, like a script. Then you need to consider your personal efficiency and level of effort.

Comments

1

Only reason to not use LOAD DATA INFILE SQL is if you where to process some of the data. For instance if had to convert or join data before insert. But if its a straight import this is by far the fastest.

Comments

1

I personally prefer LOAD DATA INFILE because loading script-wise usually uses multiple insert statements. If you need to modify the CSV it'll be much easier from a script, but in my experience LOAD DATA INFILE is faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.