Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL function to insert record if update fails?

Part of the system I'm working on at the moment involves a log in mysql, with counts being frequently updated.

The data being inserted is of the format:

   date    | name | count |
-----------+------+-------+
2009-01-12 | alan |   5   |
2009-01-12 | dave |   2   |
2009-01-12 | mary |   1   |

This data is parsed regularly from a flat file, summarised as above in preparation for a db insert/update - the unique key on the database is the (date, name) pair.

Previously, this system would check the existing table for any records for a given date and name pair before deciding on an update or insert.

The problem we're having is, as this table grows, the response time isn't getting any better, and we want to reduce the number of queries as much as possible.

The system was recently updated to run a INSERT ... ON DUPLICATE KEY UPDATE query, which has reduced the number of selects marginally, but our common case by some distance is the update.

I'm wondering if anyone knows of a mysql function that's essentially INSERT ... ON DUPLICATE KEY UPDATE in reverse, i.e. will try to update a row, if none match then perform the insert?


Edit

I didn't make it too clear above, what I would like to do when I have the record ('2009-01-12','alan','5') for example, is:

UPDATE table SET count = count+5 WHERE date = '2009-01-12' and name = 'alan';

and if the above fails, insert the above data. The need to increment a counter is why REPLACE won't work. Replace performs a delete & insert, and doesn't let you refer to the row being deleted, so count = count + 5 wouldn't increment the previous count value for by 5.

@jasoncohen - the INSERT ... ON DUPLICATE KEY UPDATE does do the job, but I'm asking if there's a more optimal way to do this.

Sorry for any confusion resulting from the poor original phrasing!

like image 439
ConroyP Avatar asked Jan 21 '09 18:01

ConroyP


People also ask

Can I insert with update MySQL?

When you insert a new row into a table if the row causes a duplicate in UNIQUE index or PRIMARY KEY , MySQL will issue an error. However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will update the existing row with the new values instead.

Does MySQL update insert if not exists?

Often you have the situation that you need to check if an table entry exists, before you can make an update. If it does not exist, you have to do an insert first. Unfortunately, this the 'ON DUPLICATE KEY' statement only works on PRIMARY KEY and UNIQUE columns.

How do you insert a new record in a table if not exists?

There are three ways you can perform an “insert if not exists” query in MySQL: Using the INSERT IGNORE statement. Using the ON DUPLICATE KEY UPDATE clause. Or using the REPLACE statement.

Which command insert rows that do not exist and update the rows that exist?

Using INSERT IGNORE This means that an INSERT IGNORE statement which contains a duplicate value in a UNIQUE index or PRIMARY KEY field does not produce an error, but will instead simply ignore that particular INSERT command entirely.


1 Answers

It's just the same. With "UPDATE ... ON NO KEY INSERT", the database engine will still have to check first if there is something to update. Hence no need for a separate construct even if update is most common

like image 187
Michael Buen Avatar answered Sep 22 '22 15:09

Michael Buen