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DVHVHVSHFLDOO\ 0\64/VLQFHYHUVLRQ
&XUUHQWO\KHLVPRVWO\LQYROYHGZLWKWKHRQJRLQJGHYHORSPHQWRIIUDPHZRUNOHYHO IXQFWLRQDOLW\LQFOXGLQJFXVWRPL]DWLRQDQGH[WHQVLRQRIDQ250EDVHGSHUVLVWHQFHOD\HU +HLVLQYROYHGLQGLIIHUHQWRSHQVRXUFHSURMHFWVVXFKDV)LQG%XJV(FOLSVHDQG&KHFNVW\OH DQGLQIUHTXHQWO\EORJVDERXW-DYD0\64/:LQGRZV/LQX[DQGRWKHULQVDQLWLHVDW http://www.danielschneller.com tŚĞŶ/ĮƌƐƚǁĂƐĂƐŬĞĚďLJWĂĐŬƚWƵďůŝƐŚŝŶŐǁŚĞƚŚĞƌ/ǁŽƵůĚďĞŝŶƚĞƌĞƐƚĞĚŝŶ ǁƌŝƟŶŐĂŬĂďŽƵƚDLJ^Y>ŽŶŚƌŝƐƚŵĂƐǀĞϮϬϬϴůŝƩůĞĚŝĚ/ŬŶŽǁŚŽǁŵƵĐŚ ǁŽƌŬ͕ƐƚƌĞƐƐ͕ďƵƚĂůƐŽǁŚĂƚĂůŽƚŽĨĨƵŶ/ǁĂƐŚĞĂĚĞĚĨŽƌ͘ EŽǁ͕ƚŚĂƚƚŚĞŬŝƐĮŶĂůůLJĚŽŶĞ/ǁŽƵůĚůŝŬĞƚŽƚŚĂŶŬƚŚŽƐĞƉĞŽƉůĞǁŝƚŚŽƵƚ ǁŚŽŵŐĞƫŶŐŝƚĚŽŶĞǁŽƵůĚŚĂǀĞďĞĞŶŝŵƉŽƐƐŝďůĞ͘ &ŝƌƐƚŽĨĂůů͕/ΖĚůŝŬĞƚŽƚŚĂŶŬhĚŽĨŽƌĂŐƌĞĞŝŶŐƚŽďĞŵLJĐŽͲĂƵƚŚŽƌ͘tŝƚŚŽƵƚŚŝŵ͕ ƚŚŝƐǁŚŽůĞƚŚŝŶŐǁŽƵůĚŚĂǀĞƚĂŬĞŶĂůŽƚůŽŶŐĞƌĂŶĚǁŽƵůĚŚĂǀĞďĞĞŶŶŽƚŚĂůĨĂƐ ƵƐĞĨƵůĂƐ/ďĞůŝĞǀĞŝƚŚĂƐƚƵƌŶĞĚŽƵƚŶŽǁ͘ /ǁŽƵůĚĂůƐŽůŝŬĞƚŽƚŚĂŶŬƚŚĞƚĞĂŵĂƚWĂĐŬƚWƵďůŝƐŚŝŶŐͶŵŽƐƚŝŵƉŽƌƚĂŶƚůLJ ĨŽƌŶŽƟĐŝŶŐĂŶĚƌĞĂĚŝŶŐŵLJďůŽŐ͕ĐŽŶƐĞƋƵĞŶƚůLJĐŽŶƚĂĐƟŶŐŵĞƚŽŐĞƚƚŚĞ ǁŚŽůĞƚŚŝŶŐƐƚĂƌƚĞĚͶďƵƚĂůƐŽĨŽƌƚĂŬŝŶŐĐĂƌĞŽĨƐĐŚĞĚƵůĞƐ͕ƉƌŽǀŝĚŝŶŐƐƵƉƉŽƌƚ͕ ŐƵŝĚĂŶĐĞĂŶĚĨĞĞĚďĂĐŬ͕ĂŶĚŬĞĞƉŝŶŐƵƐŽŶƚƌĂĐŬƚŚĞǁŚŽůĞǁĂLJ͘ >ĂƐƚ͕ďƵƚďLJŶŽŵĞĂŶƐůĞĂƐƚ͕/ǁĂŶƚƚŽƚŚĂŶŬ:ĞŶŶLJͶĨŽƌĞŶĐŽƵƌĂŐŝŶŐŵĞƚŽ ǁƌŝƚĞĂŬŝŶƚŚĞĮƌƐƚƉůĂĐĞ͕ĂŶĚƚŚĞŶŵĂŬŝŶŐƐƵƌĞ/ŶĞǀĞƌƌĂŶŽƵƚŽĨƚĞĂ͕ ĐŽŽŬŝĞƐ͕ŽƌŵŽƟǀĂƟŽŶŽŶƚŚĞĐŽƵŶƚůĞƐƐĞǀĞŶŝŶŐƐ/ƐƉĞŶƚƐŝƫŶŐŝŶĨƌŽŶƚŽĨƚŚĞ ŬĞLJďŽĂƌĚŝŶƐƚĞĂĚŽĨǁŝƚŚŚĞƌ͘/ĚĞĚŝĐĂƚĞƚŚŝƐŬƚŽŚĞƌ͘
8GR6FKZHGWKDVRYHUWHQ\HDUVRIH[SHULHQFHLQWKH,7LQGXVWU\DVDSURIHVVLRQDO-DYDGH
YHORSHUDQGVRIWZDUHDUFKLWHFW+HLVKHDGRIWKH-DYDDUFKLWHFWXUHWHDPDQGGHSXW\KHDGRI WKH-DYDGHYHORSPHQWGHSDUWPHQWDWWKH,7VHUYLFHSURYLGHUIRU*HUPDQ\ VPDUNHWOHDGHULQ WKH'R,W START SLAVE;
:KHQZHZLVKWRGUDZ\RXUDWWHQWLRQWRDSDUWLFXODUSDUWRIDFRGHEORFNWKHUHOHYDQWOLQHVRU LWHPVDUHVHWLQEROG slave> SHOW SLAVE STATUS\G ************************** 1. row *************************** ... Slave_IO_Running: Yes Slave_SQL_Running: Yes ...
1HZWHUPVDQGLPSRUWDQWZRUGVDUHVKRZQLQEROG:RUGVWKDW\RXVHHRQWKHVFUHHQLQ PHQXVRUGLDORJER[HVIRUH[DPSOHDSSHDULQWKHWH[WOLNHWKLV -> ->
CREATE DATABASE repdb; USE repdb; CREATE TABLE tblA ( id INT(10) PRIMARY KEY NOT NULL, label VARCHAR(30) ) ENGINE=InnoDB; CREATE TABLE tblB ( name VARCHAR(20) PRIMARY KEY NOT NULL, age INT(3) ) ENGINE=InnoDB;
&KDSWHU master> INSERT INTO tblA VALUES -> (1, 'label 1'), -> (2, 'label 2'), -> (3, 'label 3'); master> INSERT INTO tblB VALUES -> ('Peter', 55), -> ('Paul', 43), -> ('Mary', 25);
,QVHUWLQJWKLVGDWDDOUHDG\FUHDWHVELQORJLQIRUPDWLRQ GRANT REPLICATION SLAVE -> ON *.* -> TO 'repblack'@'localhost' -> IDENTIFIED BY 'blackpass';
&RQÀJXUHWKHÀOWHUEODFNKROH LQVWDQFHZLWKDFRQÀJXUDWLRQÀOHQDPHGmy.black WKDW FRQWDLQVDWOHDVWWKHIROORZLQJ [client] port
= 3308
socket
= /home/ds/blacktest/black.sock
[mysqld_safe] socket
= /home/ds/blacktest/black.sock
[mysqld] log-slave-updates skip-innodb default-storage-engine=blackhole user pid-file socket port basedir
= = = = =
mysql /home/ds/blacktest/black.pid /home/ds/blacktest/black.sock 3308 /home/ds/blacktest
5HSOLFDWLRQ datadir tmpdir language bind-address server-id log-bin relay-log
= /home/ds/blacktest/data.black = /tmp = /home/ds/blacktest/share/mysql/english = 127.0.0.1 = 2 = /home/ds/blacktest/black-bin.log = /home/ds/blacktest/black-relay.log
1RWLFHWKDWDOORFFXUUHQFHVRIPDVWHUKDYHEHHQUHSODFHGZLWKEODFN 0RUHRYHUWKHserver-idVHWWLQJKDVEHHQFKDQJHGDQGWKH log-slave-updatesskip-innodbDQGdefault-storage-engine RSWLRQVKDYHEHHQDGGHG7KHVHFRQGRQHSUHYHQWVWKLVLQVWDQFHIURP FUHDWLQJibdataWDEOHVSDFHÀOHVZKLFKZRXOGQRWEHXVHGODWHUDQ\ZD\ 7KHODVWRQHVSHFLÀHVZKLFKVWRUDJHHQJLQHWRXVHZKHQDCREATETABLE VWDWHPHQWGRHVQRWH[SOLFLWO\VSHFLI\RQHRULIWKHVSHFLÀHGHQJLQHLVQRW DYDLODEOH:HZLOOFRPHEDFNWRWKLVVRRQ
0DNHVXUHWKLVLQVWDQFHEDVLFDOO\ZRUNVE\VWDUWLQJLWWKHVDPHZD\DVWKHPDVWHU EHIRUH\RXZLOOQRWVHHWKH,QQR'%PHVVDJHVKHUHRIFRXUVH ~/blacktest$ xterm -T BLACK -e bin/mysqld \ > --defaults-file=my.black \ > --console &
&UHDWHDVHWRIGXPSÀOHVIURPWKHPDVWHU WRVHWXSERWKWKHEODFNKROHÀOWHUDQGDQH[DPSOHVODYH7KHGHWDLOVRQZK\ZHQHHG WZRDQGLQZKLFKZD\VWKH\DUHGLIIHUHQWZLOOEHH[SODLQHGODWHU8VHWKHVHFRPPDQGV WRFUHDWHWKHÀOHVQHHGHG ~/blacktest$ bin/mysqldump -S master.sock -uroot \ > --master-data \ > --single-transaction \ > --no-create-info \ > --ignore-table=repdb.tblA \ > repdb > master_data.sql ~/blacktest$ bin/mysqldump -S master.sock -uroot \ > --no-data \ > repdb > master_struct.sql
&KDSWHU
&RQQHFWWRWKHÀOWHUVHUYHUFUHDWHWKHGDWDEDVHPDNHLWWKHGHIDXOWGDWDEDVHDQG ÀQDOO\LPSRUWWKHVWUXFWXUHLQIRUPDWLRQFUHDWHGEHIRUH ~/blacktest$ bin/mysql -uroot -S black.sock --prompt='black> ' black> CREATE DATABASE repdb; black> USE repdb; black> source master_black.sql;
$WWKLVSRLQWZHQRZKDYHWKHVWUXFWXUHRIWKHPDVWHUWUDQVIHUUHGWRWKHÀOWHUHQJLQH DGDSWHGWRXVHWKHEODFNKROHHQJLQHIRUDOOWKHWDEOHV
6HWXSWKHUHSOLFDWLRQEHWZHHQPDVWHUDQGÀOWHUHQJLQH7RGRVRZH QHHGWRNQRZWKHH[DFWSRVLWLRQIURPZKHUHWKHÀOWHUZLOOVWDUWUHSOLFDWLQJ([WUDFWWKLV LQIRUPDWLRQIURPWKHSUHYLRXVO\WDNHQGDWDGXPSOLNHWKLV
~/blacktest$ head -n 30 master_data.sql | grep 'CHANGE MASTER TO'
:ULWHGRZQWKDWLQIRUPDWLRQZHZLOOQHHGLWLQDPRPHQW
0RGLI\WKHmy.blackFRQÀJXUDWLRQÀOHWRFRQWDLQWKHIROORZLQJLQWKH[mysqld] VHFWLRQ replicate-ignore-table=repdb.tblA replicate-do-table=repdb.tblB
7KLVLVDYHU\VLPSOHÀOWHUVHWXSLQDUHDODSSOLFDWLRQVFHQDULRWKHVHUXOHVZLOO SUREDEO\EHPRUHFRPSOH[
5HVWDUWWKHÀOWHUHQJLQHWRDFWLYDWHWKHQHZFRQÀJXUDWLRQ ~/blacktest$ bin/mysqladmin -uroot -S black.sock shutdown ~/blacktest$ xterm -T BLACK -e bin/mysqld \ > --defaults-file=my.black \ > --console &
5HFRQQHFWWKHFOLHQWFRQQHFWHGWRWKHEODFNKROHHQJLQH7RGRWKLVMXVWLVVXHD SELECT1;FRPPDQG
([HFXWHWKHIROORZLQJFRPPDQGWRKRRNXSWKHÀOWHUWRWKHPDVWHU%HVXUHWRÀOOLQWKH YDOXHV\RXZURWHGRZQDPRPHQWDJRLQWKHVWDWHPHQW black> -> -> -> -> -> ->
CHANGE MASTER TO master_host='localhost', master_port=3307, master_user='repblack', master_password='blackpass', master_log_file='master-bin.000001', master_log_pos=1074;
5HSOLFDWLRQ 5HWULHYHLQIRUPDWLRQUHTXLUHGWRVHWXSWKHÀOWHUVODYHSRUWLRQ:ULWHGRZQWKHUHVXOWV RIWKHSHOWMASTERSTATUSFRPPDQGWKH\ZLOOEHQHHGHGODWHU black> FLUSH LOGS; black> SHOW MASTER STATUS; +------------------+----------+---+---+ | File | Position | … | … | +------------------+----------+---+---+ | black-bin.000003 | 98 | | | +------------------+----------+---+---+
6WDUWWKHVODYHWKUHDGRQWKHÀOWHUHQJLQHDQGYHULI\WKDWHYHU\WKLQJLVJRLQJZHOO black> START SLAVE; black> SHOW SLAVE STATUS \G ************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: localhost Master_User: repblack Master_Port: 3307 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 1074 Relay_Log_File: black-relay.000003 Relay_Log_Pos: 236 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Replicate_Do_Table: repdb.tblB Replicate_Ignore_Table: repdb.tblA ... Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1074 Relay_Log_Space: 236 ... Seconds_Behind_Master: 0
$WWKLVSRLQWZHKDYHVXFFHVVIXOO\HVWDEOLVKHGDUHSOLFDWLRQFRQQHFWLRQEHWZHHQWKH PDVWHUGDWDEDVHDQGWKHEODFNKROHEDVHGÀOWHULQVWDQFH
&KDSWHU
&KHFNWKDWQRWKLQJKDV\HWEHHQZULWWHQWR WKHÀOWHU VELQORJV%HFDXVHZHLVVXHGDFLUSHLOGSFRPPDQGRQWKHÀOWHULQVWDQFH WKHUHVKRXOGEHQRWKLQJLQWKHPRVWUHFHQWELQORJÀOH9HULI\WKLVDVIROORZV ~/blacktest$ bin/mysqlbinlog black-bin.000003
7HVWWKHÀOWHUVHWXSZLWKVRPHVWDWHPHQWVLVVXHGRQWKHPDVWHU master> UPDATE repdb.tblA -> SET label='modified label 3' -> WHERE id=3; master> INSERT INTO repdb.tblB -> VALUES ('John', 39);
:HZRXOGH[SHFWWRVHHWKHINSERTLQWKHELQORJÀOHRIWKHÀOWHULQVWDQFHEXWQRWWKH UPDATEVWDWHPHQWEHFDXVHLWPRGLÀHVtblAZKLFKLVWREHLJQRUHG
9HULI\WKDWWKHUXOHVZRUNDVH[SHFWHGE\KDYLQJDQRWKHUORRNDWWKHÀOWHU VELQORJV ~/blacktest$ bin/mysqlbinlog black-bin.000003
5HSOLFDWLRQ 7KLVORRNVSUHFLVHO\DVH[SHFWHG³WKHINSERTLVSUHVHQWWKHUPDATELVQRZKHUHWR EHVHHQ
6HWXSWKHFRQÀJXUDWLRQRIDVODYHXVLQJWKHVHVHWWLQJV [client] port socket
= 3309 = /home/ds/blacktest/slave.sock
[mysqld_safe] socket
= /home/ds/blacktest/slave.sock
[mysqld] user pid-file socket port basedir datadir tmpdir language
= = = = = = = =
bind-address
= 127.0.0.1
server-id relay-log
= 3 = /home/ds/blacktest/slave-relay.log
mysql /home/ds/blacktest/slave.pid /home/ds/blacktest/slave.sock 3309 /home/ds/blacktest /home/ds/blacktest/data.slave /tmp /home/ds/blacktest/share/mysql/english
1RWLFHWKDWDOORFFXUUHQFHVRIPDVWHUKDYHEHHQUHSODFHG ZLWKVODYH
$JDLQWKHserver-idVHWWLQJKDVEHHQFKDQJHGDQGWKHlog-slave-updates skip-innodbDQGdefault-storage-engineRSWLRQVWKDWZHUHSDUWRIWKHÀOWHU LQVWDQFH VFRQÀJXUDWLRQDUHQRWLQFOXGHG$OVRWKHlog-binSDUDPHWHUKDVEHHQ UHPRYHGEHFDXVHFKDQJHVRQWKHVODYHQHHGQRWEHUHFRUGHGVHSDUDWHO\ 6WDUWXSWKHVODYHHQJLQH ' slave> CREATE DATABASE repdb; slave> USE repdb;
$WWKLVSRLQWWKHVODYHLVVHWXSDQGKDVDQHPSW\repdbGDWDEDVH )LOOXSWKHVODYHGDWDEDVHZLWKWKHLQLWLDOVQDSVKRWRIWKHPDVWHU:HQHHGWRORDGWZR ÀOHVKHUH7KHGHWDLOVRIZK\DUHH[SODLQHGIXUWKHUGRZQLQWKH+RZLWZRUNVVHFWLRQ slave> source master_struct.sql; ... slave> source master_data.sql; ...
9HULI\WKDW\RXFDQÀQGWKHGDWDIURPWKHPDVWHURQWKHVODYHQRZE\GRLQJDSELECT * FROMÀUVWWDEOHrepdb.tblADQGWKHQrepdb.tblB
7KHÀUVWSELECTVKRZVQRUHFRUGVEHFDXVHtblAZDVH[FOXGHGIURPWKHGXPS 7DEOHtblBFRQWDLQVWKHWKUHHUHFRUGVZHLQVHUWHGRQWKHPDVWHU
&UHDWHDUHSOLFDWLRQXVHUDFFRXQWRQWKHÀOWHULQVWDQFHIRUWKHVODYHVWRXVH black> -> -> ->
GRANT REPLICATION SLAVE ON *.* TO 'repslave'@'localhost' IDENTIFIED BY 'slavepass';
&RQQHFWWKHVODYHWRWKHÀOWHUHQJLQH%HVXUHWRLQVHUWWKHFRUUHFW YDOXHVIRUMASTER_LOG_FILEDQGMASTER_LOG_POSLQWKHVWDWHPHQW7KRVHDUH WKHYDOXHV\RXZURWHGRZQZKHQ\RXLVVXHGWKHSHOWMASTERSTATUSFRPPDQGRQ WKHÀOWHUVHUYHUEHIRUHVWDUWLQJWKHUHSOLFDWLRQWKHUH
5HSOLFDWLRQ slave> CHANGE MASTER TO -> master_host='localhost', -> master_port=3308, -> master_user='repslave', -> master_password='slavepass', -> master_log_file='black-bin.000003', -> master_log_pos=98; Query OK, 0 rows affected (0.01 sec)
6WDUWWKHVODYHDQGYHULI\WKDWLWVWDUWVXSFRUUHFWO\ slave> START SLAVE slave> SHOW SLAVE STATUS \G ************************** 1. row *************************** Slave_IO_State: Waiting for master to send event ... Relay_Master_Log_File: black-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Seconds_Behind_Master: 0
$VVRRQDVWKHSUHYLRXVVWHSLVFRPSOHWHWKHUHSOLFDWLRQVKRXOGDOUHDG\KDYHXSGDWHG tblBRQWKHVODYHDQGLQVHUWHGWKHQHZ-RKQ UHFRUG9HULI\LWOLNHWKLV slave> SELECT * FROM repdb.tblB;
$SSDUHQWO\WKHUHSOLFDWLRQZRUNV "D:\MySQLBackup\MySQLDumpAllDatabases.sql.gz"
+RZLWZRUNV 7KHRQO\GLIIHUHQFHLQWKHDERYHFRPPDQGOLQHLQFRPSDULVRQWRWKHSUHYLRXVUHFLSHVEHVLGH WKHVOLJKWO\FKDQJHGWDUJHWÀOHQDPHWRUHÁHFWWKHFRPSUHVVHGFRQWHQW OLHVZLWKLQ WKH| gzip --fastSRUWLRQ7KLVUHGLUHFWVWKHRXWSXWRIWKHmysqldumpFRPPDQGWRWKH J]LSSURJUDPZKLFKFRPSUHVVHVGDWDRQWKHÁ\7KHFRPSUHVVHGGDWDVWUHDPLVWKHQZULWWHQ WRWKHJLYHQÀOHUHVXOWLQJLQDVLJQLÀFDQWO\UHGXFHGVL]H
7KHUH VPRUH« :KLOHWKHUHFLSHLWVHOILVQRWWRRFRPSOLFDWHGWKHUHDUHVRPHDVSHFWVWRFRQVLGHUZKLFKDUH GLVFXVVHGLQWKHIROORZLQJVHFWLRQV
%DFNLQJ8SDQG5HVWRULQJ0\64/'DWD
$FKLHYLQJEHWWHUFRPSUHVVLRQUDWLR $VDEDOOSDUNÀJXUHIRUWKHFRPSUHVVLRQUDWLRIRUW\SLFDOGDWDEDVHVWKHFRPSUHVVHGGXPS ZLOOSUREDEO\EHUHGXFHGWRRQHWKLUGRILWVXQFRPSUHVVHGVL]H7RDFKLHYHEHWWHUFRPS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WKHmysqlFRPPDQGOLQHFOLHQWLVQRWDEVROXWHO\QHFHVVDU\EXWSOHDVHQRWHWKDW \RXZLOOQRWEHDEOHWRLVVXHWKHsourceFRPPDQGIURPDQ\RWKHU64/FOLHQW7KLVLVEHFDXVH LWLVQRWDUHJXODU64/VWDWHPHQWEXWDNH\ZRUGUHFRJQL]HGE\WKHmysqlFRPPDQGOLQH FOLHQWLWVHOIgrep --invert-match "^INSERT INTO .op_detail. VALUES .*" D:\MySQLBackup\MySQLDump.sql | mysql -u admin4mysql -p"As,ysp4M"
7KLVFRPPDQGXVHVgrepWRÀOWHUDOOOLQHVLQFOXGLQJDQINSERT INTO 'op_detail'DW WKHVWDUWRIWKHOLQHOHDYLQJDOORWKHUOLQHVXQWRXFKHG$OORWKHUOLQHVDUHSLSHGWRWKHmysql FRPPDQGOLQHFOLHQWWKDWLPSRUWVWKHUHPDLQLQJGXPS͘ 3OHDVHQRWHWKDWLIGDWDKDVUHIHUHQFHVWRHQWULHVLQWKHH[FOXGHGWDEOHWKHLPSRUWRIWKH GXPSZLOOQRWIDLO,QWKLVFDVH\RXZRXOGHQGXSZLWKGDWDLQFRQVLVWHQFLHVWKDWPLJKWFDXVH HUURUVPXFKODWHUVRPDNHVXUHWRH[FOXGHRQO\WDEOHVWKDWDUHQRWUHIHUHQFHGE\IRUHLJQNH\V 2WKHUZLVH\RXKDYHWRVHDUFKIRUSRVVLEOHIRUHLJQNH\FRQVWUDLQWYLRODWLRQVPDQXDOO\6RPH LGHDVRQKRZWRDFKLHYHWKLVDUHSUHVHQWHGIRUH[DPSOHDWhttp://dev.mysql.com/doc/ refman/5.1/en/innodb-foreign-key-constraints.html
6HHDOVR f
'HÀQLQJDVSHFLÀFXVHUIRUDGPLQLVWUDWLYHWDVNV &UHDWLQJD64/GXPSRIDOOGDWDEDVHV
f
&UHDWLQJD64/GXPSRIVSHFLÀFGDWDEDVHV
f
&KDSWHU
3HUIRUPLQJDSRLQWLQWLPHUHFRYHU\XVLQJ WK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DNVWKHÀOHIRUW\SLFDOXVDJH 7RSUHYHQWWKLV\RXQHHGWRUHSODFH NULLHQWULHVE\DSSURSULDWHYDOXHV$VVXPLQJWKDWWKHWDEOHsample.table1FRQVLVWVRID QXPHULFFROXPQaDQGDFKDUDFWHUFROXPQb\RXVKRXOGXVHWKHIROORZLQJVWDWHPHQW mysql> SELECT IFNULL(a, 0), IFNULL(b, "NULL") FROM sample.table1 INTO OUTFILE 'C:/target.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n';
7KHGRZQVLGHWRWKLVDSSURDFKLVWKDW\RXKDYHWROLVWDOOÀHOGVLQZKLFKDNULLYDOXH PLJKWRFFXU
+DQGOLQJOLQHEUHDNV ,I\RXWU\WRH[SRUWYDOXHVWKDWFRQWDLQWKHVDPHFKDUDFWHUFRPELQDWLRQXVHGIRUOLQH WHUPLQDWLRQLQWKHSELECT … INTO OUTFILEVWDWHPHQW0\64/ZLOOWU\WRHVFDSHWKH FKDUDFWHUFRPELQDWLRQZLWKWKHFKDUDFWHUVGHÀQHGE\WKHESCAPED BYFODXVH+RZHYHU WKLVZLOOQRWDOZD\VZRUNWKHZD\LWLVLQWHQGHG
UNION SELECT @timestamp
">
UNION SELECT @rows
">
UNION SELECT @header
"> ">
UNION SELECT CONCAT('#', @counter := @counter + 1,
0DQDJLQJ'DWD ">
': ',
">
CONCAT_WS(' | ', c1, c2, c3),
">
' $')
">
FROM sample.table2
">
INTO OUTFILE '", file, "';");
->
SET @counter := 0;
->
PREPARE statement FROM @command;
->
EXECUTE statement;
->
END //
Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;
7KLVURXWLQHDOORZVSDVVLQJWKHÀOHQDPHZKHQFDOOLQJLW mysql> call sample.export_table2_FileAsParam("C:/data.out"); Query OK, 0 rows affected (0.00 sec)
,PSRUWLQJGDWDIURPDVLPSOH&69ÀOH $FRPPRQWDVNZKHQZRUNLQJZLWKGDWDEDVHVLVWRLPS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' INTO TABLE sample.table1 FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n'; Query OK, 20 rows affected (0.06 sec) Records: 20
Deleted: 0
Skipped: 0
Warnings: 0
+RZLWZRUNV 7KHLOAD DATA INFILEFRPPDQGZRUNVDQDORJRXVWRWKHSELECT … INTO OUTFILE FRPPDQGGLVFXVVHGLQWKHSUHYLRXVUHFLSHVEXWDVDPHDQVIRULPSRUWLQJGDWDUDWKHUWKDQ H[SRUWLQJ7KHIRUPDWRSWLRQVDYDLODEOHIRUERWKFRPPDQGVDUHLGHQWLFDOVR\RXFDQW\SLFDOO\ LPSRUWGDWDH[SRUWHGE\DSELECT … INTO OUTFILEVWDWHPHQWXVLQJDLOAD DATA INFILEFRPPDQGZLWKWKHVDPHIRUPDWRSWLRQV $VPRVWÀOHVFRQVLVWRIOLQHVWHUPLQDWHGE\DVHTXHQFHRIDFDUULDJHUHWXUQDQGDOLQHIHHG FKDUDFWHUZHXVHWKHLINES TERMINATED BY '\r\n'RSWLRQ7KHFKRLFHRIWKHVHPLFRORQ FKDUDFWHU³DVDVHSDUDWRUIRUGLIIHUHQWÀHOGVRIHYHU\OLQHTERMINATED BY ';' ³LVPDLQO\ GXHWRWKHIDFWWKDW([FHOXVHVWKLVIRUPDW,I\RXKDSSHQWRUHFHLYH&69ÀOHVWKDWIRUH[DPSOH XVHDFRPPDLQVWHDG\RXKDYHWRDGMXVWWKLVDFFRUGLQJO\ 7KHWHUPFIELDS ENCLOSED BY '"'WHOOVWKHLPSRUWWRORRNIRUGRXEOHTXRWHVDWWKH VWDUWRIHYHU\ÀHOGLPSRUWHG,IWKHUHLVRQHWKHÀHOGLVFRQVLGHUHGWRHQGDWWKHQH[WGRXEOH TXRWH7REHDEOHWRKDYHGRXEOHTXRWHVLQVLGHDÀHOGYDOXHZHGHÀQHDQHVFDSHFKDUDFWHU ESCAPED BY '"' :LWKWKLVFRQVWHOODWLRQDVHTXHQFHRIWZRGRX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OHDWLPHVWDPSDGHVFULSWLRQDQGWKHQXPEHURIURZV DKHDGHUOLQHZLWKWKHQDPHRIWKH FROXPQVDQGVXEVHTXHQWO\WKHURZVZLWKWKHDFWXDOGDWDWRLPSRUW(DFKGDWDURZVWDUWVZLWK DKDVKFKDUDFWHU# WKHOLQHQXPEHUDFRORQDQGDVSDFH7KHGDWDYDOXHVWKDWIROORZWKH URZQXPEHUDUHVHSDUDWHGE\DSLSH| FKDUDFWHUDQGWKHURZFORVHVZLWKDGROODUVLJQ$
*HWWLQJUHDG\ $JDLQWKHDFFRXQWXVHGLQWKHUHFLSHQHHGVWKHFILESULYLOHJHEHVLGHVWKHINSERT SHUPLVVLRQIRUWKHWDEOHWKHGDWDVKRXOGEHLPSRUWHGLQWR :LWKD64/FOLHQWDÀOHZLWK WKHDSSURSULDWHIRUPDWDQGDWDEOHDVWKHLPSRUWWDUJHWZHDUHUHDG\WRJR$VLQSUHYLRXV UHFLSHVZHXVHsample_installDVWKHDFFRXQWQDPHC:/source.txtDVWKHVRXUFHÀOH DQGsample.table2FRQVLVWLQJRIWKUHHFROXPQVc1c2DQGc3 DVWKHWDUJHWWDEOH:H DVVXPHWKHVRXUFHÀOHWRKDYHWKHIROORZLQJFRQWHQW Filename: C:/source.txt Description: This is a file for test import to sample.table2, columns c1, c2, and c3 2009-06-14 13:25:05 Row count: 3 #Row Nr: Column c1 | Column c2 | Column c3 $ #1: 209 | Some text in my test data | Some more text $ #2: 308 | Next test text for testing | Text to test $ #3: 406 | "A water | pipe" | Really? $
+RZWRGRLW &RQQHFW\RXUIDYRULWHFOLHQWIRUH[DPSOHWKHmysqlFRPPDQGOLQHFOLHQW WR\RXU 0\64/VHUYHUXVLQJWKHsample_installDFFRXQW
&KDSWHU
([HFXWHWKHIROORZLQJ64/FRPPDQG mysql> LOAD DATA INFILE "C:/source.txt" ->
INTO TABLE sample.table2
->
FIELDS TERMINATED BY ' | '
->
OPTIONALLY ENCLOSED BY '"'
->
LINES STARTING BY ':'
->
TERMINATED BY '\r\n'
->
IGNORE 5 LINES
->
SET c3=TRIM(TRAILING ' $' FROM c3);
Query OK, 3 rows affected (0.05 sec) Records: 3
Deleted: 0
Skipped: 0
Warnings: 0
+RZLWZRUNV /HWXVGLVVHFWWKHDERYHVWDWHPHQWE\KDYLQJDORRNDWWKHVRXUFHÀOHÀUVWRIDOOZHZDQW WRLPSRUWGDWDIURPWKHÀOHC:\source.txtLQWRWKHWDEOHsample.table2ZKLFKLV UHSUHVHQWHGE\WKHÀUVWWZROLQHVLOAD DATA INFILE … INTO TABLE … $WWKHWRSRIWKHÀOHZHKDYHÀYHOLQHVWKHLQLWLDOIRXUOLQHVZLWKLQIRUPDWLRQDERXWWKHÀOHSOXV WKHKHDGHU WKDWVKRXOGQRWEHLPSRUWHGLQWRWKHWDUJHWWDEOH7RDFKLHYHWKLVWKHIGNORE 5 linesRSWLRQLVDGGHG 7KHUHPDLQLQJOLQHVDUHSUHÀ[HGZLWKDKDVKFKDUDFWHUWKHURZQXPEHUDQGDFRORQ7KLVSDUW RIHYHU\OLQHKDVWREHLJQRUHGZKLFKLVZKDWWKHLINES STARTING BY ':'RSWLRQGRHVLW WHOOV0\64/WRLJQRUHWKHÀUVWFRORQRIWKHOLQHDQGDQ\FKDUDFWHUEHIRUHLW%\GRLQJWKLVWKH URZQXPEHUSUHÀ[LVVNLSSHG $IWHUWKHSUHÀ[WKHOLQHVFRQWDLQWKHDFWXDOYDOXHVVHSDUDWHGE\SLSHFKDUDFWHUV7KHFIELDS TERMINATED BY ' | 'RSWLRQWHOOV0\64/KRZWRLGHQWLI\DÀHOGVHSDUDWRU:LWKWKH DGGLWLRQDOVHWWLQJOPTIONALLY ENCLOSED BY '"'WKHYDOXHLWVHOIPLJKWFRQWDLQWKLVÀHOG VHSDUDWRUVHTXHQFH³LIWKHZKROHYDOXHLVHQFORVHGE\GRXEOHTXRWHVWKLVLVWKHFDVHLQWKH ODVWURZRIWKHVDPSOHÀOH $WWKLVSRLQWWKHU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DOGDWDEDVHFRQWHQWEHIRUHKDQG" INSERT INTO sample.config VALUES ("maxPriceDiscount", "25%") ON DUPLICATE KEY UPDATE value='25%'; Query OK, 1 row affected (0.05 sec)
+RZLWZRUNV 7KLVFRPPDQGLVHDVLO\H[SODLQHGEHFDXVHLWVLPSO\GRHVZKDWLWVD\VLWLQVHUWVDQHZURZLQ WKHWDEOHXVLQJWKHJLYHQYDOXHVDVORQJDVWKLVGRHVQRWFDXVHDGXSOLFDWHHQWU\LQHLWKHUWKH SULPDU\NH\RUDQRWKHUXQLTXHLQGH[,IDGXSOLFDWHUHFRUGH[LVWVWKHH[LVWLQJURZLVXSGDWHG DFFRUGLQJWRWKHFODXVHVGHÀQHGDIWHUON DUPLICATE KEY UPDATE :KLOHLWLVVRPHWLPHVWHGLRXVWRHQWHUVRPHRIWKHGDWDDQGFROXPQVWZRWLPHVRQFHIRUWKH INSERTDQGDVHFRQGWLPHIRUWKHUPDATE WKLVVWDWHPHQWDOORZVIRUDORWRIÁH[LELOLW\ZKHQLW FRPHVWRWKHPDQLSXODWLRQRISRWHQWLDOO\H[LVWLQJGDWD 3OHDVHQRWHWKDWZKHQH[HFXWLQJWKHDERYHVWDWHPHQWWKHUHVXOWGLIIHUVVOLJKWO\ZLWKUHVSHFW WRWKHQXPEHURIDIIHFWHGURZVGHSHQGLQJRQWKHDFWXDOGDWDSUHVHQWLQWKHGDWDEDVH:KHQ WKHUHFRUGGRHVQRWH[LVW\HWLWLVLQVHUWHGZKLFKUHVXOWVLQRQHDIIHFWHGURZ%XWLIWKHUHFRUG LVXSGDWHGUDWKHUWKDQLQVHUWHGLWUHSRUWVWZRDIIHFWHGURZVLQVWHDGHYHQLIRQO\RQHURZ JHWVXSGDWHG
7KHUH VPRUH 7KHINSERT INTO … ON DUPLICATE UPDATEFRQVWUXFWGRHVQRWZRUNZKHQWKHUHLV QRUNIQUERUPRIMARY KEYGHÀQHGRQWKHWDUJHWWDEOH,I\RXKDYHWRSURYLGHWKHVDPH VHPDQWLFVZLWKRXWKDYLQJDSSURSULDWHNH\GHÀQLWLRQVLQSODFHLWLVUHFRPPHQGHGWRXVHWKH WHFKQLTXHVGLVFXVVHGLQWKHQH[WUHFLSH
6HHDOVR f
,QVHUWLQJGDWDEDVHGRQH[LVWLQJGDWDEDVHFRQWHQW
&KDSWHU
,QVHUWLQJGDWDEDVHGRQH[LVWLQJGDWDEDVH FRQWHQW ,QWKHSUHYLRXVUHFLSH,QVHUWLQJQHZGDWDDQGXSGDWLQJGDWDLILWDOUHDG\H[LVWVZHGLVFXVVHG DPHWKRGWRHLWKHULQVHUWRUXSGDWHUHFRUGVGHSHQGLQJRQZKHWKHUWKHUHFRUGVDOUHDG\H[LVWLQ WKHGDWDEDVH$VLPLODUSUREOHPDULVHVZKHQ\RXQHHGWRLQVHUWGDWDWR\RXUGDWDEDVHEXWWKH GDWDWRLQVHUWGHSHQGVRQWKHGDWDLQ\RXUGDWDEDVH $VDQH[DPSOHFRQVLGHUDVLWXDWLRQLQZKLFK\RXQHHGWRLQVHUWDUHFRUGZLWKDFHUWDLQ PHVVDJHLQWRDWDEOHlogMsgsEXWWKHPHVVDJHLWVHOIVKRXOGEHGLIIHUHQWGHSHQGLQJRQWKH FXUUHQWV\VWHPODQJXDJHWKDWLVVWRUHGLQDFRQÀJXUDWLRQWDEOHconfig ,WLVIDLUO\HDV\WRDFKLHYHDVLPLODUEHKDYLRUIRUDQUPDATEVWDWHPHQWEHFDXVHWKLVVXSSRUWVD WHEREFODXVHWKDWFDQEHXVHGWRRQO\SHUIRUPDQXSGDWHLIDFHUWDLQSUHFRQGLWLRQLVPHW UPDATE logMsgs SET message= CONCAT('Last update: ', NOW()) WHERE EXISTS (SELECT value FROM config WHERE name='lang' AND value = 'en'); UPDATE logMsgs SET message= CONCAT('Letztes Update: ', NOW()) WHERE EXISTS (SELECT value FROM config WHERE name='lang' AND value = 'de'); UPDATE logMsgs SET message= CONCAT('Actualisation derniere: ', NOW()) WHERE EXISTS (SELECT value FROM config WHERE name='lang' AND value = 'fr');
8QIRUWXQDWHO\WKLVDSSURDFKLVQRWDSSOLFDEOHWRINSERTFRPPDQGVDVWKHVHGRQRWVXSSRUW DWHEREFODXVH'HVSLWHWKLVPLVVLQJRSWLRQWKHIROORZLQJUHFLSHGHVFULEHVDPHWKRGWRPDNH INSERTVWDWHPHQWVH[HFXWHRQO\LIDQDSSURSULDWHSUHFRQGLWLRQLQWKHGDWDEDVHLVPHW
*HWWLQJUHDG\ $VEHIRUHZHDVVXPHDGDWDEDVHD64/FOLHQWmysql DQGD0\64/XVHUZLWKVXIÀFLHQW SULYLOHJHVINSERTDQGSELECTLQWKLVFDVH $GGLWLRQDOO\ZHQHHGDWDEOHWRLQVHUWGDWD LQWRKHUHlogMsgs DQGDFRQÀJXUDWLRQWDEOHconfigSOHDVHUHIHUWRWKHSUHYLRXVUHFLSH IRUGHWDLOV
0DQDJLQJ'DWD
+RZWRGRLW &RQQHFWWR\RXUGDWDEDVHXVLQJ\RXU64/FOLHQW ([HFXWHWKHIROORZLQJ64/FRPPDQGV mysql> INSERT INTO sample.logMsgs(message) ->
SELECT CONCAT('Last update: ', NOW())
->
FROM sample.config WHERE name='lang' AND value='en';
Query OK, 0 rows affected (0.00 sec) Records: 0
Duplicates: 0
Warnings: 0
mysql> INSERT INTO sample.logMsgs(message) ->
SELECT CONCAT('Letztes Update: ', NOW())
->
FROM sample.config WHERE name='lang' AND value='de';
Query OK, 1 row affected (0.05 sec) Records: 1
Duplicates: 0
Warnings: 0
mysql> INSERT INTO sample.logMsgs(message) ->
SELECT CONCAT('Dernière actualisation: ', NOW())
->
FROM sample.config WHERE name='lang' AND value='fr';
Query OK, 0 rows affected (0.00 sec) Records: 0
Duplicates: 0
Warnings: 0
+RZLWZRUNV 2XUJRDOLVWRKDYHDQINSERTVWDWHPHQWWDNHLQWRDFFRXQWWKHSUHVHQWODQJXDJHVWRUHGLQ WKHGDWDEDVH7KHWULFNWRGRVRLVWRXVHDSELECTVWDWHPHQWDV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QVWDOOHGZLWKDQ,QQR'%VWRUDJHHQJLQH HQDEOHG7RGHOHWHDOOURZVRIWKHODUJHVWWDEOHemployees.salariesLQDTXLFNZD\SOHDVH UHDGRQ
&KDSWHU
+RZWRGRLW &RQQHFWWR\RXUGDWDEDVH (QWHUWKHIROORZLQJ64/FRPPDQG mysql> TRUNCATE TABLE employees.salaries; Query OK, 0 rows affected (0.16 sec)
+RZLWZRUNV 7KHTRUNCATE TABLEFRPPDQGLVDUDWKHUIDVWZD\RIGHOHWLQJDOOGDWDIURPDWDEOH)RU WDEOHVWKDWDUHQRWUHIHUHQFHGE\)RUHLJQNH\FRQVWUDLQWVPRUHRQWKDWODWHU WKHFRPPDQG EDVLFDOO\GURSVWKHWDEOHWHPSRUDULO\DQGUHFUHDWHVWKHWDEOHZLWKWKHVDPHVWUXFWXUHDV EHIRUH7KLVRSHUDWLRQKDVEDVLFDOO\DFRQVWDQWWLPHFKDUDFWHULVWLF³WKHDPRXQWRIGDWDVWRUHG LQVLGHWKHWDEOHGRHVQRWKDYHDQ\HIIHFWLQWKHWLPHQHHGHGIRUWKHTRUNCATEFRPPDQG %HIRUH0\64/WKHTRUNCATE TABLEVWDWHPHQWIRU,QQR'%WDEOHV ZDVDOZD\VHTXLYDOHQWWRDDELETEVWDWHPHQWUHJDUGOHVVRIZKHWKHU)RUHLJQ NH\FRQVWUDLQWVH[LVWRUQRW7RWDNHDGYDQWDJHRIWKHVSHHGLPSURYHPHQWV \RXKDYHWRXVH0\64/RUODWHU
,QFRPSDULVRQWRDFODVVLFDODELETE FROM employees.salaries;RSHUDWLRQWKH UHGXFWLRQLQWLPHQHHGHGLVVWULNLQJ 2SHUDWLRQ
7LPHQHHGHG
7581&$7(7$%/(
VHF
'(/(7(
PLQVHF
100
Time in seconds
80
60
40
20
0 TRUNCATETABLE
DELETE
0DQDJLQJ'DWD 7KHTRUNCATE TABLEFRPPDQGWDNHVRQO\DIUDFWLRQRIWKHWLPHQHHGHGIRUWKHDELETE +RZHYHUWKHUHDUHVRPHFDYHDWV )LUVWRIDOOWKHTRUNCATEFRPPDQGZLOORQO\KDYHWKHVSHHGDGYDQWDJHRQ,QQR'%WDEOHV LIWKHWDEOHLVQRWUHIHUHQFHGE\DQ\)RUHLJQNH\FRQVWUDLQWV%XWLIWKHWDEOHLVUHIHUHQFHG E\)RUHLJQNH\VWKHTRUNCATE TABLEFRPPDQGLVHTXLYDOHQWWRH[HFXWLQJDDELETE VWDWHPHQWZLWKQRWHEREFODXVHDOVRHOLPLQDWLQJDOOVSHHGGLIIHUHQFHV mysql> CREATE TABLE employees.salaries_referencer ( -> emp_no
INT,
-> from_date
DATE,
-> CONSTRAINT
salaries_fk
-> FOREIGN KEY (emp_no, from_date) -> REFERENCES
salaries (emp_no, from_date)
-> ON DELETE RESTRICT); Query OK, 0 rows affected (0.08 sec) mysql> TRUNCATE TABLE employees.salaries; Query OK, 0 rows affected (1 min 33.44 sec)
2SHUDWLRQ
7LPHQHHGHG
7581&$7(7$%/(ZLWKIRUHLJQNH\UHI
PLQVHF
'(/(7(
PLQVHF
100
Time in seconds
80 60 40 20 0 TRUNCATETABLE (w/FKreferences)
DELETE
)XUWKHUPRUHWKHTRUNCATEVWDWHPHQWUHTXLUHVWKHDROPSULYLOHJHEHIRUH0\64/ LWRQO\UHTXLUHVWKHDELETESHUPLVVLRQ ZKLFKIRUELGVXVHRIWKLVFRPPDQGIRUVRPHXVHUV
&KDSWHU
$QGÀQDOO\TRUNCATELVQRWDWUDQVDFWLRQVDIHFRPPDQG,I\RXH[HFXWHDTRUNCATE VWDWHPHQW\RXZLOOQRWEHDEOHWRSHUIRUPDUROOEDFNRQWKLVRSHUDWLRQDQ\PRUHDQGDQ\ RSHQRSHUDWLRQIURPWKHFXUUHQWWUDQVDFWLRQJHWVDXWRPDWLFDOO\FRPPLWWHGDVZHOO7KLV LVDFKDUDFWHULVWLFWKDWGLVTXDOLÀHVWKLVVWDWHPHQWIRUVLWXDWLRQVLQZKLFKWKHSRVVLELOLW\RI SHUIRUPLQJDUROOEDFNLVPDQGDWRU\\RXZLOOKDYHWRVWLFNZLWKWKHPXFKVORZHU DELETELQ WKHVHFDVHV
7KHUH VPRUH $VZHKDYHVHHQTRUNCATE TABLERQO\KDVSHUIRUPDQFHDGYDQWDJHVLIWKHUHLVQR)RUHLJQ NH\UHIHUHQFHWRWKHWDEOHWKDWLVWREHGHOHWHG+HUHZHZLOOGLVFXVVKRZWRXVHWKHVSHHG LPSURYHPHQWVHYHQLQFDVHRIH[LVWLQJUHIHUHQFHV
7HPSRUDULO\GLVDEOLQJ)RUHLJQNH\FRQVWUDLQWV 7RPDNHXVHRIWKHLQFUHDVHGVSHHGRITRUNCATE TABLEDOWKRXJKWKHWDUJHWWDEOHLVUHI HUHQFHGYLD)RUHLJQNH\V\RXFRXOGWHPSRUDULO\UHPRYHWKH)RUHLJQNH\FRQVWUDLQWVXVHWKH TRUNCATE TABLEFRPPDQGDQGUHHVWDEOLVKWKHUHIHUHQFHVDIWHUZDUGV8VLQJWKHDERYH H[DPSOHRIDWDEOHsalaries_referencerWKDWUHIHUHQFHVVDODULHV\RXFRXOGXVHWKH IROORZLQJVHTXHQFH mysql> ALTER TABLE employees.salaries_referencer -> DROP FOREIGN KEY salaries_fk; Query OK, 0 rows affected (0.19 sec) Records: 0
Duplicates: 0
Warnings: 0
mysql> TRUNCATE TABLE employees.salaries; Query OK, 0 rows affected (0.44 sec) mysql> ALTER TABLE employees.salaries_referencer -> ADD CONSTRAINT salaries_fk -> FOREIGN KEY (emp_no, from_date) -> REFERENCES salaries (emp_no, from_date) -> ON DELETE RESTRICT; Query OK, 0 rows affected (0.14 sec) Records: 0
Duplicates: 0
Warnings: 0
0DQDJLQJ'DWD :LWKWKLVVHTXHQFH\RXWHPSRUDULO\GLVDEOHWKH)RUHLJQNH\FRQVWUDLQWVWRKDYHTRUNCATE TABLEXVHWKHIDVWHUGHOHWLRQPHWKRG%HZDUHKRZHYHUWKDWWKLVPHWKRGPLJKWDOVR OHDG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GHOHWHDQGZKLFKQRWLWDOZD\VGHOHWHVDOOHQWULHV 7KHLQWXLWLYHVROXWLRQWRWKLVZRXOGEHWRXVHDQRUPDODELETEFRPPDQGZLWKDWHEREFODXVH WKDWRQO\PDWFKHVWKHUHFRUGVWRGHOHWH)RUODUJHWDEOHVWKLVPLJKWSURYHTXLWHDQH[SHQVLYH RSHUDWLRQLQWHUPVRIGXUDWLRQ ,QWKLVUHFLSHZHZLOOVKRZ\RXKRZWRTXLFNO\UHPRYHPRVWRI WKHGDWDIURPODUJHWDEOHVZKLOHSUHVHUYLQJVRPHRIWKHUHFRUGV
&KDSWHU
*HWWLQJUHDG\ :HDJDLQQHHGD0\64/VHUYHUXSDQGUXQQLQJDQGD64/FOLHQWOLNHmysql )RUWKLVUHFLSH ZHDOVRQHHGDXVHUDFFRXQWZLWKSELECTINSERTDELETEDROPDQGCREATESULYLOHJHV IRUWKHWDUJHWGDWDEDVHZHZLOOXVHWKHsample_installXVHUWKURXJKRXWWKLVVHFWLRQ :H ZLOOIXUWKHUPRUHXVHWKH(PSOR\HHVVDPSOHGDWDEDVHLQDQ,QQR'%FRQWH[W7KLVGDWDEDVH ZDVLQWURGXFHGLQWKHSUHYLRXVUHFLSHDQGLVDYDLODEOHIRUIUHHRQWKH0\64/ZHEVLWH:HZLOO XVHWKHODUJHVWWDEOHsalariesZLWKPRUHWKDQPLOOLRQUHFRUGV DVWKHWDEOHWRGHOHWH IURP,QRXUH[DPSOHZHZLOOGHOHWHDOOUHFRUGVKDYLQJDfrom_dateEHIRUHWKHWKUHVKROGRI '2002-01-01 00:00:00.0'
+RZWRGRLW &RQQHFWWRWKHGDWDEDVHXVLQJD64/FOLHQWDQGWKHsample_installDFFRXQW ([HFXWHWKHIROORZLQJFRPPDQGV mysql> use employees; Database changed mysql> CREATE TABLE salaries_part -> SELECT * FROM salaries -> WHERE from_date >= "2002-01-01 00:00:00.0"; Query OK, 140930 rows affected (11.47 sec)Records: 140930
Duplicates: 0
Warnings: 0
mysql> TRUNCATE TABLE salaries; Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO salaries SELECT * from salaries_part; Query OK, 140930 rows affected (4.63 sec) Records: 140930
Duplicates: 0
Warnings: 0
mysql> DROP TABLE salaries_part; Query OK, 0 rows affected (0.06 sec)
0DQDJLQJ'DWD
+RZLWZRUNV )RUVSHHGLQJXSWKHGHOHWLRQRIPRVWRIWKHGDWDIURPDODUJHWDEOHZHXWLOL]HWKHVSHHG DGYDQWDJHRIWKHTRUNCATE TABLEVWDWHPHQWRYHUDDELETEFRPPDQG,QGHWDLOWKHVWHSV DUHDVIROORZV 7KHLQLWLDOUSEVWDWHPHQWLVIRUFRQYHQLHQFHRQO\VRZHGRQRWKDYHWRJLYHWKHemployees SUHÀ[IRUHYHU\WDEOH :LWKWKHQH[WVWDWHPHQWCREATE TABLE … SELECT * FROM … ZHVLPSO\FRS\WKHGDWD WKDWVKRXOGQRWEHUHPRYHGWRDQHZO\FUHDWHGWDEOHsalaries_part %HFDUHIXOWRDYRLGHUURUVZKHQLQYHUWLQJFRQGLWLRQVWRGHOHWHDOOHQWULHV EHIRUHWLPH;\RXKDYHWRFRS\DOOUHFRUGVYDOXHVODWHURUHTXDOWR;,I \RXFRS\RQO\UHFRUGVODWHUWKDQ;DOOUHFRUGVH[DFWO\DWWLPH;ZRXOGJHW GHOHWHGDVZHOO
7KLVWDEOHWHPSRUDULO\KROGVWKHGDWDZKLOHZHGHOHWHDOOGDWDIURPWKHODUJHsalaries WDEOHXVLQJTRUNCATELQWKHQH[WVWHS$IWHUZDUGVZHVLPSO\FRS\WKHSDUWLDOGDWDIURPWKH salaries_partWDEOHEDFNLQWRWKHRULJLQDOQRZHPSWLHG salariesWDEOH:LWKWKHÀQDO VWHSZHVFUDSWKHsalaries_partWDEOHDJDLQDVLWLVQRWQHHGHGDQ\PRUH SHOW VARIABLES LIKE 'innodb_fast_shutdown';
/RJRIIIURP0\64/DQGVWRSWKH0\64/VHUYHU /RFDWHWKH0\64/FRQÀJXUDWLRQÀOHXVXDOO\FDOOHGmy.cnfRUmy.iniRQ:LQGRZV DQGRSHQLWLQDWH[WHGLWRU /RFDWHWKHIROORZLQJSDUDPHWHUVLQWKH[mysqld]VHFWLRQ\RXYDOXHVZLOOYDU\ RIFRXUVH [mysqld] ... innodb_log_group_home_dir=/var/lib/mysql/redolog innodb_log_file_size=32M innodb_log_buffer_size=64M innodb_log_files_in_group=2 ...
(GLWWKHDERYHFRQÀJXUDWLRQVHWWLQJVWRWKHLUQHZYDOXHV,I\RXUHTXLUHKHOSRQKRZWR ÀQGVXLWDEOHYDOXHVVHHWKH7KHUH VPRUHVHFWLRQRIWKLVUHFLSH
6DYHWKHFRQÀJXUDWLRQÀOH 1DYLJDWHWRWKHGLUHFWRU\FRQÀJXUHGIRUinnodb_log_group_home_dir,IWKHUHLV QRVXFKVHWWLQJLQ\RXUFRQÀJXUDWLRQÀOHQDYLJDWHWR0\64/ VGDWDGLUHFWRU\WKDWLV WKHQWDNHQDVWKHGHIDXOW
&RQÀJXULQJ0\64/ 0RYHWKHÀOHVZKRVHQDPHVVWDUWZLWKib_logfileWRDEDFNXSORFDWLRQ'RQRW FRS\WKHPWKH\PXVWEHUHPRYHGIURPWKHLURULJLQDOORFDWLRQ 5HVWDUWWKH0\64/VHUYHU 9HULI\WKDWQHZÀOHVDUHFUHDWHGDV\RXFRQÀJXUHGWKHP $ ls -l /var/lib/mysqld/redolog
,I\RXGRQRWVHHWKHQHZÀOHVDSSHDUDQGWKHVHUYHUGRHVQRWVWDUWXSFRUUHFWO\FKHFNWKH 0\64/HUURUORJIRUPHVVDJHV8VXDOO\WKHRQO\WKLQJWKDWFDQJRZURQJKHUHLVWKDW\RX HLWKHUPLVW\SHGWKHGLUHFWRU\QDPHRUGLGQRWDFWXDOO\UHPRYHWKHSUHYLRXVib_logfileÀOHV 7RUHVWRUHHYHU\WKLQJEDFNWRWKHRULJLQDOFRQÀJXUDWLRQUHVWRUH\RXUFRQÀJXUDWLRQÀOH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DOORZLQJRQO\RQHRSHUDWLRQWREHFDUULHGRXWDWDWLPH$VLGHHIIHFWRIWKLVLV WKDWRQFHDGDWDWUDQVIHULQHLWKHUGLUHFWLRQKDVVWDUWHGWKHUHLVQRZD\IRULWWREHLQWHUUXSWHG LQDFRQWUROOHGPDQQHU 7KHnet_read_timeoutFRQWUROVKRZORQJDSLHFHRILQIRUPDWLRQFDQEHVHQWIURPWKHFOLHQW WRWKHVHUYHUEHIRUHWKHFRQQHFWLRQLVDERUWHG7KLVLVXVXDOO\QRWDSUREOHP³WKHGHIDXOW VHWWLQJLVVHFRQGV8QGHUQRUHJXODUFLUFXPVWDQFHVZLOODFRPPXQLFDWLRQLQWKDWGLUHFWLRQ WDNHVRORQJ
&RQÀJXULQJ0\64/ 7KHnet_write_timeoutLVPRUHSUREOHPDWLFEHFDXVHIRUODUJHUHVXOWVHWVWKHGHIDXOW YDOXHRIVHFRQGVPLJKWEHWRRVKRUW7KLVLVHVSHFLDOO\WUXHIRUFOLHQWVWKDWIHWFKWKHUHVXOW LQVWUHDPLQJPRGHSRWHQWLDOO\SHUIRUPLQJWLPHFRQVXPLQJRSHUDWLRQVRQHDFKURZUHWULHYHG WKHUHE\PDNLQJWKHRYHUDOORSHUDWLRQWDNHORQJHUWKDQWKHWLPHRXW 7KHH[DFWYDOXHUHTXLUHGIRU\RXUVHWXSGHSHQGVRQKRZFOLHQWVIHWFKGDWDDQGZRUNZLWKLWVR \RXZLOOQHHGWRH[SHULPHQWDQGÀQGDVXLWDEOHYDOXH 2QHFDYHDWWRFRQVLGHUZLWKnet_write_timeoutLVWKDWLWPD\OHDGWRVHHPLQJO\UDQGRP IDLOXUHVRImysqldumpOLNHWKLV mysqldump: Error 2013: Lost connection to MySQL server during query when dumping table `tablename` at row: 935578
7KLVFDQKDSSHQLIWKHIROORZLQJFRQGLWLRQVDSSO\ f
net_write_timeoutLVVHWWRDORZYDOXH
f
max_allowed_packetLVVHWWRDODUJHYDOXH
'HSHQGLQJRQWKHVSHHGRIWKHQHWZRUNRYHUZKLFKmysqldumpKDVFRQQHFWHGDQGWKHVL]HRI WKHURZVEHLQJGXPSHGLWPD\EHQHFHVVDU\WRLQFUHDVHnet_write_timeoutPXFKKLJKHU DWOHDVWWRDVORQJDVLWWDNHVWRWUDQVPLWmax_allowed_packetE\WHVRYHUWKHQHWZRUNDQG ZULWHLWWRWKHRXWSXW mysqldumpLVDUHJXODUFOLHQWSURJUDPDQGVXEMHFWWRWKHnet_write_timeout VHWWLQJ:KHQWKHVHUYHUVHQGVURZVWREHGXPSHGWRmysqldumpLQFKXQNVRIXS WRmax_allowed_packetE\WHVGHSHQGLQJRQWKHQHWZRUNFRQQHFWLRQLQEHWZHHQ WKLVPLJKWWDNHORQJHUWKDQnet_write_timeoutDOORZVPDNLQJWKHVHUYHUFXWWKH FRQQHFWLRQHYHQWKRXJKQRWKLQJLVUHDOO\ZURQJ,QFUHDVLQJnet_write_timeout IRUWKHmysqldumpWRRO VVHVVLRQZRXOGUHPHG\WKLVEXWXQIRUWXQDWHO\DVRIWKH WLPHRIZULWLQJWKHUHLVQRVXFKVHWWLQJIRUmysqldump$ZRUNDURXQGLI\RXHQFRXQWHU WKLVSUREOHPLVWRWHPSRUDULO\LQFUHDVHWKHJOREDOVHUYHUnet_write_timeoutYDOXH $ mysql -uroot -e "SELECT @@GLOBAL.net_write_timeout AS oldvalue; SET GLOBAL net_write_timeout=600;" $P\VTOGXPS $ mysql -e "SET GLOBAL net_write_timeout=oldvalue;"
7KHÀUVWFRPPDQGZLOOGLVSOD\WKHFXUUHQWYDOXHIRUnet_write_timeoutDQGWKHQVHWLW WRPLQXWHV$IWHUWKDWWKHmysqldumpFDQWDNHSODFH)LQDOO\WKHROGYDOXHLVUHVHWMXVW PDNHVXUH\RXÀOOLQWKHFRUUHFWROGYDOXH 6HH0\64/%XJDWhttp://bugs.mysql.com/bug.php?id=46103IRU PRUHGHWDLOV
&KDSWHU
$GMXVWLQJWDEOHDQGGDWDEDVHQDPHOHW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