Is it possible to always use INSERT .... ON DUPLICATE UPDATE for simple updates / inserts?

(MYSQL) Are there any significant differences in performance or other reasons not to use INSERT ... ON DUPLICATE UPDATEsql to update using PK / insert new material?

+3
source share
2 answers

No no.

INSERT ON DUPLICATE KEY UPDATEwill find the record and update it in the same way as it makes simple UPDATE.

This is actually just UPDATEwhat follows INSERTif a failure UPDATE.

INSERT ON DUPLICATE KEY UPDATE can be a faster alternative to group updating (when you have a source table with several records per key and you want to increase the target table once per record).

, :

INSERT
INTO    t_target
SELECT  target, cnt
FROM    t_sparse d
ON DUPLICATE KEY UPDATE
        t_target.cnt = t_target.cnt + d.cnt

, :

INSERT
INTO    t_target
SELECT  target, cnt
FROM    (
        SELECT  target, SUM(cnt) AS cnt
        FROM    t_sparse di
        GROUP BY
                target
        ) d
ON DUPLICATE KEY UPDATE
        t_target.cnt = t_target.cnt + d.cnt

. :

+3

. . , , , INSERT ... ON DUPLICATE UPDATE.

, INSERT ... ON DUPLICATE UPDATE, DMBS , . mysql, 10-20% + - . INSERT ... ON DUPLICATE UPDATE, , , .

0

Source: https://habr.com/ru/post/1725404/


All Articles