Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> sql optimization question
Hi,
I have a following sql in a pl/sql function against oracle 8i and 9i. However I am only interested to see if count(*) >0 or not for a particular USERID-COMPANYRID pair. This means, after the sql bulk collect into arrays, I will loop through the arrays to do something like
if array_count (i) = 0 Then
Right now this sql will get all the counts (say 165), but I only need one to make decision. So is there a way to optimize the sql (say using rownum=1 somewhere) so that I would get this:
USERID COMPANYRID COUNT(*)
---------- ---------- ----------
15 7 1 35 7 1 90 7 1 293 7 1 320 7 1 434 7 1 535 7 1
SQL> select A.UserId, A.CompanyRid, Count(*) 2 from usercompanyapplications A, (select distinct COMPANYRID,UserId
3 from usercompanyapplications 4 where Application = 1 5 and UserId in (selectRID
8 ) B 9 Where A.UserId = B.UserId 10 and A.CompanyRid = B.CompanyRid11 and A.Application != 1
USERID COMPANYRID COUNT(*)
---------- ---------- ----------
15 7 7 35 7 5 90 7 5 293 7 7 320 7 165 434 7 165 535 7 9
7 rows selected.
Guang
![]() |
![]() |