Cost of using subquery vs using same table twice in query
Hi all,
In a current project, I was asked by my supervisor what is the cost difference between the following two methods. First method is using a subquery to get the name field from table2. A subquery is needed because it requires the field sa_id from table1. The second method is using table2 again under a different alias to obtain table2.name. The two table2 are not self-joined. The outcome of these two queries are the same.
Using subquery:
select a.sa_id R1, b.other_field R2,
(select b.name from b
where b.b_id = a.sa_id) R3
from table1 a, table2 b
where ...Using same table twice (table2 under 2 different aliases)
select a.sa_id R1, b.other_field R2, c.name R3
from table1 a, table2 b, table2 c
where
c.b_id = a.sa_id,
and ....Can anyone tell me which version is better and why? (or under what circumstances, which version is better). And what are the costs involved? Many thanks.
pl/sql novice wrote:
Hi all,
In a current project, I was asked by my supervisor what is the cost difference between the following two methods. First method is using a subquery to get the name field from table2. A subquery is needed because it requires the field sa_id from table1. The second method is using table2 again under a different alias to obtain table2.name. The two table2 are not self-joined. The outcome of these two queries are the same.
Using subquery:
Using same table twice (table2 under 2 different aliases)
Can anyone tell me which version is better and why? (or under what circumstances, which version is better). And what are the costs involved? Many thanks.In theory, if you use the scalar "subquery" approach, the correlated subquery needs to be executed for each row of your result set. Depending on how efficient the subquery is performed this could require significant resources, since you have that recursive SQL that needs to be executed for each row.
The "join" approach needs to read the table only twice, may be it can even use an indexed access path. So in theory the join approach should perform better in most cases.
Now the Oracle runtime engine (since Version 8) introduces a feature called "filter optimization" that also applies to correlated scalar subqueries. Basically it works like an in-memory hash table that caches the (hashed) input values to the (deterministic) correlated subquery and the corresponding output values. The number of entries of the hash table is fixed until 9i (256 entries) whereas in 10g it is controlled by a internal parameter that determines the size of the table (and therefore can hold different number of entries depending on the size of each element).
If the input value of the next row corresponds to the input value of the previous row then this optimization returns immediately the corresponding output value without any further action. If the input value can be found in the hash table, the corresponding output value is returned, otherwise execute the query and keep the result combination and eventually attempt to store this new combination in the hash table, but if a hash collision occurs the combination will be discarded.
So the effectiveness of this clever optimization largely depends on three different factors: The order of the input values (because as long as the input value doesn't change the corresponding output value will be returned immediately without any further action required), the number of distinct input values and finally the rate of hash collisions that might occur when attempting to store a combination in the in-memory hash table.
In summary unfortunately you can't really tell how good this optimization is going to work at runtime and therefore can't be properly reflected in the execution plan.
You need to test both approaches individually because in the optimal case the optimization of the scalar subquery will be superior to the join approach, but it could also well be the other around, depending on the factors mentioned.
Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/
Similar Messages
-
Use same table twice in subform
Hi,
i´ve created a form like this
subformA -> positioned
--subformB -> flowed
table
header
body row
Everything is working fine and a I´m able to see the expected data.
But when my table includes more than 10 rows it destroys the layout of my page.
My idea:
Create a second subform with same table data and place it right beside it. And using FormCalc to show data from row 11 to end.
But only the first subform is filled. There is no data in second subform (FormCalc code is not implemented).
It seems, that I can´t use same tabele twice.
Any ideas?
Regards
AndyHi Andy,
Have both the subforms as flowed and page breaked, it should work.
As per as having 2 tables techically i believe its not possible.reason behind.
When you do data binding for 1st table and say multiple rows, at runtime data is iterated and displayed in the 1st table.
when it reaches to the 2nd table the pointer on the table data source is already eof i.e., last record so it will not display it again.
to acheive this on form load of the 2nd table you need to initialize and iterate through the data source again.
I have never tested this scenario but read it in some adobe fourms.
Check it and let me know, if its still not working i can design a sample form with this scenario for you.
Cheers,
Sai -
Upgrading to Mountain Lion using free upgrade, download froze halfway, tried again and unable to use same code twice. Is there a way I can get a new code without having to submit other information (scan/photo of proof of purchase)?
Open the App Store and navigate to the Purchases tab. You should see Mountain Lion there. You can click there to restart the download.
-
Inserting to a table using same table and master table
[code]
I have a table test_master where I have stored the status of the individual claims ; the status would be changed quarterly basis.
I have another table test_detail which will store status chages on each quarter.The curr_status column will store the status from test_master; and prev_status will be the curr_status for the previous trans_id from test_detail table itself
create table test_master( claim_id number,
status varchar2(10));
insert into test_main VALUES (100, 'L1');
insert into test_main VALUES (101, 'L2'); ---- first quarter data.. it will be purged at the begining of second quarter
insert into test_main VALUES (102, 'L1');
insert into test_main VALUES (103, 'L3');
insert into test_main VALUES (100, 'L2');
insert into test_main VALUES (101, 'L2'); ---- second quarter data.. it will be purged at the begining of third quarter
insert into test_main VALUES (102, 'L1');
insert into test_main VALUES (103, 'L4');
insert into test_main VALUES (100, 'L4');
insert into test_main VALUES (101, 'L5'); ---- third quarter data.. it will be purged at the begining of fourth quarter
insert into test_main VALUES (102, 'L6');
insert into test_main VALUES (103, 'L7');
create table test_detail( trans_id number,
claim_id number,
prev_status varchar(20),
curr_status varchar2(21
prev_status = curr_status of the previous id for same claim_id s
curr_status = status from TEST_MASTER
how to write an insert statement to insert records in TEST_DETAIL to have below expected data?
id claim_id PREV_STATUS CURR_STATUS
1 100 null L1 ---prev_status will be null for the first run
1 101 null L2
1 102 null L1
1 103 null L3
2 100 L1 L2
2 101 L2 L2
2 102 L1 L1
2 103 L3 L4
3 100 L2 L4
3 101 L2 L5
3 102 L1 L6
3 103 L4 L7
[/code]
Thanks
-AnnHi,
Do you really need to store prev_status in test_detail? You can always compute it on the fly, using the analytic LAG function, and you won't have to worry about re-computing it when test_detail is updated.
If test_detail will never be updated, and performance is important, then you may want to store prev_status. Here's one way to do that.
When you're ready to archive the data from test_main, then run this INSERT statement:
INSERT INTO test_detail (trans_id, claim_id, prev_status, curr_status)
SELECT &2, m.claim_id, d.curr_status, m.status
FROM test_main m
LEFT OUTER JOIN test_detail d ON d.claim_id = m.claim_id
AND d.trans_id = &1
Where &1 is the previous trans_id (the highest value already in test_detail), and &2 is the new trans_id (&1 + 1).
The first time you run this, when test_detail is empty, it doesn't matter what value you use for &1; you can use 0.
After you've confirmed that this worked, then you can start putting new data into test_main.
I assume that test_master and test_main are the same table. -
Namedquery using same table field multiple times with the use of a label
Hi all,
i'm having some trouble with a namedquery. I'm trying to
use the following namedquery in Toplink to retrive some
data out of a database.
select proj.id
, proj.code
, proj.name
, proj.budget
, proj.status
, proj.startdate
, proj.enddate
, proj.mdr_id projleader_id
, med_leader.name projleader
, proj.mdr_id_valt_onder promanager_id
, med_promanager.name promanager
, proj.mdr_id_is_account_from accmanager_id
, med_accmanager.name accmanager
from uur_projecten proj
, uur_medewerkers med_leader
, uur_medewerkers med_promanager
, uur_medewerkers med_accmanager
where ( #p_name is not null or #p_search_string is not null )
and med_leader.id = proj.mdr_id
and ( proj.mdr_id = nvl( #p_name, proj.mdr_id )
or proj.mdr_id_valt_onder = nvl( #p_name, proj.mdr_id )
or proj.mdr_id_is_account_van = nvl( #p_name, proj.mdr_id ))
and (( #p_status is not null
and substr( proj.status, 1, 1 ) = upper( #p_status ))
or ( #p_status is null ))
and ( upper( proj.code ) like upper( '%' || #p_search_string || '%' )
or upper( proj.name ) like upper( '%' || #p_search_string || '%' ))
and med_promanager.id = proj.mdr_id_valt_onder
and med_accmanager.id = proj.mdr_id_is_account_van
order by decode( substr( proj.status, 1, 1 )
, 'A', 2, 'T', 3, 'F', 4, 1 ), proj.code desc
As you all can see the table ‘uur_medewerkers’ is been used trice to
determine the name for the corresponding ID. I have a Java class with
the fields for the results and created a Toplink descriptor to map
the fields to the database fields.
The problem is that for the 'projleader', 'promanager' and 'accmanager'
fields the results are null. The reason is probably that Toplink doesn't
recognize the fields because of the label for the tables.
Is there a way to make this work?
Greets, RenéPost Author: quafto
CA Forum: .NET
Your query is not too clear so I'll do my best to answer it broadly.
You mentioned that you have a .NET web application where your users enter data on one screen and then may retrieve it on another. If the data is written in real time to a database then you can create a standard Crystal Report by adding multiple tables. The tables should be linked together using the primary and foreign keys in order to optimize the database query and give you a speedy report. Using unlinked tables is not recommended and requires the report engine to index the tables (it is quite slow).
You also mentioned you have a "PropID" to be used in a WHERE clause. This is a great place to use a parameter in your report. This parameter can then be used in your record selection formula inside Crystal Reports. The report engine will actually create the WHERE clause for you based on the parameter value. This is helpful because it allows you to simply concentrate on your code rather than keeping track of SQL queries.
Now, what Crystal does not do well with is uncertainty. When you design a report with X number of tables the report engine expects X number of tables to be available at processing time. You should not surprise the print engine with more or less tables because you could end up with processing errors or incorrect data. You may need to design multiple reports for specific circumstances.
Regarding the group expert question. I'm not sure how you would/could use the group expert to group a table? A table is a collection of fields and cannot be compared to another table without a complex algorithm. The group expert is used to group and sort records based on a field in the report. Have a look at the group expert section of the help file for more information.
Hopefully my comments have given you a few ideas. -
I want to use the same image twice, one in colour, the other black and white. But when I use the monochrome mixer, both images in the book turn to b/w. Do I have to import two separate images?
Using the right click menu (or shortcut key) duplicate the version - using the top stack menu extract item from the stack, make it b+w, drag it to the book and viola.
RB -
UPDATE involving the same table in sub query
DB version: 11.2
We have a table called SHP_GC_TRACK which has around 8 million records with partitions . In the below UPDATE, it is updating a column based on the SELECT on the same table in a subquery.
UPDATE shp_gc_track a
SET f_tran_proc = 'Y'
WHERE last_update_date <
(SELECT MAX (last_update_date)
FROM shp_gc_track b
WHERE a.shp_trx_rowid = b.shp_trx_rowid
AND a.c_shp_inst = b.c_shp_inst
AND a.f_tran_proc = b.f_tran_proc
AND b.f_ltr_received = 'D'
AND f_rec_code IN ('G', 'W')
AND b.f_rec_status = 'B'
AND b.c_shp_inst = :b1
AND a.c_shp_inst = :b1
AND a.f_ltr_received = 'D' -----------------> part of composite index
AND a.f_tran_proc = 'N' -----------------> part of composite index
AND a.f_rec_code IN ('G', 'W') --------------> part of composite index
AND a.f_rec_status = 'B'; -----------------> part of composite index This UPDATE takes a long time to execute and sometime get hung.
We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status, f_tran_proc . Explain plan shows this composite index is being used.
Is there anyway to rewrite this query or any other suggestions ?Steve_74 wrote:
DB version: 11.2
We have a table called SHP_GC_TRACK which has around 8 million records with partitions . In the below UPDATE, it is updating a column based on the SELECT on the same table in a subquery.
UPDATE shp_gc_track a
SET f_tran_proc = 'Y'
WHERE last_update_date <
(SELECT MAX (last_update_date)
FROM shp_gc_track b
WHERE a.shp_trx_rowid = b.shp_trx_rowid
AND a.c_shp_inst = b.c_shp_inst
AND a.f_tran_proc = b.f_tran_proc
AND b.f_ltr_received = 'D'
AND f_rec_code IN ('G', 'W')
AND b.f_rec_status = 'B'
AND b.c_shp_inst = :b1
AND a.c_shp_inst = :b1
AND a.f_ltr_received = 'D' -----------------> part of composite index
AND a.f_tran_proc = 'N' -----------------> part of composite index
AND a.f_rec_code IN ('G', 'W') --------------> part of composite index
AND a.f_rec_status = 'B'; -----------------> part of composite index This UPDATE takes a long time to execute and sometime get hung.
We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status, f_tran_proc . Explain plan shows this composite index is being used.
Is there anyway to rewrite this query or any other suggestions ?Tuning updates with subqueries can be hard :(. Sadly my suggestions below are of the try-it-and-see-what-happens variety - nothing certain
First, check the index. Is it bitmap or b-tree? If b-tree see if the most restrictive columns are listed first - this can help with b-tree index efficiency. Also if b-tree a composite bitmap for columns with lots of repeating values instead might help
Its a correlated subquery so you can't just run the subquery first putting the result into a scalar varaiable and using the variable in the SQL instead. You can try putting the results of the subuqery w/join keys in a GTT first using the GTT in the SQL to see if I/O is reduced overall during both operations.
Do you have the licence for the parallel query option? Using parallel DML (this must be turned on manually) might help. Check the docs for the ALTER SESSION command to do this. Also, the PARALLEL_INDEX() hint might help
Post the execution plan of the SQL -
Select another column in same table in CF query
Hello..
I'm curious as to the best way to run this query..
When my CF template page loads I would like it to grab a number value in one column and match it up to the table ID in the same table and display the name for that ID.
Do I have to make another table to do this...? QoQ's?
Thanks!Hi Dan..
Sorry I wasnt clearer.. here is a picture.
Thanks for the help! -
Selecting same column twice makes query slow
Hello,
Has anybody any idea why following statement is slow when selecting the
DATA_TYPE column twice?
SELECT S.OWNER,
S.SYNONYM_NAME,
UCOL.COLUMN_NAME,
UCOL.DATA_TYPE,
UCOL.DATA_TYPE,
UCOL.DATA_LENGTH,
COLUMN_ID
FROM ALL_SYNONYMS S,
ALL_TAB_COLUMNS UCOL
WHERE S.TABLE_OWNER = UCOL.OWNER
AND S.TABLE_NAME = UCOL.TABLE_NAME
and S.owner = 'REPORTING_TEST'
and S.SYNONYM_NAME = 'OV_COMMERCIAL_ENTITY_JN'
Replace the owner and table name with one of your own.
If you take one of the DATA_TYPE's out the select is much faster.
Why, I hear you ask, do you select the same column twice?
Well, it's not me. It's BusinessObjects when it does a structure refresh it
produces a statement similar to the one above except that one of the
DATA_TYPE's is a SUBSTR + DECODE (however they in themselves
have virtually no impact on the statement). I just find it weird that repeating
a column twice can have such a dramatic impact on performance.
I'm not a tuning expert and have looked at EXPLAIN PLAN outputs but can't
make head nor tail of them.
Thanks,
RuudYes it's true that Oracle caches data but I did run both statements several times within the same session. Run statement 1, run statement 2, add 2nd column and run again, take column away and run again, etc. Always the results were the same. The query with duplicate columns ran in seconds, the one without in milliseconds.
If it tells you anything, below both explain plans. The main difference that I noticed is the full table scan on USER$ for the first query. The second one (although a more elaborate execution plan) seems to use indexes more.
Don't spend too much time on this. I've found a workaround where I create temporary tables for ALL_SYNONYMS and ALL_TAB_COLUMNS and re-direct the public synonyms to point at those. After I've done what I wanted to do with BusinessObjects, I restore the original public synonyms. Next thing I'll try is rebuilding my database from scratch and see if the problem is still there.
Apologies if the listings come out in variable font (how do you paste fixed font?).
=== SQL1 ====================================================
EXPLAIN PLAN FOR
SELECT UCOL.COLUMN_NAME,
UCOL.DATA_TYPE,
UCOL.DATA_TYPE
FROM ALL_SYNONYMS S,
ALL_TAB_COLUMNS UCOL
WHERE S.TABLE_OWNER = UCOL.OWNER
AND S.TABLE_NAME = UCOL.TABLE_NAME
AND S.owner = 'REPORTING_TEST'
AND S.SYNONYM_NAME = 'OV_COMMERCIAL_ENTITY_JN'
Explained.
Elapsed: 00:00:00.04
SQL> @E:\Oracle\Product\10.2.0\db_1\RDBMS\ADMIN\utlxpls.sql
Plan hash value: 1692851197
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS | |
| 2 | NESTED LOOPS | |
| 3 | NESTED LOOPS | |
| 4 | TABLE ACCESS BY INDEX ROWID | USER$ |
|* 5 | INDEX UNIQUE SCAN | I_USER1 |
| 6 | VIEW | ALL_TAB_COLUMNS |
|* 7 | FILTER | |
| 8 | NESTED LOOPS OUTER | |
| 9 | NESTED LOOPS OUTER | |
| 10 | NESTED LOOPS OUTER | |
| 11 | NESTED LOOPS OUTER | |
| 12 | NESTED LOOPS | |
| 13 | NESTED LOOPS | |
| 14 | TABLE ACCESS FULL | USER$ |
| 15 | TABLE ACCESS BY INDEX ROWID| OBJ$ |
|* 16 | INDEX RANGE SCAN | I_OBJ2 |
|* 17 | TABLE ACCESS CLUSTER | COL$ |
|* 18 | INDEX UNIQUE SCAN | I_OBJ# |
|* 19 | TABLE ACCESS CLUSTER | COLTYPE$ |
|* 20 | INDEX RANGE SCAN | I_HH_OBJ#_INTCOL# |
|* 21 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 22 | INDEX RANGE SCAN | I_OBJ3 |
| 23 | TABLE ACCESS CLUSTER | USER$ |
|* 24 | INDEX UNIQUE SCAN | I_USER# |
|* 25 | TABLE ACCESS CLUSTER | TAB$ |
|* 26 | INDEX UNIQUE SCAN | I_OBJ# |
| 27 | NESTED LOOPS | |
| 28 | FIXED TABLE FULL | X$KZSRO |
|* 29 | INDEX RANGE SCAN | I_OBJAUTH2 |
|* 30 | FIXED TABLE FULL | X$KZSPR |
|* 31 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 32 | INDEX RANGE SCAN | I_OBJ2 |
|* 33 | TABLE ACCESS BY INDEX ROWID | SYN$ |
|* 34 | INDEX UNIQUE SCAN | I_SYN1 |
Predicate Information (identified by operation id):
5 - access("U"."NAME"='REPORTING_TEST')
7 - filter(("O"."TYPE#"=3 OR "O"."TYPE#"=4 OR "O"."TYPE#"=2 AND NOT
EXISTS (SELECT 0 FROM "SYS"."TAB$" "T" WHERE "T"."OBJ#"=:B1 AND
(BITAND("T"."PROPERTY",512)=512 OR BITAND("T"."PROPERTY",8192)=8192)))
AND ("O"."OWNER#"=USERENV('SCHEMAID') OR EXISTS (SELECT 0 FROM
"SYS"."OBJAUTH$" "OBJAUTH$",SYS."X$KZSRO" "X$KZSRO" WHERE "OBJ#"=:B2
AND "GRANTEE#"="KZSROROL") OR EXISTS (SELECT 0 FROM SYS."X$KZSPR"
"X$KZSPR" WHERE "INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45)
OR (-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))))
16 - access("O"."OWNER#"="U"."USER#")
17 - filter(DECODE("C"."PROPERTY",0,'NO',DECODE(BITAND("C"."PROPERTY",
32),32,'YES','NO'))='NO')
18 - access("O"."OBJ#"="C"."OBJ#")
19 - filter("C"."INTCOL#"="AC"."INTCOL#"(+))
20 - access("C"."OBJ#"="H"."OBJ#"(+) AND
"C"."INTCOL#"="H"."INTCOL#"(+))
21 - filter("OT"."TYPE#"(+)=13)
22 - access("AC"."TOID"="OT"."OID$"(+))
24 - access("OT"."OWNER#"="UT"."USER#"(+))
25 - filter(BITAND("T"."PROPERTY",512)=512 OR
BITAND("T"."PROPERTY",8192)=8192)
26 - access("T"."OBJ#"=:B1)
29 - access("GRANTEE#"="KZSROROL" AND "OBJ#"=:B1)
30 - filter("INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45) OR
(-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))
31 - filter("O"."TYPE#"=5)
32 - access("O"."OWNER#"="U"."USER#" AND
"O"."NAME"='OV_COMMERCIAL_ENTITY_JN')
33 - filter("S"."NAME"="UCOL"."TABLE_NAME" AND
"S"."OWNER"="UCOL"."OWNER")
34 - access("O"."OBJ#"="S"."OBJ#")
Note
- rule based optimizer used (consider using cbo)
=== SQL2 ====================================================
EXPLAIN PLAN FOR
SELECT UCOL.COLUMN_NAME,
UCOL.DATA_TYPE
FROM ALL_SYNONYMS S,
ALL_TAB_COLUMNS UCOL
WHERE S.TABLE_OWNER = UCOL.OWNER
AND S.TABLE_NAME = UCOL.TABLE_NAME
AND S.owner = 'REPORTING_TEST'
AND S.SYNONYM_NAME = 'OV_COMMERCIAL_ENTITY_JN'
Explained.
SQL>
SQL> @E:\Oracle\Product\10.2.0\db_1\RDBMS\ADMIN\utlxpls.sql
Plan hash value: 3285788911
| Id | Operation | Name |
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 2 | INDEX UNIQUE SCAN | I_OBJ1 |
| 3 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 4 | INDEX UNIQUE SCAN | I_OBJ1 |
| 5 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 6 | INDEX UNIQUE SCAN | I_OBJ1 |
| 7 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 8 | INDEX UNIQUE SCAN | I_OBJ1 |
| 9 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 10 | INDEX UNIQUE SCAN | I_OBJ1 |
|* 11 | FILTER | |
| 12 | NESTED LOOPS OUTER | |
| 13 | NESTED LOOPS OUTER | |
| 14 | NESTED LOOPS OUTER | |
| 15 | NESTED LOOPS OUTER | |
| 16 | NESTED LOOPS | |
| 17 | NESTED LOOPS | |
| 18 | NESTED LOOPS | |
| 19 | NESTED LOOPS | |
| 20 | NESTED LOOPS | |
| 21 | TABLE ACCESS BY INDEX ROWID| USER$ |
|* 22 | INDEX UNIQUE SCAN | I_USER1 |
|* 23 | TABLE ACCESS BY INDEX ROWID| OBJ$ |
|* 24 | INDEX RANGE SCAN | I_OBJ2 |
| 25 | TABLE ACCESS BY INDEX ROWID | SYN$ |
|* 26 | INDEX UNIQUE SCAN | I_SYN1 |
| 27 | TABLE ACCESS BY INDEX ROWID | USER$ |
|* 28 | INDEX UNIQUE SCAN | I_USER1 |
| 29 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 30 | INDEX RANGE SCAN | I_OBJ2 |
|* 31 | TABLE ACCESS CLUSTER | COL$ |
|* 32 | INDEX UNIQUE SCAN | I_OBJ# |
|* 33 | TABLE ACCESS CLUSTER | COLTYPE$ |
|* 34 | INDEX RANGE SCAN | I_HH_OBJ#_INTCOL# |
|* 35 | TABLE ACCESS BY INDEX ROWID | OBJ$ |
|* 36 | INDEX RANGE SCAN | I_OBJ3 |
| 37 | TABLE ACCESS CLUSTER | USER$ |
|* 38 | INDEX UNIQUE SCAN | I_USER# |
|* 39 | TABLE ACCESS CLUSTER | TAB$ |
|* 40 | INDEX UNIQUE SCAN | I_OBJ# |
| 41 | NESTED LOOPS | |
| 42 | FIXED TABLE FULL | X$KZSRO |
|* 43 | INDEX RANGE SCAN | I_OBJAUTH2 |
|* 44 | FIXED TABLE FULL | X$KZSPR |
Predicate Information (identified by operation id):
2 - access("O"."OBJ#"=:B1)
4 - access("O"."OBJ#"=:B1)
6 - access("O"."OBJ#"=:B1)
8 - access("O"."OBJ#"=:B1)
10 - access("O"."OBJ#"=:B1)
11 - filter(("O"."TYPE#"=3 OR "O"."TYPE#"=4 OR "O"."TYPE#"=2 AND NOT
EXISTS (SELECT 0 FROM "SYS"."TAB$" "T" WHERE "T"."OBJ#"=:B1 AND
(BITAND("T"."PROPERTY",512)=512 OR BITAND("T"."PROPERTY",8192)=8192)))
AND ("O"."OWNER#"=USERENV('SCHEMAID') OR EXISTS (SELECT 0 FROM
"SYS"."OBJAUTH$" "OBJAUTH$",SYS."X$KZSRO" "X$KZSRO" WHERE "OBJ#"=:B2
AND "GRANTEE#"="KZSROROL") OR EXISTS (SELECT 0 FROM SYS."X$KZSPR"
"X$KZSPR" WHERE "INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45)
OR (-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))))
22 - access("U"."NAME"='REPORTING_TEST')
23 - filter("O"."TYPE#"=5)
24 - access("O"."OWNER#"="U"."USER#" AND
"O"."NAME"='OV_COMMERCIAL_ENTITY_JN')
26 - access("O"."OBJ#"="S"."OBJ#")
28 - access("S"."OWNER"="U"."NAME")
30 - access("O"."OWNER#"="U"."USER#" AND "S"."NAME"="O"."NAME")
31 - filter(DECODE("C"."PROPERTY",0,'NO',DECODE(BITAND("C"."PROPERTY",
32),32,'YES','NO'))='NO')
32 - access("O"."OBJ#"="C"."OBJ#")
33 - filter("C"."INTCOL#"="AC"."INTCOL#"(+))
34 - access("C"."OBJ#"="H"."OBJ#"(+) AND
"C"."INTCOL#"="H"."INTCOL#"(+))
35 - filter("OT"."TYPE#"(+)=13)
36 - access("AC"."TOID"="OT"."OID$"(+))
38 - access("OT"."OWNER#"="UT"."USER#"(+))
39 - filter(BITAND("T"."PROPERTY",512)=512 OR
BITAND("T"."PROPERTY",8192)=8192)
40 - access("T"."OBJ#"=:B1)
43 - access("GRANTEE#"="KZSROROL" AND "OBJ#"=:B1)
44 - filter("INST_ID"=USERENV('INSTANCE') AND ((-"KZSPRPRV")=(-45) OR
(-"KZSPRPRV")=(-47) OR (-"KZSPRPRV")=(-48) OR (-"KZSPRPRV")=(-49) OR
(-"KZSPRPRV")=(-50)))
Note
- rule based optimizer used (consider using cbo) -
How to update the COST column using another table's column
Dear All,
I have table:
table parts: pno, pname, qoh, price, olevel
table orders: ono, cno, eno, received, shipped
table odetails: ono, pno, qty
view:orders_view: ono, cno, eno, received, shipped,sum(qty*price)order_costview:odetails_view: ono, pno, qty, (qty*price)cost
after I update the price in parts, I need to update COST and ORDER_COST too. The orders_view does not have pno, qty, and price, the odetails_view does not have price, how can I update the COST and ORDER_COST. Please help and Thanks in advance!!!
I wrote the update the price in parts:
create or replace procedure change_price(ppno in parts.pno%type, pprice in parts.price%type) as
begin
update parts
set price = pprice
where pno = ppno;
end;
show errorsthis procedure works fine.
I wrote the trigger:
create or replace trigger update_orders_v
after update of price on parts
for each row
begin
update orders_view
set order_cost = sum(parts.(:new.price)*parts.qty)
where parts.pno = :new.pno;
end;
show errorsIt gives me:Errors for TRIGGER UPDATE_ORDERS_V:
LINE/COL ERROR
3/5 PL/SQL SQL Statement ignored
4/22 PL/SQL ORA-00934: group function is not allowed hereplease help!You could add the columns to the tables and then you would need a trigger to update those columns. However, you could just as easily select the price * qty to get the cost, without creating an additional column or trigger. I have no idea what you might want to do with a global temporary table. I think you need to explain what your purpose is, before any of us can suggest the best method. Since I have already demonstrated an update with a view, I will demonstrate an update with the cost column added to the odetails table and a trigger as you asked about. Notice that you will need triggers on both tables, the one that has qty and the one that has price.
scott@ORA92> create table parts
2 (pno number(5) not null primary key,
3 pname varchar2(30),
4 qoh integer check(qoh >= 0),
5 price number(6,2) check(price >= 0.0),
6 olevel integer)
7 /
Table created.
scott@ORA92> create table odetails
2 (ono number(5),
3 pno number(5) references parts,
4 qty integer check(qty > 0),
5 cost number,
6 primary key (ono,pno))
7 /
Table created.
scott@ORA92> create or replace procedure change_price
2 (ppno in parts.pno%type,
3 pprice in parts.price%type)
4 as
5 begin
6 update parts
7 set price = pprice
8 where pno = ppno;
9 end;
10 /
Procedure created.
scott@ORA92> create or replace trigger update_cost1
2 after insert or update of price on parts
3 for each row
4 begin
5 update odetails
6 set cost = qty * :new.price
7 where pno = :new.pno;
8 end update_cost1;
9 /
Trigger created.
scott@ORA92> show errors
No errors.
scott@ORA92> create or replace trigger update_cost2
2 before insert or update of qty on odetails
3 for each row
4 declare
5 v_price parts.price%type;
6 begin
7 select price
8 into v_price
9 from parts
10 where pno = :new.pno;
11 --
12 :new.cost := :new.qty * v_price;
13 end update_cost2;
14 /
Trigger created.
scott@ORA92> show errors
No errors.
scott@ORA92> insert into parts values (1, 'name1', 1, 10, 1)
2 /
1 row created.
scott@ORA92> insert into odetails values (1, 1, 22, null)
2 /
1 row created.
scott@ORA92> -- starting data:
scott@ORA92> select * from parts
2 /
PNO PNAME QOH PRICE OLEVEL
1 name1 1 10 1
scott@ORA92> select * from odetails
2 /
ONO PNO QTY COST
1 1 22 220
scott@ORA92> -- update:
scott@ORA92> execute change_price (1, 11)
PL/SQL procedure successfully completed.
scott@ORA92> -- results:
scott@ORA92> select * from parts
2 /
PNO PNAME QOH PRICE OLEVEL
1 name1 1 11 1
scott@ORA92> select * from odetails
2 /
ONO PNO QTY COST
1 1 22 242
scott@ORA92> -- select works without extra cost column or trigger:
scott@ORA92> select o.ono, o.pno, o.qty, (o.qty * p.price) as cost
2 from odetails o, parts p
3 where o.pno = p.pno
4 /
ONO PNO QTY COST
1 1 22 242
scott@ORA92> -
Outer/Cartesian Join using same table
Hi All,
I'm needing to write a query, which is kind of like an outer join/cartesian join that essentially tags each customer in my database with a flag indicating whether they have received a particular product for a given year. There are 8 products so for each distinct customer_id in my database I want 8 records, with a 'Yes' or 'No' indicating whether they have received this product or not. Currently, for a given year, if a customer only receives 2 out of the 8 products there will be two records for that individual but I want 8 records for each individual so that each product is shown to either have been received or not received by the customer. This is the table format I'm looking for:
CUSTOMER_ID PRODUCT_CD PRODUCT_RECEIVED_FLAG YEAR
999999999 1 Y 2010
999999999 2 N 2010
999999999 3 N 2010
999999999 4 N 2010
999999999 5 N 2010
999999999 6 N 2010
999999999 7 Y 2010
999999999 8 Y 2010
888888888 1 N 2010
888888888 2 N 2010
888888888 3 Y 2010
888888888 4 Y 2010
888888888 5 N 2010
888888888 6 N 2010
888888888 7 Y 2010
888888888 8 Y 2010
777777777 1 Y 2010
777777777 2 Y 2010
777777777 3 Y 2010
777777777 4 Y 2010
777777777 5 Y 2010
777777777 6 Y 2010
777777777 7 N 2010
777777777 8 N 2010Thanks,
EdI am in good mood today ;) :
with customer as (
select '999999999' customer_id from dual union all
select '888888888' from dual union all
select '777777777' from dual
product as (
select level product_cd from dual connect by level <= 8
orders as (
select '999999999' customer_id,1 product_cd,2010 year from dual union all
select '999999999',7,2010 from dual union all
select '999999999',8,2010 from dual union all
select '888888888',3,2010 from dual union all
select '888888888',4,2010 from dual union all
select '888888888',7,2010 from dual union all
select '888888888',8,2010 from dual union all
select '777777777',1,2010 from dual union all
select '777777777',2,2010 from dual union all
select '777777777',3,2010 from dual union all
select '777777777',4,2010 from dual union all
select '777777777',5,2010 from dual union all
select '777777777',6,2010 from dual union all
select '777777777',7,2010 from dual union all
select '777777777',8,2010 from dual
-- end of on-the-fly data sample
select c.customer_id,
p.product_cd,
nvl2(o.product_cd,'Y','N') product_received_flag,
y.year
from customer c
cross join
product p
cross join
select distinct year
from orders
) y
left join
orders o
on (
c.customer_id = o.customer_id
and
p.product_cd = o.product_cd
and
y.year = o.year
order by y.year,
c.customer_id desc,
p.product_cd
CUSTOMER_ PRODUCT_CD P YEAR
999999999 1 Y 2010
999999999 2 N 2010
999999999 3 N 2010
999999999 4 N 2010
999999999 5 N 2010
999999999 6 N 2010
999999999 7 Y 2010
999999999 8 Y 2010
888888888 1 N 2010
888888888 2 N 2010
888888888 3 Y 2010
CUSTOMER_ PRODUCT_CD P YEAR
888888888 4 Y 2010
888888888 5 N 2010
888888888 6 N 2010
888888888 7 Y 2010
888888888 8 Y 2010
777777777 1 Y 2010
777777777 2 Y 2010
777777777 3 Y 2010
777777777 4 Y 2010
777777777 5 Y 2010
777777777 6 Y 2010
CUSTOMER_ PRODUCT_CD P YEAR
777777777 7 Y 2010
777777777 8 Y 2010
24 rows selected.
SQL> SY.
P.S. Code assumes at least one customer ordered at least one product within each year. Otherwise you will need year table.
Edited by: Solomon Yakobson on Oct 28, 2011 2:36 PM -
Use of #tables in partition query definition
hi everyone,
I have a cube with 10 partitions i am trying to use the query logic in each partition something like below
create table #tmp1
insert into #tmp1(col1) select col1 from bags where month=01
create table #tmp2
insert into #tmp2(col2)
select col1 from books where month=01
create index on #tmp1
create index on #tmp2
select * from #tmp1 a inner join #tmp2 b on a.col1=b.col1
the month in where conditions get changes for different partitions.
error ::
The SQL syntax is not valid. The relational database returned the following error message: The metadata could not be determined because statement 'SELECT col1 uses a temp table.
please suggest any other alternatives. apart from usage of physical tables
Thanks
Praxykj
PraxyI am using view already in the partition query. Rephrasing the queries used as shown below.
create table #tmp1
insert into #tmp1(col1) select
col1 from Vwbags where month=01
create table #tmp2
insert into #tmp2(col2)
select col1 from Vwbooks where month=01
create index on #tmp1
create index on #tmp2
select * from #tmp1 a inner join #tmp2 b on a.col1=b.col1
Praxy -
Plz Help : Can use drop table in dataset query...?
Can I use this script in my dataset Query in SSRS 2008 R2?
Hi NafisehPanahi,
Reporting Services provides both a graphical query designer and a text-based query designer for creating queries to retrieve data from a relational database for a report dataset in Report Designer.
The text-based query designer does not preprocess the query and can accommodate any kind of query syntax, therefore, you can select “Edit as Text” to type the same command to SQL Server Management Studio (SSMS).
The graphical query designer supports three types of query commands: Text which supports standard Transact-SQL query text for relational database data sources, StoredProcedure, or TableDirect.
For the details, please see the links below:
Graphical Query Designer User Interface
Text-based Query Designer User Interface
Regards,
Heidi Duan
Heidi Duan
TechNet Community Support -
Runtime error using range table in select query
I have to select tcodes from table tstc, based on the entries in ust12,
the entries in ust12-von and ust12-bis, these contains wild charcters also, and i have to selct all the tcodes from von to bis.
so ia m preparing a range table for the entries in ust12, and querying table tstc, then i am getting a runtime error with following description.
'If the problem occurred because an excessively large table was used
in an IN itab construct, you can use FOR ALL ENTRIES instead.'
but how can i use FOR ALL ENTRIES here, because if von = A* and bis = AB*,then i ahve to read all the entries from AAAA till ABZZ (may be something like this),
is there any way to write this query, with out runtime error.
there are total 15000 entries in ust12, i am preparing range table for 3000 entries each and querying tstc.
Thanks in advance
Best Regards
Amarender Reddy BHi,
first write a select on ust12 based on ust12-von and ust12-bis.
eg: select von bis from ust12 into table gt_ust12
where von LIKE 'A%'
and bis LIKE 'AB%'.
now write another select for tstc for all entries in gt_ust12...
Hope it helps
Regards,
Pavan -
Using same cell twice in one formula
Example: (B2+B3)*(B2/B4)
If I want to use B2 twice in the same formula, numbers does not accept the second entry of B2. Any solution to this irritating problem?Geert,
If you are clicking on cells to enter them as references in an expression, you must hold down the Command (Apple) key if you want to enter (click on) the same cell a second time in the same expression. Of course you are always able to type the reference into the Formula Bar entry area as many times as you wish without any special gestures.
Jerry
Maybe you are looking for
-
ADC and The G3 Question about Monitors
My G3 Blue and White PowerMac is running great running OS X but my clunky monitor is starting to take up alot of desk space. I would like to add a 15" ADC Monitor to it (just to keep my apple apple). I currently know that I need the DVI to ADC adapte
-
Is Robohelp 5x compatable with Windows 7
Is Robohelp 5x compatable with Windows 7 - what will happen if I upgrade to Windows 7
-
hi, i have to get in field the date of six month ago what is the best way to do that? example. if now_date = 14.10.07 i wont in l_date 14.4.07 or if now_date = 15.1.2003 l_date = 15.7.2002 Regards
-
Hi Why in the following bit of code does the window not close when I close the frame, I understand why it closes and end's when I take the "open" option on the menu. I want to know why to get a better undersstanding as opposed to the solution. import
-
Dreamweaver 8 install on Mac OSX 10.6 Snow Leopard
Hi all, I have just done a fresh install of Mac OSx Snow Leopard (10.6) and am trying to install our copy of Dreamweaver 8 onto it. However I have now run into a few problems. Firstly it came up with an error message stating that some old format is n