ON DUPLICATE KEY UPDATE - precedence“INSERT IGNORE” vs “INSERT … ON DUPLICATE KEY UPDATE”Finding duplicate values in MySQLDoing an INSERT INTO tableA with sub-SELECT on tableB sorted with ON DUPLICATE KEY UPDATEMysql Index Keyname ImportanceYii INSERT … ON DUPLICATE UPDATEUsing IF() With ON DUPLICATE KEY UPDATE in MySQLINSERT ON DUPLICATE KEY UPDATE when composite keyForeign Keys Referencing multiple primary key & cannot add foreign key constraint on MySQLWhy is affectedRows takes time to updateMySQL Insert / On duplicate key failure
How can I stop myself from micromanaging other PCs' actions?
How is the uk visa 180 calculated
What do teaching faculty do during semester breaks?
Using "Kollege" as "university friend"?
How to get the two pictures aligned
Why is chess failing to attract big name sponsors?
Difficulty pronouncing "maths", "baths", "hundredths", "sixths"
Invert Some Switches on a Switchboard
Iterate over non-const variables in C++
Area of parallelogram = Area of square. Shear transform
Is my employer paying me fairly? Going from 1099 to W2
How important is a good quality camera for good photography?
Why are so many countries still in the Commonwealth?
How do campaign rallies gain candidates votes?
Character is called by their first initial. How do I write it?
Can two figures have the same area, perimeter, and same number of segments have different shape?
Are there any examples of technologies have been lost over time?
How to judge a Ph.D. applicant that arrives "out of thin air"
Send a single HTML email from Thunderbird, overriding the default "plain text" setting
401(k) investment after being fired. Do I own it?
Is it better to memorize verb's 1st person perfect tense?
Why isn't my platform event chain working?
Does the Intel 8086 CPU have user mode and kernel mode?
How to copy a file transactionally?
ON DUPLICATE KEY UPDATE - precedence
“INSERT IGNORE” vs “INSERT … ON DUPLICATE KEY UPDATE”Finding duplicate values in MySQLDoing an INSERT INTO tableA with sub-SELECT on tableB sorted with ON DUPLICATE KEY UPDATEMysql Index Keyname ImportanceYii INSERT … ON DUPLICATE UPDATEUsing IF() With ON DUPLICATE KEY UPDATE in MySQLINSERT ON DUPLICATE KEY UPDATE when composite keyForeign Keys Referencing multiple primary key & cannot add foreign key constraint on MySQLWhy is affectedRows takes time to updateMySQL Insert / On duplicate key failure
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
When doing an INSERT INTO tableA SELECT a,b,c FROM tableB ON DUPLICATE KEY UPDATE x=y
What is the precedence on how the duplicate keys are evaluated? I assume that MySQL first checks to see if a tuple from tableB exists clashes with a unique/primary key in tableA. If the SELECT doesn't include a primary key, or if no other duplicate key exists, then each subsequent UNIQUE INDEX "group" is evaluated and the same checking is performed. But what happens if your tableB has multiple sets of unique, multi-column indexes? Are they evaluated top-to-bottom, as described by SHOW INDEXES FROM tableB
?
Here's my SHOW INDEXES FROM <table>
:
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation
daily_metrics,0,PRIMARY,1,id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,1,line_item_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,2,creative_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,3,year,A
daily_metrics,0,unique_lineItem_creative_y_m_d,4,month,A
...
Imagine there are additional sets of unique indexes similar to unique_lineItem_creative_y_m_d
The docs don't seem to illustrate this behavior.
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I also assume that the first matching unique index is used, if a match exists, without any attempt to match subsequent unique indexes that could match. In other words, the first unique index that matches is used, without regard for trying to find the best possible match across all indexes.
mysql on-duplicate-key
add a comment |
When doing an INSERT INTO tableA SELECT a,b,c FROM tableB ON DUPLICATE KEY UPDATE x=y
What is the precedence on how the duplicate keys are evaluated? I assume that MySQL first checks to see if a tuple from tableB exists clashes with a unique/primary key in tableA. If the SELECT doesn't include a primary key, or if no other duplicate key exists, then each subsequent UNIQUE INDEX "group" is evaluated and the same checking is performed. But what happens if your tableB has multiple sets of unique, multi-column indexes? Are they evaluated top-to-bottom, as described by SHOW INDEXES FROM tableB
?
Here's my SHOW INDEXES FROM <table>
:
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation
daily_metrics,0,PRIMARY,1,id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,1,line_item_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,2,creative_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,3,year,A
daily_metrics,0,unique_lineItem_creative_y_m_d,4,month,A
...
Imagine there are additional sets of unique indexes similar to unique_lineItem_creative_y_m_d
The docs don't seem to illustrate this behavior.
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I also assume that the first matching unique index is used, if a match exists, without any attempt to match subsequent unique indexes that could match. In other words, the first unique index that matches is used, without regard for trying to find the best possible match across all indexes.
mysql on-duplicate-key
I general, the sort order of a select is undefined, except when you specify an order by clause. The existence of indexes can affect that order, but there is no guarantee. I'm not even sure the order of inserting/updating is guaranteed, and I can imagine it is not, to possibly support parallel inserts/updates. Anyway, if you want to use specific values, it's better to write the select in such a way that it only returns those values, and don't try to guess if the incorrect values will be overwritten before the end of the statement.
– GolezTrol
Mar 26 at 17:09
add a comment |
When doing an INSERT INTO tableA SELECT a,b,c FROM tableB ON DUPLICATE KEY UPDATE x=y
What is the precedence on how the duplicate keys are evaluated? I assume that MySQL first checks to see if a tuple from tableB exists clashes with a unique/primary key in tableA. If the SELECT doesn't include a primary key, or if no other duplicate key exists, then each subsequent UNIQUE INDEX "group" is evaluated and the same checking is performed. But what happens if your tableB has multiple sets of unique, multi-column indexes? Are they evaluated top-to-bottom, as described by SHOW INDEXES FROM tableB
?
Here's my SHOW INDEXES FROM <table>
:
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation
daily_metrics,0,PRIMARY,1,id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,1,line_item_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,2,creative_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,3,year,A
daily_metrics,0,unique_lineItem_creative_y_m_d,4,month,A
...
Imagine there are additional sets of unique indexes similar to unique_lineItem_creative_y_m_d
The docs don't seem to illustrate this behavior.
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I also assume that the first matching unique index is used, if a match exists, without any attempt to match subsequent unique indexes that could match. In other words, the first unique index that matches is used, without regard for trying to find the best possible match across all indexes.
mysql on-duplicate-key
When doing an INSERT INTO tableA SELECT a,b,c FROM tableB ON DUPLICATE KEY UPDATE x=y
What is the precedence on how the duplicate keys are evaluated? I assume that MySQL first checks to see if a tuple from tableB exists clashes with a unique/primary key in tableA. If the SELECT doesn't include a primary key, or if no other duplicate key exists, then each subsequent UNIQUE INDEX "group" is evaluated and the same checking is performed. But what happens if your tableB has multiple sets of unique, multi-column indexes? Are they evaluated top-to-bottom, as described by SHOW INDEXES FROM tableB
?
Here's my SHOW INDEXES FROM <table>
:
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation
daily_metrics,0,PRIMARY,1,id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,1,line_item_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,2,creative_id,A
daily_metrics,0,unique_lineItem_creative_y_m_d,3,year,A
daily_metrics,0,unique_lineItem_creative_y_m_d,4,month,A
...
Imagine there are additional sets of unique indexes similar to unique_lineItem_creative_y_m_d
The docs don't seem to illustrate this behavior.
https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I also assume that the first matching unique index is used, if a match exists, without any attempt to match subsequent unique indexes that could match. In other words, the first unique index that matches is used, without regard for trying to find the best possible match across all indexes.
mysql on-duplicate-key
mysql on-duplicate-key
edited Mar 26 at 18:07
Jesse Skrivseth
asked Mar 26 at 16:54
Jesse SkrivsethJesse Skrivseth
19611 bronze badges
19611 bronze badges
I general, the sort order of a select is undefined, except when you specify an order by clause. The existence of indexes can affect that order, but there is no guarantee. I'm not even sure the order of inserting/updating is guaranteed, and I can imagine it is not, to possibly support parallel inserts/updates. Anyway, if you want to use specific values, it's better to write the select in such a way that it only returns those values, and don't try to guess if the incorrect values will be overwritten before the end of the statement.
– GolezTrol
Mar 26 at 17:09
add a comment |
I general, the sort order of a select is undefined, except when you specify an order by clause. The existence of indexes can affect that order, but there is no guarantee. I'm not even sure the order of inserting/updating is guaranteed, and I can imagine it is not, to possibly support parallel inserts/updates. Anyway, if you want to use specific values, it's better to write the select in such a way that it only returns those values, and don't try to guess if the incorrect values will be overwritten before the end of the statement.
– GolezTrol
Mar 26 at 17:09
I general, the sort order of a select is undefined, except when you specify an order by clause. The existence of indexes can affect that order, but there is no guarantee. I'm not even sure the order of inserting/updating is guaranteed, and I can imagine it is not, to possibly support parallel inserts/updates. Anyway, if you want to use specific values, it's better to write the select in such a way that it only returns those values, and don't try to guess if the incorrect values will be overwritten before the end of the statement.
– GolezTrol
Mar 26 at 17:09
I general, the sort order of a select is undefined, except when you specify an order by clause. The existence of indexes can affect that order, but there is no guarantee. I'm not even sure the order of inserting/updating is guaranteed, and I can imagine it is not, to possibly support parallel inserts/updates. Anyway, if you want to use specific values, it's better to write the select in such a way that it only returns those values, and don't try to guess if the incorrect values will be overwritten before the end of the statement.
– GolezTrol
Mar 26 at 17:09
add a comment |
2 Answers
2
active
oldest
votes
You are correct: as soon as MySQL detects a duplicate in any UNIQUE
index, it abandons the INSERT
and does the update.
The order in which MySQL evaluates the unique indexes does not change the outcome. There's no such thing as a better match for some unique index over another. Because they're unique indexes, any combination of column values that shows up as a duplicate is enough to completely specify the row to be updated.
MySQL's query planner, hopefully, chooses the index that's least costly to evaluate. But, formally speaking, the index it uses for this purpose is unpredictable. This unpredictability is an attribute of declarative languages like SQL. MySQL can do its work any way that works, and it doesn't have to tell you. It can be hard for programmers to grasp, because we're used to procedural languages.
add a comment |
If any primary or unique keys exist in tableB
, that's irrelevant. The only thing that matters for INSERT...ON DUPLICATE KEY UPDATE
are the primary or unique keys of the table you're inserting into — tableA
in your example.
If the values you insert to tableA
are already found in any of tableA
's primary or unique keys, that triggers the UPDATE
part of the IODKU.
It's about the values being inserted, not the constraints of the source table.
You can also trigger the UPDATE
without using any source table -- just by inserting a VALUES()
clause with a set of constants.
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
1
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362441%2fon-duplicate-key-update-precedence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are correct: as soon as MySQL detects a duplicate in any UNIQUE
index, it abandons the INSERT
and does the update.
The order in which MySQL evaluates the unique indexes does not change the outcome. There's no such thing as a better match for some unique index over another. Because they're unique indexes, any combination of column values that shows up as a duplicate is enough to completely specify the row to be updated.
MySQL's query planner, hopefully, chooses the index that's least costly to evaluate. But, formally speaking, the index it uses for this purpose is unpredictable. This unpredictability is an attribute of declarative languages like SQL. MySQL can do its work any way that works, and it doesn't have to tell you. It can be hard for programmers to grasp, because we're used to procedural languages.
add a comment |
You are correct: as soon as MySQL detects a duplicate in any UNIQUE
index, it abandons the INSERT
and does the update.
The order in which MySQL evaluates the unique indexes does not change the outcome. There's no such thing as a better match for some unique index over another. Because they're unique indexes, any combination of column values that shows up as a duplicate is enough to completely specify the row to be updated.
MySQL's query planner, hopefully, chooses the index that's least costly to evaluate. But, formally speaking, the index it uses for this purpose is unpredictable. This unpredictability is an attribute of declarative languages like SQL. MySQL can do its work any way that works, and it doesn't have to tell you. It can be hard for programmers to grasp, because we're used to procedural languages.
add a comment |
You are correct: as soon as MySQL detects a duplicate in any UNIQUE
index, it abandons the INSERT
and does the update.
The order in which MySQL evaluates the unique indexes does not change the outcome. There's no such thing as a better match for some unique index over another. Because they're unique indexes, any combination of column values that shows up as a duplicate is enough to completely specify the row to be updated.
MySQL's query planner, hopefully, chooses the index that's least costly to evaluate. But, formally speaking, the index it uses for this purpose is unpredictable. This unpredictability is an attribute of declarative languages like SQL. MySQL can do its work any way that works, and it doesn't have to tell you. It can be hard for programmers to grasp, because we're used to procedural languages.
You are correct: as soon as MySQL detects a duplicate in any UNIQUE
index, it abandons the INSERT
and does the update.
The order in which MySQL evaluates the unique indexes does not change the outcome. There's no such thing as a better match for some unique index over another. Because they're unique indexes, any combination of column values that shows up as a duplicate is enough to completely specify the row to be updated.
MySQL's query planner, hopefully, chooses the index that's least costly to evaluate. But, formally speaking, the index it uses for this purpose is unpredictable. This unpredictability is an attribute of declarative languages like SQL. MySQL can do its work any way that works, and it doesn't have to tell you. It can be hard for programmers to grasp, because we're used to procedural languages.
answered Mar 26 at 17:06
O. JonesO. Jones
62.2k10 gold badges78 silver badges116 bronze badges
62.2k10 gold badges78 silver badges116 bronze badges
add a comment |
add a comment |
If any primary or unique keys exist in tableB
, that's irrelevant. The only thing that matters for INSERT...ON DUPLICATE KEY UPDATE
are the primary or unique keys of the table you're inserting into — tableA
in your example.
If the values you insert to tableA
are already found in any of tableA
's primary or unique keys, that triggers the UPDATE
part of the IODKU.
It's about the values being inserted, not the constraints of the source table.
You can also trigger the UPDATE
without using any source table -- just by inserting a VALUES()
clause with a set of constants.
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
1
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
add a comment |
If any primary or unique keys exist in tableB
, that's irrelevant. The only thing that matters for INSERT...ON DUPLICATE KEY UPDATE
are the primary or unique keys of the table you're inserting into — tableA
in your example.
If the values you insert to tableA
are already found in any of tableA
's primary or unique keys, that triggers the UPDATE
part of the IODKU.
It's about the values being inserted, not the constraints of the source table.
You can also trigger the UPDATE
without using any source table -- just by inserting a VALUES()
clause with a set of constants.
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
1
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
add a comment |
If any primary or unique keys exist in tableB
, that's irrelevant. The only thing that matters for INSERT...ON DUPLICATE KEY UPDATE
are the primary or unique keys of the table you're inserting into — tableA
in your example.
If the values you insert to tableA
are already found in any of tableA
's primary or unique keys, that triggers the UPDATE
part of the IODKU.
It's about the values being inserted, not the constraints of the source table.
You can also trigger the UPDATE
without using any source table -- just by inserting a VALUES()
clause with a set of constants.
If any primary or unique keys exist in tableB
, that's irrelevant. The only thing that matters for INSERT...ON DUPLICATE KEY UPDATE
are the primary or unique keys of the table you're inserting into — tableA
in your example.
If the values you insert to tableA
are already found in any of tableA
's primary or unique keys, that triggers the UPDATE
part of the IODKU.
It's about the values being inserted, not the constraints of the source table.
You can also trigger the UPDATE
without using any source table -- just by inserting a VALUES()
clause with a set of constants.
answered Mar 26 at 17:04
Bill KarwinBill Karwin
396k67 gold badges536 silver badges690 bronze badges
396k67 gold badges536 silver badges690 bronze badges
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
1
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
add a comment |
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
1
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
Yes, my question is in what order is the set of tableA's primary or unique keys evaluated when attempting to insert.
– Jesse Skrivseth
Mar 26 at 17:08
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
I'm not sure why it matters.
– Bill Karwin
Mar 26 at 17:17
1
1
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
There is an example here which shows that the order in which unique indexes are searched matters. If one insert violates one index on row A and violates a second index on row B, then it is unclear if row A or row B should be updated.
– unutbu
Mar 26 at 17:39
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
@unutbu This illustrates part of my concern. It seems that the "first" match in tableA is updated - probably "first" in natural or index ordering - leaving any other possible matching rows as-is. We can control the order of our SELECT, but we can't control the search order used by the INSERT/UPDATE when searching for a matching row. Main takeaway from that doc: "If more than one unique index is matched, only the first is updated. It is not recommended to use this statement on tables with more than one unique index."
– Jesse Skrivseth
Mar 26 at 18:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55362441%2fon-duplicate-key-update-precedence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I general, the sort order of a select is undefined, except when you specify an order by clause. The existence of indexes can affect that order, but there is no guarantee. I'm not even sure the order of inserting/updating is guaranteed, and I can imagine it is not, to possibly support parallel inserts/updates. Anyway, if you want to use specific values, it's better to write the select in such a way that it only returns those values, and don't try to guess if the incorrect values will be overwritten before the end of the statement.
– GolezTrol
Mar 26 at 17:09