英文:
Is there any significant downside to just always using transactions in JDBI?
问题
对于永远不会变得太大的表,是否只使用JdbiExecutor::executeInTransaction
而不是JdbiExecutor::execute
有任何不利之处吗?
我试图简化一个非常庞大且复杂的代码片段,以便新开发人员能够更快地上手。如果性能上没有明显的差异(即使有差异,在这里进行优化也是过早的优化),那么我想编写一个实用程序,始终只使用JdbiExecutor::executeInTransaction
。
英文:
For tables that will never get too large, is there any downside to just using JdbiExecutor::executeInTransaction
instead of JdbiExecutor::execute
?
I'm trying to simplify a very large and complex piece of code so that new developers can onboard more quickly. If there isn't a significant difference in performance (which, even then, it would be premature optimization to address it here) then I'd like write a utility that just always uses JdbiExecutor::executeInTransaction
.
答案1
得分: 2
PostgreSQL服务器以自动提交模式运行,因此使用显式事务不仅会导致将COMMIT
语句发送到数据库,还会发送开始显式事务的BEGIN
语句。
这意味着您每个事务最多可能会多出2次客户端-服务器往返。
仅当您需要将多个语句捆绑到一个事务中时,使用显式事务可能会提高性能。
我会运行一个简单的基准测试,以查看影响是否明显;如果没有影响,可以继续简化您的代码。
英文:
PostgreSQL server operates in autocommit mode, so using explicit transactions will not only cause COMMIT
statements to be sent to the database, but also BEGIN
statements that start an explicit transaction.
That means you could have up to 2 extra client-server round trips per transaction.
Using explicit transactions only if you need to bundle multiple statements into a transaction might improve your performance.
I would run a simple benchmark to see if the impact is noticeable; if not, go ahead and simplify your code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论