Comments on: REPLACE INTO: think twice https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice Blog by Shlomi Noach Tue, 01 May 2018 05:39:00 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: Shlomi Noach https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-437007 Tue, 01 May 2018 05:39:00 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-437007 @SacTiw correct.

]]>
By: SacTiw https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-436559 Mon, 30 Apr 2018 07:21:00 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-436559 Quick question:
REPLACE INTO will be an atomic operation right? That is, for a row “delete and insert” will executed as a part of a transaction, right?

]]>
By: That Oz Guy https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-425505 Wed, 14 Mar 2018 04:59:00 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-425505 I’ve just hit this too – I was getting over 100ms to execute the REPLACE INTO and using a stored procedure with the obvious “SELECT IFNULL(pk) INTO… IF found THEN UPDATE … ELSE INSERT …” was more than 10x faster. If you do use it and speed matters, measure it.

]]>
By: parminder Singh https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-222758 Wed, 06 Nov 2013 23:24:36 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-222758 REPLACE INTO is very good i often use this But the problem is that if you have used foreign key constraints On delete cascade than this will be big headache for you , actually i have already faced this problem , That’s why i sharing with you guys, coz may be it will be helpful for some body

]]>
By: replace into | Brinsmead Data Services https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-82885 Tue, 10 Apr 2012 00:03:46 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-82885 […] had been using mysql’s replace into syntax until I read this article https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice which makes a lot of […]

]]>
By: Sunset https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-70190 Fri, 17 Feb 2012 08:23:15 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-70190 Thanks guys, yesterday I spent 3 hours trying to get the best method of INSERT INTO/REPLACE INTO working for my site tweet “approver” where we scan for tweets and manually approve the good ones that can be displayed on our site.

I eventually settled for a bit of a mix of on duplicate col=VALUES(col) and col2=col2 as before we save the tweets we do some preg_replace stuff on it so no need to update it with the raw tweet again:

$sql = “INSERT INTO tweets (id, tweet, date, who, approved)
VALUES (\”$twitter_tweet_id\”, \”{$this->CI->db->escape($tweet_val->title)}\”, \”{$this->CI->db->escape($tweet_val->pubDate)}\”, \”{$this->CI->db->escape($tweet_val->author)}\”, ‘no’)
ON DUPLICATE KEY UPDATE id=VALUES(id), tweet=tweet, date=VALUES(date), who=who, approved=approved”;

I vote for UPSERT, too though; would have been much easier.

Cheers,

Kevin

]]>
By: rsandwick3 https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-62082 Wed, 07 Dec 2011 21:22:40 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-62082 One of the most useful features of ON DUPLICATE KEY UPDATE is that not all columns need to be specified in the UPDATE clause.

This allows adding new rows which either are fully qualified or use some or all column defaults while only modifying the desired subset of columns when the key is already present.

REPLACE INTO fails in this respect — once the row is deleted, only the values in the new definition will be populated, so you’d better have somehow managed to define everything you don’t want unset/defaulted for existing rows, in which case you’re basically reinserting the entire table anyway.

This could well be another source of the data loss @Tony happened upon.

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-57641 Fri, 04 Nov 2011 18:03:09 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-57641 @Tony,
Thanks for sharing this story. You are right about the overhead of foreign keys. This is to some extent due to the indexes on which the foreign keys rely, and, well, the deletion of child tables with “random loss of data”…

]]>
By: Tony https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-57626 Fri, 04 Nov 2011 14:53:42 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-57626 Newly onboard as the lead developer for my company, I have already run into this issue BIGTIME.

The previous developer(s) were quite fond of the “REPLACE INTO” syntax; the company’s web app was littered with it. Many problems plagued the system, the most common was the “random” loss of data.

As it turned out, the database has been in use for many years; some tables holding more than 1M records, as well as keyed with composites. Inevitably, someone would complain to the mother company’s I.T. dept about the app being slow. Mother company’s I.T. would just simply restart the MySQL instance in order to get performance back up. This occurred many times per day.

Of course, the sluggishness was due to the fact that the db engine was rebuilding the indexes for large tables. Restarting the server simply resulted in nightmarish situations.

Long story short: a good db programmer SHOULD understand the semantics of “REPLACE INTO” and carefully consider whether it is necessary.

One thing that was not mentioned in this article was the additional overhead of foreign-key relationships and how those tables are impacted.

]]>
By: Renowned Media https://shlomi-noach.github.io/blog/mysql/replace-into-think-twice/comment-page-1#comment-31911 Fri, 25 Feb 2011 18:08:37 +0000 https://shlomi-noach.github.io/blog/?p=397#comment-31911 I vote for MySQL implementing this UPSERT command… There are a lot of things in MySQL which don’t follow the ANSI SQL standard!

]]>