mariadb - Replace Into Table in MySQL InnoDB extremely slow -
using mysql (mariadb exact though). have following script needs run every other day update database, it's unbearably slow. each table updated takes hours run. it's shell
script:
cmd_mysql="${mysql_dir}mysql --local-infile=1 --default-character-set=utf8 --protocol=${mysql_protocol} --port=${mysql_port} --user=${mysql_user} --pass=${mysql_pass} --host=${mysql_host} --database=${mysql_db}" ### update mysql data ### ## table name lowercase tablename=`echo $file | tr "[[:upper:]]" "[[:lower:]]"` echo "uploading ($file) ($mysql_db.$tablename) replace option..." ## let's try replace option $cmd_mysql --execute="load data local infile '$file.txt' replace table $tablename character set utf8 fields terminated '|' ignore 1 lines;" ## need erase records, not updated today echo "erasing old records ($tablename)..." $cmd_mysql --execute="delete $tablename datediff(timestamp, now()) < 0;"
you may safely ignore variables set somewhere else in file. $file
txt
file delimited |
. each row represents 1 record, example:
airportid|airportcode|airportname|latitude|longitude|maincityid|countrycode 6024358|dme|moscow, russia (dme-domodedovo intl.)|55.414495|37.899907|2395|ru 6024360|dmm|dammam, saudi arabia (dmm-king fahd intl.)|26.468075|49.796824|180543|sa
the script runs on existing database, old records found. check it's last updated date, , performs replace into
, takes 8 hours 100mb txt
file.
how can improve speed?
this issue affects versions of innodb (4.1+). replacing duplicates in innodb slow. 1 place myisam superior. myisam took 0.05 seconds.
the reason thatthe duplicate key error handling in innodb relies on undo log , statement rollback: 1. lock record. 2. write update_undo log delete-marking record. 3. write insert_undo log inserting record. 4. attempt insert new record. 5. notice duplicate. 6. roll undo log written in steps 3 , 2.
this issue in mysql has been fixed & verified in version 5.7: https://bugs.mysql.com/bug.php?id=71507
they planning detect duplicate @ step 2. avoid rollback in case.
here 3 suggestions:
- upgrade v 5.7
- use myisam if applicable
- use queries such "insert ... on duplicate key update ..."
Comments
Post a Comment