Question

I am setting up an uploader (using php) for my client where they can select a CSV (in a pre-determined format) on their machine to upload. The CSV will likely have 4000-5000 rows. Php will process the file by reading each line of the CSV and inserting it directly into the DB table. That part is easy.

However, ideally before appending this data to the database table, I'd like to review 3 of the columns (A, B, and C) and check to see if I already have a matching combo of those 3 fields in the table AND IF SO I would rather UPDATE that row rather than appending. If I DO NOT have a matching combo of those 3 columns I want to go ahead and INSERT the row, appending the data to the table.

My first thought is that I could make columns A, B, and C a unique index in my table and then just INSERT every row, detect a 'failed' INSERT (due to the restriction of my unique index) somehow and then make the update. Seems that this method could be more efficient than having to make a separate SELECT query for each row just to see if I have a matching combo already in my table.

A third approach may be to simply append EVERYTHING, using no MySQL unique index and then only grab the latest unique combo when the client later queries that table. However I am trying to avoid having a ton of useless data in that table.

Thoughts on best practices or clever approaches?

Was it helpful?

Solution

If you make the 3 columns the unique id, you can do an INSERT with ON DUPLICATE KEY.

INSERT INTO table (a,b,c,d,e,f) VALUES (1,2,3,5,6,7)
  ON DUPLICATE KEY UPDATE d=5,e=6,f=7;

You can read more about this handy technique here in the MySQL manual.

OTHER TIPS

If you add a unique index on the ( A, B, C ) columns, then you can use REPLACE to do this in one statement:

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top