<small id='rDn14'></small><noframes id='rDn14'>

          <bdo id='rDn14'></bdo><ul id='rDn14'></ul>

        <legend id='rDn14'><style id='rDn14'><dir id='rDn14'><q id='rDn14'></q></dir></style></legend>
        <tfoot id='rDn14'></tfoot>

        <i id='rDn14'><tr id='rDn14'><dt id='rDn14'><q id='rDn14'><span id='rDn14'><b id='rDn14'><form id='rDn14'><ins id='rDn14'></ins><ul id='rDn14'></ul><sub id='rDn14'></sub></form><legend id='rDn14'></legend><bdo id='rDn14'><pre id='rDn14'><center id='rDn14'></center></pre></bdo></b><th id='rDn14'></th></span></q></dt></tr></i><div id='rDn14'><tfoot id='rDn14'></tfoot><dl id='rDn14'><fieldset id='rDn14'></fieldset></dl></div>

        Spring数据保存与saveAll性能

        时间:2024-08-24

        1. <i id='4PYU9'><tr id='4PYU9'><dt id='4PYU9'><q id='4PYU9'><span id='4PYU9'><b id='4PYU9'><form id='4PYU9'><ins id='4PYU9'></ins><ul id='4PYU9'></ul><sub id='4PYU9'></sub></form><legend id='4PYU9'></legend><bdo id='4PYU9'><pre id='4PYU9'><center id='4PYU9'></center></pre></bdo></b><th id='4PYU9'></th></span></q></dt></tr></i><div id='4PYU9'><tfoot id='4PYU9'></tfoot><dl id='4PYU9'><fieldset id='4PYU9'></fieldset></dl></div>
          • <tfoot id='4PYU9'></tfoot>

            <small id='4PYU9'></small><noframes id='4PYU9'>

            • <bdo id='4PYU9'></bdo><ul id='4PYU9'></ul>
            • <legend id='4PYU9'><style id='4PYU9'><dir id='4PYU9'><q id='4PYU9'></q></dir></style></legend>

                  <tbody id='4PYU9'></tbody>
                  本文介绍了Spring数据保存与saveAll性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图理解为什么 saveAll 比保存在 Spring Data 存储库中的性能更好.我正在使用 CrudRepository 可以看到 这里.

                  I'm trying to understand why saveAll has better performance than save in the Spring Data repositories. I'm using CrudRepository which can be seen here.

                  为了测试,我创建并添加了 10k 个实体,这些实体只有一个 id 和一个随机字符串(对于基准测试,我将字符串保持为常量)到一个列表中.遍历我的列表并在每个元素上调用 .save 需要 40 秒.对同一整个列表调用 .saveAll 只需 2 秒.使用 30k 个元素调用 .saveAll 需要 4 秒.在执行每个测试之前,我确保截断我的表.即使将 .saveAll 调用批处理到 50 个子列表也需要 10 秒,而 30k.

                  To test I created and added 10k entities, which just have an id and a random string (for the benchmark I kept the string a constant), to a list. Iterating over my list and calling .save on each element, it took 40 seconds. Calling .saveAll on the same entire list completed in 2 seconds. Calling .saveAll with even 30k elements took 4 seconds. I made sure to truncate my table before performing each test. Even batching the .saveAll calls to sublists of 50 took 10 seconds with 30k.

                  带有整个列表的简单 .saveAll 似乎是最快的.

                  The simple .saveAll with the entire list seems to be the fastest.

                  我试图浏览 Spring Data 源代码,但 this 是我发现的唯一有价值的东西.这里似乎 .saveAll 只是简单地迭代整个 Iterable 并像我一样在每个人上调用 .save .那么它是如何快得多的呢?它是否在内部进行一些事务批处理?

                  I tried to browse the Spring Data source code but this is the only thing I found of value. Here it seems .saveAll simply iterates over the entire Iterable and calls .save on each one like I was doing. So how is it that much faster? Is it doing some transactional batching internally?

                  推荐答案

                  没有你的代码,我不得不猜测,我相信这与在 save 与在 saveAll 的情况下打开一笔交易.

                  Without having your code, I have to guess, I believe it has to do with the overhead of creating new transaction for each object saved in the case of save versus opening one transaction in the case of saveAll.

                  注意 savesaveAll 的定义,它们都用 @Transactional 注释.如果您的项目配置正确,这似乎是因为实体被保存到数据库中,这意味着每当调用这些方法之一时都会创建一个事务.如果您在循环中调用 save,这意味着每次调用 save 时都会创建一个新事务,但在 saveAll 的情况下是一次调用,因此无论保存的实体数量如何,都会创建一个事务.

                  Notice the definition of save and saveAll they are both annotated with @Transactional. If your project is configured properly, which seems to be the case since entities are being saved to the database, that means a transaction will be created whenever one of these methods are called. if you are calling save in a loop that means a new transaction is being created each time you call save, but in the case of saveAll there is one call and therefor one transaction created regardless of the number of entities being saved.

                  我假设测试本身并没有在事务中运行,如果要在事务中运行,那么所有对保存的调用都将在该事务中运行,因为默认的事务传播是 Propagation.REQUIRED,这意味着如果有一个事务已经打开,调用将在其中运行.如果您打算使用 spring 数据,我强烈建议您阅读 Spring 中的事务管理.

                  I'm assuming that the test is not itself being run within a transaction, if it were to be run within a transaction then all calls to save will run within that transaction since the the default transaction propagation is Propagation.REQUIRED, that means if there is a transaction already open the calls will be run within it. If your planning to use spring data I strongly recommend that you read about transaction management in Spring.

                  这篇关于Spring数据保存与saveAll性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么我看不到通过 Spring Data JPA 的更新查询发出的更改? 下一篇:Spring数据查询日期时间只有日期

                  相关文章

                    • <bdo id='4uTvU'></bdo><ul id='4uTvU'></ul>
                    1. <i id='4uTvU'><tr id='4uTvU'><dt id='4uTvU'><q id='4uTvU'><span id='4uTvU'><b id='4uTvU'><form id='4uTvU'><ins id='4uTvU'></ins><ul id='4uTvU'></ul><sub id='4uTvU'></sub></form><legend id='4uTvU'></legend><bdo id='4uTvU'><pre id='4uTvU'><center id='4uTvU'></center></pre></bdo></b><th id='4uTvU'></th></span></q></dt></tr></i><div id='4uTvU'><tfoot id='4uTvU'></tfoot><dl id='4uTvU'><fieldset id='4uTvU'></fieldset></dl></div>

                      <tfoot id='4uTvU'></tfoot>
                    2. <legend id='4uTvU'><style id='4uTvU'><dir id='4uTvU'><q id='4uTvU'></q></dir></style></legend>
                    3. <small id='4uTvU'></small><noframes id='4uTvU'>