Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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
Oracle SQL—什么比3快;和exists();在哪里空间?_Sql_Group By_Where_Exists - Fatal编程技术网

Oracle SQL—什么比3快;和exists();在哪里空间?

Oracle SQL—什么比3快;和exists();在哪里空间?,sql,group-by,where,exists,Sql,Group By,Where,Exists,有更好的写作方法吗 Select distinct id_no from revenue_table where (exists (select * from revenue_table i where revenue_type = 'Shipping' and i.id_no = r.id_no) and exists(select * from revenue_table i where revenue_type = 'Reproduction' and

有更好的写作方法吗

Select distinct id_no
from revenue_table
where (exists (select * from revenue_table i 
       where revenue_type = 'Shipping' and i.id_no = r.id_no)
  and exists(select * from revenue_table i 
       where revenue_type = 'Reproduction' and i.id_no = r.id_no)
  and exists(select * from revenue_table i 
       where revenue_type = 'Tape' and i.id_no = r.id_no))
id_no表示一个表单,该表单针对表单上的每个收入项目输入一次表中。同一收入类型可以出现多次。有许多高级函数使用OR逻辑工作,但我似乎找不到任何使用OR和集合论的函数。如果GROUP BY有一些功能来比较一个组(如id_no)和一个集合(装运、复制、磁带),那就太好了


这是否存在?

是的,您可以使用
内部联接来实现:

select distinct r.id_no 
from revenue_table r
inner join revenue_table i1 on i1.id_no = r.id_no and i1.revenue_type = 'Shipping'
inner join revenue_table i2 on i2.id_no = r.id_no and i2.revenue_type = 'Reproduction'
inner join revenue_table i3 on i3.id_no = r.id_no and i3.revenue_type = 'Tape'

另一个选择是

SELECT id_no
FROM   revenue_table
WHERE  revenue_type IN ( 'Tape', 'Shipping', 'Reproduction' )
GROUP  BY id_no
HAVING COUNT(DISTINCT revenue_type) = 3  

您必须测试它是否是一种改进。

许多关系运算符表示逻辑AND,例如连接、限制、扩展、交叉

SQL的
INTERSECT
在这里是合适的,例如

SELECT id_no
  FROM revenue_table
 WHERE revenue_type = 'Shipping' 
INTERSECT
SELECT id_no
  FROM revenue_table
 WHERE revenue_type = 'Reproduction' 
INTERSECT
SELECT id_no
  FROM revenue_table
 WHERE revenue_type = 'Tape';

+1:单次扫描表格,据我所知不会超过这个速度。我运行了一些测试,我相信在运行时,在正在运行的大型表格上,这个速度会加快20%。我以前从未想过使用“count(distinct)”语句。感谢you@Matt-你有关于
(id\u no,revenue\u type)
的索引吗?@Matt-这将产生重大影响。@wildplasser-没有。它计算不同的收入类型,所以这三种类型都必须存在。如果对
id\n、revenue\u type
有唯一的约束,它可以只使用
COUNT(*)
注意,这是等效的,因为存在
DISTINCT
关键字。