Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: Need help with query which runs faster in SQL than ORACLE
Thanks Gints
This is great the query ran instantaneously for 30,000 rows all of user B.
But I want to order each partition such that first user (which i specify in the first union clause ) comes to the top, if it does not exists for a particluar partion then the second user's row come up. Thats is the effect I get with this query.
select * from UNION_TEST WHERE USER_NAME = 'A'
UNION
select * from UNION_TEST WHERE USER_NAME = 'B' AND USER_ID NOT IN
(SELECT USER_ID FROM UNION_TEST WHEREUSER_NAME = 'A') By the way I wonder why this query ran slower on Oracle than SQL. In SQL this problem is not noticible.
This is Oracle's Query Plan
7 border=0 SELECT STATEMENT 6 border=0 SORT (UNIQUE) 5 border=0 UNION-ALL 1 border=0 TABLE ACCESS (FULL), MARS_IN_ACCOUNTS (SYS) 4 border=0 FILTER 2 border=0 TABLE ACCESS (FULL), MARS_IN_ACCOUNTS (SYS) 3 border=0 TABLE ACCESS (FULL), MARS_IN_ACCOUNTS (SYS)
And this is SQL Server query plan
|--Hash Match(Union)
|--Table Scan(OBJECT:([MARS_QA].[dbo].[UNION_TEST]),
WHERE:([UNION_TEST].[USER_NAME]='A'))
|--Hash Match(Aggregate, HASH:([UNION_TEST].[USER_ID])
DEFINE:([UNION_TEST].[USER_NAME]=ANY([UNION_TEST].[USER_NAME])))
|--Nested Loops(Left Anti Semi Join,
WHERE:([UNION_TEST].[USER_ID]=[UNION_TEST].[USER_ID]))
|--Table Scan(OBJECT:([MARS_QA].[dbo].[UNION_TEST]), WHERE:([UNION_TEST].[USER_NAME]='B')) |--Table Spool |--Table
Faisal Mansoor
Gints Plivna wrote:
> faisal.mansoor_at_gmail.com wrote:
> > Hello All
> >
> > I am having performance problems with the following query. For the
> > following data
> >
> > TABLE: UNION_TEST
> > user_name, user_id
> > A 1
> > A 2
> > A 3
> > B 2
> > B 4
> >
> > For retrieving all rows for user A and those rows of user B for which
> > user A does not have a user_id. I have written following query for
> > this.
> >
> > select * from UNION_TEST WHERE USER_NAME = 'A'
> > UNION
> > select * from UNION_TEST WHERE USER_NAME = 'B' AND USER_ID NOT IN
> > (SELECT USER_ID FROM UNION_TEST WHERE
> > USER_NAME = 'A')
> >
> > which produces the following desired output.
> >
> > A 1
> > A 2
> > A 3
> > B 4
> >
> > But for large data set this query is running very slow.
> >
> > //Create table
> > create table UNION_TEST
> > (
> > USER_NAME VARCHAR2(50),
> > USER_ID NUMBER(22)
> > );
> >
> > //insert 30000 rows for A
> > BEGIN
> > FOR i in 1 .. 30000
> > LOOP
> > insert into UNION_TEST VALUES('A', i);
> > end LOOP;
> > END;
> >
> > //RUN
> > select * from UNION_TEST WHERE USER_NAME = 'A'
> > UNION
> > select * from UNION_TEST WHERE USER_NAME = 'B' AND USER_ID NOT IN
> > (SELECT USER_ID FROM UNION_TEST WHERE
> > USER_NAME = 'A')
> >
> > Even now the query runs in resonable time. But if I ran this query
> >
> > UPDATE UNION_TEST SET USER_NAME = 'B'
> >
> > Now this query takes more than 30 min to execute.
> >
> > For SQL Server both scenarios take alomst same time.
> >
> > Can any please explain this problem and recommend how can rewrite the
> > query to optimize it.
> >
> > Thanks
> > Faisal
>
>
>
![]() |
![]() |