7

I inherited a system in which all updates (even to a single row/record) to MySQL tables are not done using UPDATE table_name SET. Rather, they are done by:

  1. Exporting the existing table to a CSV (text) file.
  2. Modifying the corresponding row(s) in the CSV file.
  3. Reloading the CSV file using LOAD DATA ... REPLACE INTO TABLE.

This is the first time I see such an approach to updating table records and I am wondering what could be the rational for that.

BTW, this scheme results in numerous thread synchronization issues because of the need to lock CSV files while updating.

I would appreciate an explanation or insights on the benefits of using LOAD DATA ... REPLACE INTO TABLE instead of UPDATE table_name SET.

2
  • 1
    If all you have is an Excel hammer, then ...
    – Rick James
    Oct 2, 2022 at 23:37
  • 1
    Just suggesting one nasty possibility: are the CSV files that are created on disk used by any other processes, so this method of updating the database has the side effect of passing on the data to those external processes? Sounds bloody awful :) If they are purely temporary then it sounds like a junior developer's first attempts at working with an RDBMS, sorry you're inherited such a mess!
    – Neek
    Sep 23 at 11:19

2 Answers 2

4

I would guess the previous developer read that LOAD DATA is faster for bulk-loading data. This is stated in the MySQL manual and repeated often on sites like this one.

Then they made a naive assumption that LOAD DATA is faster for everything, even single-row updates.

I'm pretty certain that the developer never measured the performance themselves.

It's very unusual to use LOAD DATA for anything besides bulk-loading data that is already in a file. I would never use it for single-row updates.

If I were in your shoes, I would change that code to use conventional UPDATEs. Don't suffer with the thread synchronization issues.

4

I would appreciate an explanation or insights on the benefits of using LOAD DATA ... REPLACE INTO TABLE instead of UPDATE table_name SET

I see no benefit on using LOAD DATA when you could use UPDATE table_name SET.

Use LOAD DATA when importing large amounts of data or data from files.

Personally, I have and would never use LOAD DATA for an UPDATE task.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.