插入新的数据库条目是否比检查该条目是否首先存在快? [英] Is inserting a new database entry faster than checking if the entry exists first?

查看:79
本文介绍了插入新的数据库条目是否比检查该条目是否首先存在快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经有人告诉我,仅运行插入并让插入失败比检查数据库条目是否存在然后插入是否丢失要快.

I was once told that it is faster to just run an insert and let the insert fail than to check if a database entry exists and then inserting if it is missing.

我还被告知,大多数数据库都针对读而不是写进行了大量优化,所以快速检查不会比慢速插入快吗?

I was also told that that most databases are heavily optimized for reading reading rather than writing, so wouldn't a quick check be faster than a slow insert?

这是预期的碰撞次数的问题吗? (即,仅当条目已存在的可能性很小时,插入它才会更快.)它是否取决于我正在运行的数据库类型?因此,是否有一种方法会不断向我的错误日志中添加插入错误,这是不好的做法吗?

Is this a question of the expected number of collisions? (IE it's faster to insert only if there is a low chance of the entry already existing.) Does it depend on the database type I am running? And for that matter, is it bad practice to have a method that is going to be constantly adding insert errors to my error log?

谢谢.

推荐答案

如果插入由于索引冲突而失败,那么它最多比检查记录是否存在慢. (两者都需要检查索引是否包含该值.)如果插入将成功,则发出两个查询要比发出一个查询慢得多.

If the insert is going to fail because of an index violation, it will be at most marginally slower than a check that the record exists. (Both require checking whether the index contains the value.) If the insert is going to succeed, then issuing two queries is significantly slower than issuing one.

这篇关于插入新的数据库条目是否比检查该条目是否首先存在快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆