Rusty Razor Blade
Why have REPLACE INTO and INSERT … ON DUPLICATE KEY UPDATE?
REPLACE INTO will actually perform a delete and then an insert, while INSERT … ON DUPLCIATE KEY UPDATE will perform an update (as the name suggests). I would think the latter would be faster. I have not done any performance testing between the two, but it only seems logical the update would be faster than the delete/insert. Please correct me if I’m wrong.
Since the two statements both end up with the same result, I’m not sure yet what the benefit of REPLACE into it.
Yes, I realize both are MySQL additions, so please don’t bother telling me they’re not ANSI standard.
References:
4 Responses to Why have REPLACE INTO and INSERT … ON DUPLICATE KEY UPDATE?
Leave a Reply Cancel reply
Recent Comments
- Anil on MySQL Triggers Tutorial
- Ashish on MySQL Triggers Tutorial
- David on iCal Agenda
- jon on IP address geolocation SQL database
- pim on IP address geolocation SQL database
- jnns on Redis Wildcard Delete
- K.C. Murphy on iCal Agenda
- BA on Experts Exchange should be removed from Google search results
- Andrew on Executing multiple curl requests in parallel with PHP and curl_multi_exec
- Stu on Executing multiple curl requests in parallel with PHP and curl_multi_exec
Recent Posts
- New Project: Jester
- Open New Terminal Tip
- Installing MySQLdb on MacOS Lion
- Headless VM Server Using Ubuntu 11.10
- Get rid of Facebook’s Awful Ticker
- Api Tester now hosted on Github
- Trac .11 jQuery bug
- Multiple Filetypes in Vim
- Git Tip: Setting Up Your Remote Server
- Install issue pymongo on OSX (setuptools out of date)
Categories
- amazon (1)
- answerbag (6)
- apache (9)
- apple (8)
- awk (2)
- bbedit (2)
- c++ (3)
- chrome (2)
- cluster (1)
- cocoa (1)
- collective intelligence (1)
- curl (3)
- db2 (1)
- demand media (1)
- ebay (1)
- eclipse (4)
- erlang (13)
- facebook (1)
- fortran (1)
- gen_server (1)
- git (5)
- google (4)
- haddad (1)
- hdf5 (1)
- html (1)
- innodb (1)
- itunes (1)
- java (2)
- jester (1)
- kvm (1)
- launchbar (1)
- leex (1)
- letsgetnuts.com (1)
- libvirt (1)
- links (6)
- linux (27)
- lucene (1)
- mac (16)
- memcached (1)
- misconception (1)
- mobile (1)
- mono (1)
- mssql (1)
- munin (1)
- mysql (31)
- numpy (1)
- oracle (1)
- php (23)
- puppet (4)
- pyparsing (1)
- pytables (1)
- python (11)
- q&a (1)
- quicksilver (1)
- rant (6)
- readynas (1)
- redis (2)
- regex (1)
- replication (1)
- search (1)
- shitty code (1)
- solr (3)
- spaces (1)
- sshfs (1)
- stored procedure (1)
- svn (5)
- textmate (2)
- tips (22)
- trac (1)
- tutorial (4)
- ubuntu (3)
- Uncategorized (4)
- unix (1)
- vim (3)
- virtual box (6)
- vmware (1)
- weird (3)
- wikipedia (1)
- windows (1)
- xcode (1)








Purely hypothetical, but…
CREATE TABLE foo (
mykey INT PRIMARY KEY,
cola CHAR(1) DEFAULT ‘A’,
colb CHAR(2) DEFAULT ‘B’
);
INSERT INTO foo (mykey, cola, colb)
VALUES (1, ‘X’, ‘Y’), (2, ‘X’, ‘Y’);
INSERT INTO foo (mykey, cola) VALUES (1, ‘Z’) ON DUPLICATE KEY UPDATE cola=’Z';
REPLACE INTO foo (mykey, cola) VALUES (2, ‘Z’);
We should now have (1, ‘Z’, ‘Y’), (2, ‘Z’, ‘B’). Of course, this could still be done with UPDATE by appending colb=DEFAULT. The maintainability of that would decrease though. Not that relying on REPLACE to set fields back to their default values is very maintainable either.
Real world case? Nope, can’t think of one either.
REPLACE is dangerous with InnoDB and cascading DELETEs if something references that row. It will also change the auto_increment of the record.
ON DUPLICATE also allows for more freedom because, while you may opt to update with the values of the INSERT clause you are not tied down to that. You can use any kind of complex expression in order to achieve different results upon INSERT or UPDATE
FYI update is not always faster! sometimes a DELETE followed by an INSERT may actually be faster.
I use DUPLICATE very often, yet I have yet to find use for REPLACE in an environment that enforces referential integrity
Think you don’t have a primary key and want to replace a tuple which might exist (or not) in that table …
They’re not equivalent; ODKU allows you to specify exactly which fields get updated, where REPLACE wipes out all existing data and makes a new row. Sometimes you want this, for example when you have a timestamp column and don’t want to explicitly set it. Most queries can be written with either syntax, but in practice sometimes I find one or the other solves the problem better. I generally do not use REPLACE.