java Spring JpaRepostory 删除与 deleteInBatch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26142261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring JpaRepostory delete vs deleteInBatch
提问by kulatamicuda
What is the difference between delete(...)
and deleteInBatch(...)
methods in JpaRepostory in Spring ? The second one "deletes items in one SQL statement", but what does it mean from the application/database perspective ? Why exists two different methods with the similar results and when it is better to use one or other ?
Spring 中 JpaRepostory 中的delete(...)
和deleteInBatch(...)
方法有什么区别?第二个“删除一个 SQL 语句中的项目”,但从应用程序/数据库的角度来看,这意味着什么?为什么存在两种具有相似结果的不同方法,何时使用一种或另一种更好?
EDIT:The same applies also for deleteAll()
and deleteAllInBatch()
...
编辑:同样适用于deleteAll()
和deleteAllInBatch()
...
采纳答案by Kurt Du Bois
The delete
method is going to delete your entity in one operation. The deleteInBatch
is going to batch several delete-statements and delete them as 1 operation.
该delete
方法将在一次操作中删除您的实体。在deleteInBatch
即将批量删除几个语句和删除它们为1个操作。
If you need a lot of delete operations the batch-deletion might be faster.
如果您需要大量删除操作,批量删除可能会更快。
回答by Benjamin Maurer
The answers here are not complete!
这里的答案并不完整!
First off, let's check the documentation!
首先,让我们检查文档!
void deleteInBatch(Iterable<T> entities)
Deletes the given entities in a batch which means it will create a single Query.
So the "delete[All]InBatch" methods will use a JPA Batch delete, like "DELETE FROM table [WHERE ...]". That may be WAY more efficient, but it has some caveats:
因此,“delete[All]InBatch”方法将使用 JPA 批量删除,例如“DELETE FROM table [WHERE ...]”。这可能更有效,但它有一些警告:
- This will not call any JPA/Hibernate lifecycle hooks you might have (@PreDelete)
- It will NOT CASCADE to other entities
- You have to clear your persistence context or just assume it is invalidated.
- 这不会调用您可能拥有的任何 JPA/Hibernate 生命周期钩子 (@PreDelete)
- 它不会级联到其他实体
- 您必须清除持久性上下文或假设它已失效。
That's because JPA will issue a bulk DELETE statement to the database, bypassing the cache etc. and thus can't know which entities were affected.
这是因为 JPA 会向数据库发出批量 DELETE 语句,绕过缓存等,因此无法知道哪些实体受到影响。
The actual codewhere in Spring Data JPA
And while I can't find a specific article here, I'd recommend everything Vlad Mihalceahas written, to gain a deeper understanding of JPA.
虽然我在这里找不到具体的文章,但我会推荐Vlad Mihalcea写的所有内容,以便更深入地了解 JPA。
TLDR: The "inBatch" methods use bulk DELETE statements, which can be drastically faster, but have some caveats bc. they bypass the JPA cache. You should really know how they work and when to use them to benefit.
TLDR:“inBatch”方法使用批量 DELETE 语句,它可以快得多,但有一些警告 bc。它们绕过 JPA 缓存。您应该真正了解它们的工作原理以及何时使用它们来受益。
回答by Evelyn D
deleteInBatch(...)
in the log would look like this:
DELETE FROM table_name WHERE (((((((? = id) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id))
deleteInBatch(...)
在日志中看起来像这样:
DELETE FROM table_name WHERE (((((((? = id) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id)) OR (? = id))
That might leads to a problem if there are a large amount of data to be deleted, which reaches maximum size of the SQL server query: Maximum size for a SQL Server Query? IN clause? Is there a Better Approach
如果要删除的数据量很大,达到 SQL Server 查询的最大大小,这可能会导致问题: SQL Server 查询的最大大小?IN条款?有没有更好的方法