I have a csv file, maybe 1000 or more rows, and echo row’s data is related to two table in mysql. Example,
table `book`(id, title, ...)
table `scene`(id, book_id, short_content, position...)
And the csv file look like:
title,short_content1,position1,short_content2,position2,...
...
I want to extract data from it and save it to mysql. Also in the mysql maybe already have the same record, so I don’t want save the same data to mysql. My solution is:
- First, fetch all the data related to the file, a big data set labeled
BS
. - Second, for each row in file compare to the
BS
and create the book, and store the book_id for hisscene
. - Finally, batch create the
scene
.
This is my poor solution, and if the csv file has 1000 rows, then the solution will take a large memory space and 1000 + 1 times mysql calls.
I see Bulk database update/insert from CSV file and Update Oracle database from CSV post. But I still confuse how to deal with csv file which related to multiple tables.
Can anyone give me a better solution. Thanks.
1 Answer 1
If there are several rows with a consistent number of columns in the CSV, then
CREATE TABLE
with lots of columnsLOAD DATA
into it.- Do "unpivot" code to rearrange it into the 'real' tables you want.
If there is only one row in your CSV, then write code in your favorite programming language to break it apart.
Not sure what to do if you have an inconsistent number of columns.
You can use INSERT IGNORE
and/or IODKU` to deal with duplicates.
-
1CREATE TABLE with lots of columns It is possible that the values pairs max amount cannot be predicted. I'd recommend to create 2-fields table -
title
field and the whole slack field, and load data into it with dividing to title and slack data using UDV. Then unpivot it by SP or by a query using generated numbers table (in CTE or subquery) where max number is not less that max pairs amount.Akina– Akina2019年06月10日 05:31:41 +00:00Commented Jun 10, 2019 at 5:31 -
1Yeah, one row will has inconsistent number of columns, I mean one row have one
book
record and severalscene
record.hstk– hstk2019年06月10日 15:10:46 +00:00Commented Jun 10, 2019 at 15:10