Redian新闻
>
INSERT or UPDATE, which is faster?
avatar
INSERT or UPDATE, which is faster?# Database - 数据库
n*u
1
I am writing C# with SQL statement to update a record which has four text
fields (Introduction, What am I looking for, etc)
When a user, first time, inputs texts into these fields, Of course use
INSERT.
But when the user decides to modify one of these, use UPDATE or INSERT?
If I choose INSERT, I need to delete old record, INSERT a new one.
If I choose UPDATE, I could just UPDATE record, but I am afraid the text
field may take longer time to update.
I feel like INSERT is always faster than UPDATE
avatar
t*i
2
do insert when you have a new row; do update when you need to modify a row.
delete + insert is for sure more expensive than a update.
avatar
n*u
3
Thanks!

【在 t*********i 的大作中提到】
: do insert when you have a new row; do update when you need to modify a row.
: delete + insert is for sure more expensive than a update.

avatar
c*d
4
嗯,如果要操作的数据量小,update要比delete+insert快
如果要操作的数据量大,update要比truncate+insert慢

【在 t*********i 的大作中提到】
: do insert when you have a new row; do update when you need to modify a row.
: delete + insert is for sure more expensive than a update.

avatar
x*e
5
I have to say, this is very misleading.
It really depends on what database you are talking about and the design.

【在 t*********i 的大作中提到】
: do insert when you have a new row; do update when you need to modify a row.
: delete + insert is for sure more expensive than a update.

avatar
B*g
6
砸变truncate了?

【在 c*****d 的大作中提到】
: 嗯,如果要操作的数据量小,update要比delete+insert快
: 如果要操作的数据量大,update要比truncate+insert慢

avatar
c*d
7
大数据量的时候用truncate+insert比Update快
陈述一个事实,哈哈

【在 B*****g 的大作中提到】
: 砸变truncate了?
avatar
n*u
8
I agree.
Last time it would took 3 days to update 320,000 records (just update MSN
etc.)
But I truncated table first, INSERT... SELECT..., 30 seconds, done.

【在 c*****d 的大作中提到】
: 大数据量的时候用truncate+insert比Update快
: 陈述一个事实,哈哈

avatar
S*t
9
truncate is different from delete

【在 n****u 的大作中提到】
: I agree.
: Last time it would took 3 days to update 320,000 records (just update MSN
: etc.)
: But I truncated table first, INSERT... SELECT..., 30 seconds, done.

avatar
n*u
10
Yes, I talked to my colleague this morning. He thinks DELETE might take long
time. So his suggestion is UPDATE.
Well I will look at the original PHP code to find out how that team did.Then
I would write similar one in C#.
Thanks all!

【在 S*********t 的大作中提到】
: truncate is different from delete
avatar
B*g
11
read coolbid's post again.

long
Then

【在 n****u 的大作中提到】
: Yes, I talked to my colleague this morning. He thinks DELETE might take long
: time. So his suggestion is UPDATE.
: Well I will look at the original PHP code to find out how that team did.Then
: I would write similar one in C#.
: Thanks all!

avatar
n*u
12
Never mind, just got fired half hour ago. hehe

【在 B*****g 的大作中提到】
: read coolbid's post again.
:
: long
: Then

avatar
c*e
13
not necessarily

【在 t*********i 的大作中提到】
: do insert when you have a new row; do update when you need to modify a row.
: delete + insert is for sure more expensive than a update.

avatar
c*e
14
what?

【在 n****u 的大作中提到】
: Never mind, just got fired half hour ago. hehe
avatar
n*u
15
A senior guy was fired two hours before I got fired.
Not sure what happened in big boss's mind.

【在 c*******e 的大作中提到】
: what?
avatar
n*6
16
What happends when you need the log file?
Truncate does not generate the log, does not fire the trigger.

【在 c*****d 的大作中提到】
: 大数据量的时候用truncate+insert比Update快
: 陈述一个事实,哈哈

avatar
c*d
17
我ft
为什么呀?

【在 n****u 的大作中提到】
: Never mind, just got fired half hour ago. hehe
avatar
a*t
18
if the updated columns are not indexed, update is faster

【在 n****u 的大作中提到】
: I am writing C# with SQL statement to update a record which has four text
: fields (Introduction, What am I looking for, etc)
: When a user, first time, inputs texts into these fields, Of course use
: INSERT.
: But when the user decides to modify one of these, use UPDATE or INSERT?
: If I choose INSERT, I need to delete old record, INSERT a new one.
: If I choose UPDATE, I could just UPDATE record, but I am afraid the text
: field may take longer time to update.
: I feel like INSERT is always faster than UPDATE

avatar
c*e
19
comfort

【在 n****u 的大作中提到】
: A senior guy was fired two hours before I got fired.
: Not sure what happened in big boss's mind.

avatar
c*d
20
不乖,发几个包子安慰一下吧

【在 c*******e 的大作中提到】
: comfort
avatar
c*e
21
嗯,本版很穷,才66.8个伪币,都发了

【在 c*****d 的大作中提到】
: 不乖,发几个包子安慰一下吧
avatar
n*u
22
多谢老大啦

【在 c*******e 的大作中提到】
: 嗯,本版很穷,才66.8个伪币,都发了
avatar
n*6
23
how to make/create bao zi?

【在 c*******e 的大作中提到】
: 嗯,本版很穷,才66.8个伪币,都发了
avatar
w*e
24
多灌水

【在 n********6 的大作中提到】
: how to make/create bao zi?
avatar
B*g
25
此处不留爷,自有留爷处。

【在 n****u 的大作中提到】
: A senior guy was fired two hours before I got fired.
: Not sure what happened in big boss's mind.

avatar
k*e
26
Hence truncate doesn't work with replication and logshipping

【在 n********6 的大作中提到】
: What happends when you need the log file?
: Truncate does not generate the log, does not fire the trigger.

avatar
t*i
28
My gosh!
Just read this...Hope you feel better now.

【在 n****u 的大作中提到】
: Never mind, just got fired half hour ago. hehe
avatar
t*i
29
I am curious...can you give a example (or document) that a update would be
more expensive than a delete+insert.
I kind of think all relational DBs have similar structures...

【在 x***e 的大作中提到】
: I have to say, this is very misleading.
: It really depends on what database you are talking about and the design.

avatar
x*e
30
the different between structures, you should be able to get some reference
online.
One example here
one of the big difference between SQL server and Oracle is the transaction
log.
SQL use single log process, while Oracle is is multi.
Based on that, lots of code need to be re-writen.
So you can not just say it is universal for all DBs.
They could be the same in the future, but not now.
Give you one example for update could be much more expensive.
You have an update trigger for the table.
Generall

【在 t*********i 的大作中提到】
: I am curious...can you give a example (or document) that a update would be
: more expensive than a delete+insert.
: I kind of think all relational DBs have similar structures...

avatar
t*i
31
I don't know much about Sql server, here is what I found:
http://www.windowsitlibrary.com/Content/77/21/1.html
A direct, same-page Update occurs when a record is deleted from a page and
inserted onto that same page, usually because of increasing row length.
Because the record does not move to a new page, none of the indexes needs to
be updated, unless, of course, an indexed field is updated. This type of
Update gets two records, an op code of 5 (Delete), followed by an op code of
6 (Indirect Ins
avatar
m*8
32
in MS SQL,internally, an update = delete + insert
so u figure the rest.
avatar
x*e
33
I do not know about that.
I only know it is case by case.
And I am not work for MS either....
I can not image in the following case the update >= insert + delete
in any of the SQL env.
you have a table
Id1, id2, text
while ID1 is unique index, id2 is an int none indexed column and text is a
text column.
and you want to update the id2 column....based on id1....

to
of

【在 t*********i 的大作中提到】
: I don't know much about Sql server, here is what I found:
: http://www.windowsitlibrary.com/Content/77/21/1.html
: A direct, same-page Update occurs when a record is deleted from a page and
: inserted onto that same page, usually because of increasing row length.
: Because the record does not move to a new page, none of the indexes needs to
: be updated, unless, of course, an indexed field is updated. This type of
: Update gets two records, an op code of 5 (Delete), followed by an op code of
: 6 (Indirect Ins

avatar
t*i
34
Just see your reply :)
for the case you mentioned.
**************************************************
you have a table
Id1, id2, text
while ID1 is unique index, id2 is an int none indexed column and text is a
text column.
and you want to update the id2 column....based on id1....
****************************************************
I still think in Oracle, Update is quicker than delete+insert
in SQL server (and all other DB system that one update = one delete + one
insert), that should be no much
avatar
L*d
35
Another example is, if you try to update a blob or update a big chunk of
data that could cause chains.
//i am not a dba
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。