Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何优化此查询以加快执行时间_Sql_Oracle - Fatal编程技术网

Sql 如何优化此查询以加快执行时间

Sql 如何优化此查询以加快执行时间,sql,oracle,Sql,Oracle,这是我的问题 select case t.type when 'N' then (select count(*) from table10_other) when 'L' then (select count(*) from table11_other) when 'P' then (select count(*) from table12_other) end as nlp, t.* from table t left outer join employee e on e.emp_id

这是我的问题

select case t.type
when 'N' then  (select count(*) from table10_other)
when 'L' then (select count(*) from table11_other)
when 'P' then (select count(*) from table12_other)
end as nlp, t.* 
from table t  
left outer join employee e on e.emp_id = t.emp_id
left outer join table2 t2 on t2.code= t.code and d.year = t.year 
order by e.name

包含ORDER BY part会显著降低我的查询速度。

如果没有ORDER\u BY\u子句,则无法保证多次执行的同一查询将以相同的顺序检索行。 此外,与子查询或聚合查询一起使用时,
orderby
可能会提高性能。以某种排序顺序从数据库检索数据的唯一方法是在查询中包含orderby。没有什么可以替代按排序的
方法。但是,您可以通过使用联接避免子查询,它们将给您带来一些性能提升和期望值。

另外,如果不需要排序顺序,您最终可以使用group by。这也提高了性能,但每次您可能会得到不同的名称顺序。

我认为您的销售有误,因为
从表中选择count(*)
总是会给您相同的答案。我的建议是你需要

select count(*) over (partition by t.type) as nlp
     , t.* 
from table t  
left outer join employee e on e.emp_id = t.emp_id
left outer join table2 t2 on t2.code= t.code and d.year = t.year 
order by e.name
或者如果你只想得到N L p的计数

select case when t.type IN ('N','L','P') THEN 
        count(*) over (partition by t.type)  
       END as nlp
     , t.* 
from table t  
left outer join employee e on e.emp_id = t.emp_id
left outer join table2 t2 on t2.code= t.code and d.year = t.year 
order by e.name

哪种关系数据库管理系统?这并不是全部……如果要按表返回的字段之一排序,是否真的需要表的左联接?@MitchWheat PL/SQL@JohnHC是的,是的works@jaraisyn,更好,但仍然很奇怪。你什么都没做