I am working on a large MySQL database and I need to improve the performance of INSERT in a specific table. It contains about 200 million lines, and its structure is as follows:
(small premise: I'm not a database expert, so the code I wrote may be based on the wrong basics. Please help me sort out my mistakes :))
CREATE TABLE IF NOT EXISTS items ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(200) NOT NULL, key VARCHAR(10) NOT NULL, busy TINYINT(1) NOT NULL DEFAULT 1, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id, name), UNIQUE KEY name_key_unique_key (name, key), INDEX name_index (name) ) ENGINE=MyISAM PARTITION BY LINEAR KEY(name) PARTITIONS 25;
Every day I get a lot of csv files in which each row consists of a pair of "name; key", so I have to parse these files (adding the created_at and updated_at values for each row) and paste the values into my table. In this case, the combination of "name" and "key" MUST be UNIQUE, so I implemented the insert procedure as follows:
CREATE TEMPORARY TABLE temp_items ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(200) NOT NULL, key VARCHAR(10) NOT NULL, busy TINYINT(1) NOT NULL DEFAULT 1, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM; LOAD DATA LOCAL INFILE 'file_to_process.csv' INTO TABLE temp_items FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' (name, key, created_at, updated_at); INSERT INTO items (name, key, busy, created_at, updated_at) ( SELECT temp_items.name, temp_items.key, temp_items.busy, temp_items.created_at, temp_items.updated_at FROM temp_items ) ON DUPLICATE KEY UPDATE busy=1, updated_at=NOW(); DROP TEMPORARY TABLE temp_items;
The code just shown allows me to achieve my goal, but it takes about 48 hours to complete the execution, and this is a problem. I think this low performance is caused by the fact that the script has to check a very large table (200 million rows) and for each insert that the pair "name; key" is unique.
How can I improve the performance of my script?
Thanks to everyone in advance.