1
1
1010101010102014-03-25 18:58:02
MySQL
101010101010, 2014-03-25 18:58:02

Which sql commands are more efficient?

Hello, I've run into this issue.
You need to add data to the tables if they are not there (there is a unique identifier in the data).
What is the best way to do this?
1) SELECT query to check for existence, and then add
2) add without checking.
And you need to delete the data if it is in the table (there is a unique identifier in the data).
What is the best way to do this?
1) query SELECT to check for existence, and then delete
2) delete without checking.
MySql database, InnoDB tables

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2014-03-25
@101010101010

The path in the table `table` field `field1` is defined as UNIQUE (or PRIMARY KEY).
Adding only new

INSERT IGNORE INTO `table` (`field1`, `field2`, `field3`) 
    VALUES (`valA1`, `valA2`, `valA3`), (`valB1`, `valB2`, `valB3`)

Addition with replacement
INSERT INTO `table` (`field1`, `field2`, `field3`) 
    VALUES (`valA1`, `valA2`, `valA3`), (`valB1`, `valB2`, `valB3`) 
    ON DUPLICATE KEY UPDATE `field2` = VALUES(`field2`), `field3` = VALUES(`field3`)

Removal
DELETE FROM `table` WHERE `field1` IN (`valA1`, `valB1`)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question