Sql Distinct函数不提供输出
我编写了一个从oracle表中获取记录的查询。但当我使用一个独特的函数时,它会超时,不会给出任何结果。是否有一种替代方法,可以使用一个独特的、仍然可以得到结果的方法 只有Test3可以在没有distinct的情况下工作。在Test2中,我尝试使用子查询,但也不起作用 在该表的示例中,有超过10万行。但我只展示了它的百分之一 表1:Sql Distinct函数不提供输出,sql,oracle,Sql,Oracle,我编写了一个从oracle表中获取记录的查询。但当我使用一个独特的函数时,它会超时,不会给出任何结果。是否有一种替代方法,可以使用一个独特的、仍然可以得到结果的方法 只有Test3可以在没有distinct的情况下工作。在Test2中,我尝试使用子查询,但也不起作用 在该表的示例中,有超过10万行。但我只展示了它的百分之一 表1: id country 100003 Poland PL10005-100001
id country
100003 Poland
PL10005-100001 England
100007 Finland
100005 Sweden
100004 Norway
100002 Chicago
表2:
id country
100003 Poland
100007 Finland
100006 Austria
100004 Norway
100001 India
测试1:
SELECT
distinct
t1.id,
t2.country,
from table1 t1 inner join table2 t2 on t1.id = t2.id
测试2:
Select distinct
id,
country
from
(
select
t1.id,
t2.country,
from table1 t1 inner join table2 t2 on t1.id = t2.id)
测试3:
SELECT
t1.id,
t2.country,
from table1 t1 inner join table2 t2 on t1.id = t2.id
join语句的语法错误。试试这个:
通过使用缓存视图,我能够修复这个独特的视图问题。问题可能不是
独特的。问题可能是您有无数的重复行。如果没有distinct
,Oracle可能会在生成行时返回行,因此到达第一行的时间很快。但是要得到整个结果集需要很长很长的时间
您可以使用以下命令查看是否存在重复项:
select id, count(*)
from table1
group by id
having count(*) > 1
以及:
您可以使用以下方法获得结果集的大小(不生成结果集):
我猜这比你想象的要大得多。您需要找出如何处理重复项以解决性能问题。两个表的ID
列上都有索引吗?另外,country
列上是否有索引?除了语法错误,distinct
有时会大大降低性能。因为在某些情况下,它会导致您以一些不太有效的方式对数据进行排序。确保事先为数据或顺序编制了适当的索引。没有为任何列设置索引。请先尝试索引,或使用包含所有列的分组依据。这将有助于distinct
更好地工作。让我尝试一下,谢谢。感谢您发现此问题,但我的错误是将where改为on,但这是我的查询,对于此,distinct不起作用。
select id, count(*)
from table1
group by id
having count(*) > 1
select id, count(*)
from table2
group by id
having count(*) > 1;
select sum(t1.cnt * t2.cnt)
from (select id, count(*) as cnt
from table1
group by id
having count(*) > 1
) t1 join
(select id, count(*) as cnt
from table2
group by id
having count(*) > 1
) t2
on t1.id = t2.id;