|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: how to get the values from the another table if the table has null [message #613497 is a reply to message #613490] |
Wed, 07 May 2014 09:21 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 07 May 2014 10:08
The statement will always return one and only one record so what is the purpose of ROWNUM=1? Materialization?
I think in this case NOT EXISTS is a better choice (no need to count all rows just check if one exists).
ROWNUM = 1 is not for materialization. And no, it will not return one and only one row. It will return 0 or 1. When OP said EMP is NULL I thought he meant empty. Anyway, without ROWNUM = 1 Oracle wil read whole table which can take some time while with ROWNUM = 1 it will stop after fetching first row. And yes, you are right, NOT EXISTS is a better choice. And, in fact, optimizer getting smarter and smarter. It replace it with NOT EXISTS anyway:
SQL> EXPLAIN PLAN FOR
2 SELECT *
3 FROM EMP
4 UNION ALL
5 SELECT *
6 FROM EMP1
7 WHERE 0 = (
8 SELECT COUNT(*)
9 FROM EMP
10 -- WHERE ROWNUM = 1
11 )
12 /
Explained.
SQL> select * from table(dbms_xplan.display)
2 /
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
Plan hash value: 3884441943
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 42 | 1582 | 7 (58)| 00:00:01 |
| 1 | UNION-ALL | | | | | |
| 2 | TABLE ACCESS FULL | EMP | 14 | 546 | 3 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
|* 3 | FILTER | | | | | |
| 4 | TABLE ACCESS FULL| EMP1 | 28 | 1036 | 3 (0)| 00:00:01 |
| 5 | INDEX FULL SCAN | EMP_ONE_PRESIDENT | 1 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
3 - filter( NOT EXISTS (SELECT 0 FROM "EMP" "EMP"))
17 rows selected.
SQL> EXPLAIN PLAN FOR
2 SELECT *
3 FROM EMP
4 UNION ALL
5 SELECT *
6 FROM EMP1
7 WHERE 0 = (
8 SELECT COUNT(*)
9 FROM EMP
10 WHERE ROWNUM = 1
11 )
12 /
Explained.
SQL> select * from table(dbms_xplan.display)
2 /
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
Plan hash value: 4137344972
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 42 | 1582 | 7 (58)| 00:00:01 |
| 1 | UNION-ALL | | | | | |
| 2 | TABLE ACCESS FULL | EMP | 14 | 546 | 3 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
|* 3 | FILTER | | | | | |
| 4 | TABLE ACCESS FULL| EMP1 | 28 | 1036 | 3 (0)| 00:00:01 |
|* 5 | COUNT STOPKEY | | | | | |
| 6 | INDEX FULL SCAN | EMP_ONE_PRESIDENT | 1 | | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
3 - filter( NOT EXISTS (SELECT 0 FROM "EMP" "EMP" WHERE ROWNUM=1))
5 - filter(ROWNUM=1)
19 rows selected.
SQL>
SY.
|
|
|
Re: how to get the values from the another table if the table has null [message #613498 is a reply to message #613497] |
Wed, 07 May 2014 09:36 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
mist598 wrote on Wed, 07 May 2014 15:18Quote:The link between two tables that store (presumably) employee information is deptno?
Yes .
So do emp and xxx_emp both only have 1 record per deptno?
Because if they do they are really badly misnamed.
mist598 wrote on Wed, 07 May 2014 15:18
Quote:Post column lists and unique keys for both tables.
Sorry cookiemonster i have used 5 tables and that are belonging to the custom Tables have more than 100 columns .
So post only the two relevant tables along with the relevant columns.
And the only thing stopping you from posting the details of the unique keys is your usual lazyness. So stop being lazy and post the information requested.
mist598 wrote on Wed, 07 May 2014 15:18
1) Before i have 100 records
2) i added one column in the select stmt and ran the query, i want to display the only 100 records
The above boils down to:
1) I have a query I'm not going to show you.
2) I modified the query in a way I'm not going to tell you.
3) Something happened and I'm not going to tell you what.
|
|
|
Re: how to get the values from the another table if the table has null [message #613499 is a reply to message #613496] |
Wed, 07 May 2014 09:38 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
mist598 wrote on Wed, 07 May 2014 15:21Hi i have 100 records before, after adding the only one column also i want only 100 records.
for that i have taken emp, xxc_emp table joins , but how can i use outer joins in between .
1) i ran the original query i got 100 records
2) after adding the one column , i am getting 86 records only
By specifying an outer-join obviously. Have you bothered to look up the syntax for outer-joins in the documentation?
As usual it has examples.
|
|
|
Re: how to get the values from the another table if the table has null [message #613502 is a reply to message #613497] |
Wed, 07 May 2014 09:43 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Solomon Yakobson wrote on Wed, 07 May 2014 15:21And no, it will not return one and only one row. It will return 0 or 1.
Which is one row. Select aggregate without a group by always returns one row.
Michel misunderstood your intent by thinking of the rownum working in it's usual top-n fashion and limiting the number of rows return, but in this case it limits the number of rows counted.
|
|
|
|
Re: how to get the values from the another table if the table has null [message #613511 is a reply to message #613502] |
Wed, 07 May 2014 10:51 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Quote:Michel misunderstood your intent by thinking of the rownum working in it's usual top-n fashion and limiting the number of rows return, but in this case it limits the number of rows counted.
Only with a smart version of the optimizer which can detect 0=(SELECT COUNT(*)...). I bet if you remove the ROWNUM=1 you have the same thing but can't test it without the test case (and Oracle version) to reproduce.
Otherwise, in the general case, ROWNUM applies to the result set returned by the query, that is after everything but ORDER BY.
SQL Reference 11.2For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.
...
If an ORDER BY clause follows ROWNUM in the same query, then the rows will be reordered by the ORDER BY clause.
In "NOT EXISTS (SELECT 0 FROM "EMP" "EMP" WHERE ROWNUM=1)", the ROWNUM=1 is useless as it is implies in the NOT EXISTS (Oracle obviously stops at the first row it encounters, at least since 3 decades).
|
|
|
Re: how to get the values from the another table if the table has null [message #613513 is a reply to message #613511] |
Wed, 07 May 2014 10:56 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 07 May 2014 16:51
Quote:Michel misunderstood your intent by thinking of the rownum working in it's usual top-n fashion and limiting the number of rows return, but in this case it limits the number of rows counted.
Only with a smart version of the optimizer which can detect 0=(SELECT COUNT(*)...). I bet if you remove the ROWNUM=1 you have the same thing but can't test it without the test case (and Oracle version) to reproduce.
Otherwise, in the general case, ROWNUM applies to the result set returned by the query, that is after everything but ORDER BY.
I'm not sure I follow. Are you suggesting that SELECT COUNT(*) with and without a rownum restriction will give the same count?
|
|
|
|