I want to update a table value on Mysql 5 but if the key does not exist create it.
The way I found to do it is by:
INSERT yyy ON DUPLICATE KEY UPDATE field;
The question is : is the format above less efficient than other ways to do it (As the insert will happen only once and update will happen very often)?
for example:
$result = UPDATE field;
if (num_rows_effected($result)==0) INSERT yyy
Furthermore: Is there a better way to do this in Mysql: for example a kind of:
UPDATE value IF NO SUCH ROW INSERT yyy;
Update: For those who suggested REPLACE, here is an extension to my question:
"Thanks! I need to increase a counter that is already in the table (if it exists). If not create a table row with value 1 for this column. How can I do update with this format (REPLACE)? "
There is a REPLACE also.
INSERT ON DUPLICATE KEY UPDATE will fire UPDATE triggers when it will stumble upon a duplicate key and won't violate FK's in case on UPDATE.
REPLACE will fire DELETE and INSERT triggers, and will violate FK's referencing the row being REPLACE'd.
If you don't have any triggers or FK's, then use INSERT ON DUPLICATE KEY UPDATE, it's most efficient.
You seem to be looking for this query:
INSERT
INTO table (key, counter)
VALUES (#key, 1)
ON DUPLICATE KEY UPDATE
counter = counter + 1
You cannot do this with REPLACE unless you have selected previous value of the counter before running the query.
P. S. REPLACE appeared in MySQL before ON DUPLICATE KEY UPDATE and is being kept only for compatibility. There is no performance increase from using it.
Yes, you can use the 'replace' syntax:
REPLACE INTO table1 (key, col1, col2) values (1, 'val1','val2');
This is a feature specific to MySQL and is not necessarily implemented in other databases.
As for efficiency, my guess is that a straight update will be faster, since MySQL essentially catches the duplicate key error and handles it accordingly. However, unless you are doing large amounts of insert/updates, the performance impact will be fairly small.
Look at the REPLACE command, it meets your requirements.
Related
I'm moving a msSQL database to mySQL. In my MS SQL database I have stored procedures that use an "upsert" (update, if ##rowcount = 0, Insert) type of thing.
I want to do the same kind of think in My SQL. I have found a couple of options
1) use - Insert xxxx on duplicate key update values (x, y, x) etc.
2) Replace into table_name (col_name,...) (value,...)
Which one is more efficient? I'm leaning towards the 2nd one since I will be doing a lot of updating, rather than inserting. Also, I believe that the insert on duplicate key will keep bumping the auto_increment values even when it ends up being an update.
Another note: Each account record will be updated EVERY night. Occasionally a new account record will be inserted, but again, primarily the accounts will be updated.
Are there other/better options that I'm overlooking. Am I on the right track?
MySQL has several facilities for this:
REPLACE has the effect of a DELETE if the row exists, then INSERT. This means it cannot perform partial updates on the data, any fields that are not specified revert to defaults.
ON DUPLICATE KEY UPDATE is an option on an INSERT that can handle key collisions, including those on a PRIMARY KEY. If a duplicate is found, the UPDATE statement you specify is executed instead.
For example:
INSERT INTO people (id, name) VALUES (1, 'Jeremy')
ON DUPLICATE KEY UPDATE name=VALUES(name)
You can use VALUES() to specify the same value in the INSERT without having to repeat it.
It's important to remember that NULL values can be duplicated since they don't exist and aren't equivalent: NULL=NULL is false. Non-NULL values can be enforced unique, or you can have a NOT NULL column to avoid this.
I'm not optimistic that this can be done without a stored procedure, but I'm curious if the following is possible.
I want to write a single query insert/update that updates a row if it finds a match and if not inserts into the table with the values it would have been updating.
So... something like
updateInsert into table_a set n = 'foo' where p='bar';
in the event that there is no row where p='bar' it would automatically insert into table_a set n = 'foo';
EDIT:
Based on a couple of comments I see that I need to clarify that n is not a PRIMARY KEY and the table actually needs the freedom to have duplicate rows. I just have a situation where a specific entry needs to be unique... perhaps I'm just mixing metaphors in a bad way and should pull this out into a separate table where this key is unique.
I would enforce this with the table schema - utilize a unique multi-column key on the target table and use INSERT IGNORE INTO - it should throw an error on a duplicate key, but the insert will ignore on error.
What I need is to set the values of all the fields of a record with a particular key (the key is composite actually), inserting the record if there is no record with such a key yet.
REPLACE seems as meant to do the job, but at the same time its manual page suggests
INSERT ... ON DUPLICATE KEY UPDATE.
What of them should I better choose and why?
The only "side effect" of REPLACE that comes into my mind is that it would increment autoincrement values (fortunately I don't use any) while INSERT ... ON DUPLICATE KEY UPDATE probably wouldn't. What are the other practical differences to take in mind? In what particular cases can REPLACE be preferred over INSERT ... ON DUPLICATE KEY UPDATE and vice versa?
REPLACE internally performs a delete and then an insert. This can cause problems if you have a foreign key constraint pointing at that row. In this situation the REPLACE could fail or worse: if your foreign key is set to cascade delete, the REPLACE will cause rows from other tables to be deleted. This can happen even though the constraint was satisfied both before and after the REPLACE operation.
Using INSERT ... ON DUPLICATE KEY UPDATE avoids this problem and is therefore prefered.
To answer the question in terms of performance, I did a test using both the methods
Replace Into involves:
1.Try insert on the table
2. If 1 fails, delete row and insert new row
Insert on Duplicate Key Update involves:
1.Try insert on table
2.If 1 fails, update row
If all the steps involved are inserts, there should be no difference in performance. The speed has to depend on the number of updates involved. Worst case is when all the statements are updates
I have tried both the statements on my InnoDB table involving 62,510 entries (only updates). On camparing speeds:
Replace Into: 77.411 seconds
Insert on Duplicate Key Update: 2.446 seconds
Insert on Duplicate Key update is almost 32 times faster.
Table Size: 1,249,250 rows with 12 columns on an Amazon m3.medium
When using REPLACE instead of INSERT ... ON DUPLICATE KEY UPDATE, I sometimes observe key locking or deadlock problems when multiple queries arrive quickly for a given key. The atomicity of the latter (in addition to not causing cascade deletes) is all the more reason to use it.
If you don't list all the columns, I think REPLACE will reset any unmentioned columns with their default values in the replaced rows. ON DUPLICATE KEY UPDATE will leave unmentioned columns unchanged.
In what particular cases can REPLACE be preferred over INSERT ... ON
DUPLICATE KEY UPDATE and vice versa?
I've just found out the hard way that in the case of tables with a FEDERATED storage engine INSERT...ON DUPLICATE KEY UPDATE statements are accepted, but fail (with an Error 1022: Can't write; duplicate key in table...) if a duplicate-key violation occurs - see corresponding bullet point on this page of the MySQL Reference Manual.
Fortunately, I was able to use REPLACE instead of INSERT...ON DUPLICATE KEY UPDATE within my after insert trigger to achieve the desired outcome of replicating changes to a FEDERATED table.
Replace seems that it does two operations in the case that the key already exists. Perhaps that implies there is a speed difference between the two?
(INSERT)one update vs one delete + one insert(REPLACE)
EDIT: My implication that replace might be slower is actually completely wrong. Well, according to this blog post anyway... http://www.tokutek.com/2010/07/why-insert-on-duplicate-key-update-may-be-slow-by-incurring-disk-seeks/
"It is possible that in the case of a duplicate-key error, a storage engine may perform the REPLACE as an update rather than a delete plus insert, but the semantics are the same."
http://dev.mysql.com/doc/refman/5.7/en/replace.html
REPLACE seems to be necessary sometimes because INSERT IGNORE doesn't seem to work with data transformations.
If I do this, I only set largestCityPop to itself:
INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop)
SELECT stateID, MAX(city.pop) as largestCityPop, state.pop FROM city
JOIN state on city.stateID = state.ID GROUP BY city.stateID ON
DUPLICATE KEY UPDATE largestCityPop = largestCityPop
If I do this, I am using the GROUP function improperly:
INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop)
SELECT stateID, MAX(city.pop) as largestCityPop, state.pop FROM city
JOIN state on city.stateID = state.ID GROUP BY city.stateID ON
DUPLICATE KEY UPDATE largestCityPop = MAX(city.pop)
And if I do this, MySQL won't recognize the column name:
INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop)
SELECT stateID, MAX(city.pop) as largestCityPop, state.pop FROM city
JOIN state on city.stateID = state.ID GROUP BY city.stateID ON
DUPLICATE KEY UPDATE largestCityPop = city.largestCityPop
This works, but seems just plain ugly:
INSERT IGNORE INTO largestCities (stateID, largestCityPop, statePop)
SELECT * FROM (SELECT stateID, MAX(city.pop) as biggestCityPop,
state.pop FROM city JOIN state on city.stateID = state.ID GROUP BY
city.stateID) x ON DUPLICATE KEY UPDATE largestCityPop =
biggestCityPop
I have an insert query for MySQL that looks like this:
INSERT INTO table (foo, bar, fooo, baar, etc) VALUES (?,?,?,?,?)
It is used in a script to migrate some columns and keeps encountering duplicate primary keys. (Old database was awful and poorly maintained).
Is there a way I can just tell it to replace the whole offending row with the current values tossing out the old stuff entirely? I know there is an ON DUPLICATE KEY UPDATE command, but I don't know if it would help me as all I've seen it used for is incrementing.
ANSWER: INSERT INTO table (foo, bar, fooo, baar, etc) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE foo=?, bar=?, fooo=?, baar=?, etc=?
Keep in mind that you have to add the values in the referenced array again to account for the extra question marks.
on duplicate key update should work, as should a replace statement.
From MySQL manual:
If you specify ON DUPLICATE KEY
UPDATE, and a row is inserted that
would cause a duplicate value in a
UNIQUE index or PRIMARY KEY, an UPDATE
of the old row is performed.
What you said:
Is there a way I can just tell it to
replace the whole offending row with
the current values tossing out the old
stuff entirely?
Conclusion: why don't you just try it? It seems it does exactly what you want.
Edit: Since you haven't provided any examples of what you were trying to do, I'll use some from MySQL's website.
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Or if you want something that relates more to real world:
INSERT INTO table(int_field, str_field, float_field) VALUES (15, 'some string', '1.22')
ON DUPLICATE KEY UPDATE int_field = 15, str_field = 'some_string', float_field = '1.22';
Kinda strange to put it into words that short, heh.
Anyway, what I want is basically to update an entry in a table if it does exist, otherwise to create a new one filling it with the same data.
I know that's easy, but I'm relatively new to MySQL in terms of how much I've used it :P
A lot of developers still execute a query to check if a field is present in a table and then execute an insert or update query according to the result of the first query.
Try using the ON DUPLICATE KEY syntax, this is a lot faster and better then executing 2 queries. More info can be found here
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;
if you want to keep the same value for c you can do an update with the same value
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=6;
the difference between 'replace' and 'on duplicate key':
replace: inserts, or deletes and inserts
on duplicate key: inserts or updates
if your table doesn't have a primary key or unique key, the replace doesn't make any sense.
You can also use the VALUES function to avoid having to specify the actual values twice. E.g. instead of
INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=6;
you can use
INSERT INTO table (a,b,c) VALUES (4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(c);
Where VALUES(c) will evaluate to the value specified prevously (6).
Use 'REPLACE INTO':
REPLACE INTO table SET id = 42, foo = 'bar';
See more in the MySQL documentation
As the others have said, REPLACE is the way to go. Just be careful using it though, since it actually does a DELETE and INSERT on the table. This is fine most of the time, but if you have foreign keys with constraints like ON DELETE CASCADE, it can cause some big problems.
Look up REPLACE in the MySQL manual.
REPLACE works exactly like INSERT,
except that if an old row in the table
has the same value as a new row for a
PRIMARY KEY or a UNIQUE index, the old
row is deleted before the new row is
inserted. See Section 12.2.5, “INSERT
Syntax”.
REPLACE is a MySQL extension to the
SQL standard. It either inserts, or
deletes and inserts. For another MySQL
extension to standard SQL — that
either inserts or updates — see
Section 12.2.5.3, “INSERT ... ON
DUPLICATE KEY UPDATE Syntax”.
If you have the following INSERT query:
INSERT INTO table (id, field1, field2) VALUES (1, 23, 24)
This is the REPLACE query you should run:
REPLACE INTO table (id, field1, field2) VALUES (1, 23, 24)