4

From the docs, it states:

The CSV storage engine stores data in text files using comma-separated values format.

What are the advantages of this? Here are some I can think of:

  1. You can edit the CSV files using simple text editor (however, you can export data easily using SELECT INTO OUTFILE)
  2. Can be easily imported into Spreadsheet programs
  3. Lightweight and maybe better performance (wild guess)

What are some disadvantages?

  1. No indexing
  2. Cannot be partitioned
  3. No transactions
  4. Cannot have NULL values

Granted this (non-exhaustive) list of advantages and disadvantages, in what practical scenarios should I consider using the CSV storage engine over others?

6
  • this is opinion based. Oct 21, 2014 at 16:41
  • 3
    @DanielA.White This is not opinion-based. I am not asking whether I should use CSV or not. I am asking for people to give me scenarios where using CSV can be considered useful. This is not opinion-based.
    – dayuloli
    Oct 21, 2014 at 16:42
  • well its either opinion based or too broad. Oct 21, 2014 at 16:43
  • @DanielA.White Thank you for your comment. I am genuinely trying to get an answer here, as I am learning about the different storage engines and which ones to use for my web application. I have made an effort to find an answer but have not been able to. How do you suggest I narrow the question so it will be acceptable for you and other SO users?
    – dayuloli
    Oct 21, 2014 at 16:46
  • 1
    Considering it is very simple to SELECT INTO OUTFILE to get CSV from a regular table, I would find little utility in working with the CSV engine format unless you happens to have an application that needed to work directly with this data locally without having access to mysql daemon and had some other application that was continually modifying the table via MySQL. Even then locking the table for reads/writes would be a nightmare.
    – Mike Brant
    Oct 21, 2014 at 16:50

2 Answers 2

13

I seldom use the CSV storage engine. One scenario I have found it useful, however, is for bulk data imports.

  1. Create a table with columns matching my input CSV file.
  2. Outside of mysql, just using a shell prompt, mv the CSV file into the MySQL data dictionary, overwriting the .csv file that belongs to my table I just created.
  3. ALTER TABLE mytable ENGINE=InnoDB

Voilà! One-step import of a huge CSV data file using DDL instead of INSERT or LOAD DATA.

Granted, it's less flexible than INSERT or LOAD DATA, because you can't do NULLs or custom overrides of individual columns, or any "replace" or "ignore" features for handling duplicate values. But if you have an input file that is exactly what you want to import, it could make the import very easy.

2
  • 1
    Thank you for your answer! Just as I thought there isn't much use for CSV tables for any long-term use. I wonder why they spent so much effort in creating it?
    – dayuloli
    Oct 22, 2014 at 8:50
  • 1
    Could be a variety of reasons. If only one customer needed it when MySQL was a new company, they might have been motivated to keep the customer. Or they might have thought it was a proof of concept of the reusable storage engine architecture, and pretty easy to implement. Oct 22, 2014 at 15:26
-1

This is a tad bit hacky, but as of MySQL 8, assuming you know the data structure beforehand and have permissions in the CSV-based schema directory, you can create the table definition in MySQL and then overwrite the generated CSV table file in the data directory with a symlink to the data file:

mysql --execute="CREATE TABLE TEST.CSV_TEST ( test_col VARCHAR(255) ) ENGINE=CSV;"
ln -sf /path/to/data.file /var/lib/mysql/TEST/CSV_TEST.CSV

An advantage here is that this completely obviates the need to run import operations (via LOAD DATA INFILE, etc.), as it allows MySQL to read directly from the symlinked file as if it were the table file.

Drawbacks beyond those inherent to the CSV engine:

  • table will contain header row if there is one (you'd need to filter it out from read operations)
  • table metadata in INFORMATION_SCHEMA will not update using this method, will just show the CREATE_TIME for which the initial DDL is run

Note this method is obviously more geared toward READ operations, though update/insert operations could be conducted on the command line using SELECT ... INTO OUTFILE and then copying onto/appending the source file.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.