不同的查询还是一个?哪一个是可行的?
Different queries or One? Which one is feasible?
我有以下查询,我想删除行政部门的 3 名员工。
Delete from Employee_tbl where emp_Id = 123 and emp_dep = 'Admin';
Delete from Employee_tbl where emp_Id = 456 and emp_dep = 'Admin';
Delete from Employee_tbl where emp_Id = 789 and emp_dep = 'Admin';
我正在考虑为此编写一个查询 -
Delete from Employee_tbl where emp_Id in (123, 456, 789) and emp_dep = 'Admin';
考虑到性能和所有方面,这是编写此查询的正确方法吗?我很迷惑。
如有任何解释,我们将不胜感激。
两种查询各有利弊
1) 多个 SELECT。
我宁愿使用绑定变量重写查询。
Delete from Employee_tbl where emp_Id = :l_emp_id and emp_dep = :l_emp_dep;
并且运行它多次。
这样,oracle 就不会每次都硬解析查询,并且 Oracle 会重新使用解释计划来减少共享全局区域 (SGA) 中的闩锁 activity,从而最大限度地减少 CPU 的使用。
来自 Bind variables - The key to application performance
的更多详细信息
但这里的一个小问题是,实际上每次执行查询都需要从客户端切换到 SQL 引擎进程。(上下文切换,这又是一个代价高昂的过程)
为了覆盖它,我们使用 bulk binding
。但并非所有客户都支持这一点。
2) 使用 IN 或 Temp 的单个查询 tables/CTE.
此查询仅取决于 DB intelligence
本身。对于开发人员来说,这很简单。最近的优化器通过内部巧妙地处理它们,如果需要,使用 IN-list iterators
或临时表。
另一个好处是,您 运行 查询一次,操作只需要往返数据库一次。
让数据库决定负载。
我有以下查询,我想删除行政部门的 3 名员工。
Delete from Employee_tbl where emp_Id = 123 and emp_dep = 'Admin';
Delete from Employee_tbl where emp_Id = 456 and emp_dep = 'Admin';
Delete from Employee_tbl where emp_Id = 789 and emp_dep = 'Admin';
我正在考虑为此编写一个查询 -
Delete from Employee_tbl where emp_Id in (123, 456, 789) and emp_dep = 'Admin';
考虑到性能和所有方面,这是编写此查询的正确方法吗?我很迷惑。
如有任何解释,我们将不胜感激。
两种查询各有利弊
1) 多个 SELECT。
我宁愿使用绑定变量重写查询。
Delete from Employee_tbl where emp_Id = :l_emp_id and emp_dep = :l_emp_dep;
并且运行它多次。
这样,oracle 就不会每次都硬解析查询,并且 Oracle 会重新使用解释计划来减少共享全局区域 (SGA) 中的闩锁 activity,从而最大限度地减少 CPU 的使用。
来自 Bind variables - The key to application performance
的更多详细信息但这里的一个小问题是,实际上每次执行查询都需要从客户端切换到 SQL 引擎进程。(上下文切换,这又是一个代价高昂的过程)
为了覆盖它,我们使用 bulk binding
。但并非所有客户都支持这一点。
2) 使用 IN 或 Temp 的单个查询 tables/CTE.
此查询仅取决于 DB intelligence
本身。对于开发人员来说,这很简单。最近的优化器通过内部巧妙地处理它们,如果需要,使用 IN-list iterators
或临时表。
另一个好处是,您 运行 查询一次,操作只需要往返数据库一次。
让数据库决定负载。