Hibernate还是SQL进行删除操作?
我需要根据某个键删除表中的条目,这些条目可能达到 500 万左右。有两种方法可以解决这个问题。一种使用 Hibernate,另一种通过直接 SQL 查询。
Hibernate:
List<Employee> empList =
getHibernateTemplate()
.findByNamedParam("from Employee emp where emp.Id=:empId","empId",employeeId);
Iterator<Employee> empIter = empList .iterator();
while(empIter.hasNext()) {
Employee empTran = empIter.next();
getHibernateTemplate().delete("Employee", empTran);
SQL:
delete from Employee where Id = employeeId";
两者中哪一个会更快得到结果? Hibernate 查询可以进一步调整吗?
(如有语法错误请忽略)
I need to delete entries from a table based on a key, and these entries might come to around 5 million. There are two ways to go about it. One using Hibernate and the other by direct SQL query.
Hibernate:
List<Employee> empList =
getHibernateTemplate()
.findByNamedParam("from Employee emp where emp.Id=:empId","empId",employeeId);
Iterator<Employee> empIter = empList .iterator();
while(empIter.hasNext()) {
Employee empTran = empIter.next();
getHibernateTemplate().delete("Employee", empTran);
SQL:
delete from Employee where Id = employeeId";
Which among the two will get the results faster? And can the Hibernate query be tuned further?
(Please ignore syntax errors if any)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个肯定会更快,但有显着差异,因为在第一个中,您为每个删除执行一个查询,但在第二个中,您只发送一个查询,而您的 DBMS 处理其余的。
你可以通过Hibernate、HSQL执行第二个。
编辑:
顺便说一句,如果您对每个 ID 运行“DELETE FROM”查询,它几乎会像第一个查询一样慢,除非您不会迭代整个 Employee 记录,这更好:)
使用 SQL 的 IN() 运算符
尝试最小化 SQL 查询执行次数,如果您仍然对性能不满意,请尝试通过改进查询本身而不是编程部分来提高性能。
Second one definitely will be faster with a significant difference because in the first one you execute a query for every deletion but in the second you only send one query and your DBMS handles the rest.
You can execute the second one through Hibernate, HSQL.
Edit:
Btw, if you run your "DELETE FROM" query for every ID it will be slow almost as the first one, except you won't be iterating whole Employee records, which is better :)
Use SQL's IN() operator
Try to minimize your SQL Query executions and after if you are still not satisfied with the performance try to improve the performance by the improving the query itself, not the programming part.