Can this query statement be made robust.
select to_char(t1.t_date,'YYYY-MM') as date_used,
SUM(t1.much_amount) as amount,
t1.p_id as type_id
from t1_one
where t1.wo_type like 'NEWYORK'
and t1.p_id = 'CAR'
or t1.p_id = 'BOAT'
or t1.p_id = "PLANE'
or t1.p_id = 'HORSE'
or t1.p_id = 'DOG'
or t1.p_id = 'CAT'
or t1.p_id = 'PIG'
or t1.p_id = 'ELEPHANT'
or t1.p_id = 'TIGER'
or t1.p_id = 'SNAKE'
and trunc(t1.t_date) between to_date('2010'||'-01-01','YYYY-MM-DD') and to_date('2010'||'-12-31', 'YYYY-MM-DD')
group by to_char(t1.t_date,'YYYY-MM'), t1.p_id
{code}
kindly note in my table, i have the following p_id: CAR, BOAT, PLANE, HORSE, DOG, CAT, PIG, ELEPHANT, TIGER, SNAKE, how though, in the near future, i have a feeling those p_id items could be added on, so i need a way to get all the p_id without hardcoding it like the way i did above. I am still new to oracle and learning everyday. thank you.
If you always wanted to limit your query to those 10 names, you could simplify your query a little:
select to_char(t1.t_date,'YYYY-MM') as date_used
,SUM(t1.much_amount) as amount
,t1.p_id as type_id
from t1_one
where t1.wo_type like 'NEWYORK'
and t1.p_id IN ('CAR', 'BOAT', 'PLANE', 'HORSE', 'DOG'
,'CAT', 'PIG', 'ELEPHANT', 'TIGER', 'SNAKE')
and t1.t_date >= to_date('2010-01-01','YYYY-MM-DD')
and t1.t_date < to_date('2011-01-01','YYYY-MM-DD')
group by to_char(t1.t_date,'YYYY-MM'), t1.p_id;If you want to make the query more flexible, store those names in a separate table and use a subquery (or join to it):
select to_char(t1.t_date,'YYYY-MM') as date_used
,SUM(t1.much_amount) as amount
,t1.p_id as type_id
from t1_one
where t1.wo_type like 'NEWYORK'
and t1.p_id IN (select x.p_id from some_table x) ---<<<
and t1.t_date >= to_date('2010-01-01','YYYY-MM-DD')
and t1.t_date < to_date('2011-01-01','YYYY-MM-DD')
group by to_char(t1.t_date,'YYYY-MM'), t1.p_id;Then you just need to manage the table with the list of names and your query won't need to be changed.
Similar Messages
-
How can this query avoid full table scans?
It is difficult to avoid full table scans in the following query because the values of column STATUS reiterant numbers. There are only 10 numbers values for the STATUS column (1..10)
But the table is very large. there are more than 1 million rows in it. A full table scanning consumes too much time.
How can this query avoid full table scans?
Thank you
SELECT SYNC,CUS_ID INTO V_SYNC,V_CUS_ID FROM CONSUMER_MSG_IDX
WHERE CUS_ID = V_TYPE_CUS_HEADER.CUS_ID AND
ADDRESS_ID = V_TYPE_CUS_HEADER.ADDRESS_ID AND
STATUS =! 8;Edited by: junez on Jul 23, 2009 7:30 PMYour code had an extra AND. I also replaced the "not equal" operator, which has display problems with the forum software
SELECT SYNC,CUS_ID
INTO V_SYNC,V_CUS_ID
FROM CONSUMER_MSG_IDX
WHERE CUS_ID = V_TYPE_CUS_HEADER.CUS_ID AND
ADDRESS_ID = V_TYPE_CUS_HEADER.ADDRESS_ID AND
STATUS != 8;Are you sure this query is doing a table scan? Is there an index on CUS_ID, ADDRESS_ID? I would think that would be mostly unique. So I'm not sure why you think the STATUS column is causing problems. It would seem to just be a non-selective additional filter.
Justin -
Can this query be optimised?
Hi,
This query is taking more time to execute. please can someone advise..
SELECT reference_value AS billing_system_account_id,
account_id AS sim_account_id
FROM account_reference
WHERE reference_name = 'ACCOUNT_ID'
AND account_id IN
(SELECT DISTINCT (ACCOUNT_ID)
FROM asset
WHERE status NOT IN ('CEASED', 'CANCELLED')
AND asset_id IN
(SELECT asset_id
FROM asset_config
WHERE config_name IN
('userName', 'login')
AND (config_value IN
('abc'
|| '@abc',
'abc'))))Using EXISTS instead of IN, as Salim suggested, might work for you.
Some more explanation:
http://asktom.oracle.com/pls/asktom/f?p=100:11:2095243262787694::::P11_QUESTION_ID:953229842074
But anyway:
when posting a tuning request, you always need to mention:
- your database version
- an execution plan of the query (or tkprof output)
- information regarding indexes
- information regarding table statistics
in order to make it possible for us to help you better.
Please read these informative threads regarding posting tuning requests:
When your query takes too long ...
HOW TO: Post a SQL statement tuning request - template posting -
Can this Query be written better?
Dear Experts,
I have this SQL running long, can this be re-written to improve performance. Below is the SQL stmt and its current execution plan:
/* Formatted on 9/12/2012 3:04:16 PM (QP5 v5.163.1008.3004) */
SELECT *
FROM SEA_DWSTG.XO_E_PER_ASSIGNMENT_P XO_E_PER_ASSIGNMENT_P
RIGHT OUTER JOIN
SEA_DWSTG.X_SNI_FUEL_PUR_TXN_P X_SNI_FUEL_PUR_TXN_P
ON X_SNI_FUEL_PUR_TXN_P.DRV_NUM = XO_E_PER_ASSIGNMENT_P.DRV_NUM
AND X_SNI_FUEL_PUR_TXN_P.TXN_DTTM BETWEEN XO_E_PER_ASSIGNMENT_P.ASN_EFF_STRT_DT
AND XO_E_PER_ASSIGNMENT_P.ASN_EFF_END_DT
AND XO_E_PER_ASSIGNMENT_P.PS_CURR_IND = 1
AND XO_E_PER_ASSIGNMENT_P.DRV_NUM IS NOT NULL
AND XO_E_PER_ASSIGNMENT_P.ACTV_EMP_IND = 1
AND XO_E_PER_ASSIGNMENT_P.PRSN_TYP_RNK_NUM = 1
LEFT OUTER JOIN
SEA_DWSTG.X_SNI_FUEL_PLN_STP_DTL_P X_SNI_FUEL_PLN_STP_DTL_P
RIGHT OUTER JOIN
SEA_DWSTG.X_SNI_FUEL_CMPLY_DTL_P X_SNI_FUEL_CMPLY_DTL_P
ON (X_SNI_FUEL_PLN_STP_DTL_P.PWR_NUM =
X_SNI_FUEL_CMPLY_DTL_P.PWR_NUM)
AND X_SNI_FUEL_PLN_STP_DTL_P.PLN_DTTM =
X_SNI_FUEL_CMPLY_DTL_P.PLN_DT
AND (X_SNI_FUEL_PLN_STP_DTL_P.pln_stp_dtl_id,
X_SNI_FUEL_PLN_STP_DTL_P.pwr_num,
X_SNI_FUEL_PLN_STP_DTL_P.pln_gal_qty) IN
(SELECT pln_stp_dtl_id, pwr_num, pln_gal_qty
FROM (SELECT pln_stp_dtl_id,
pwr_num,
pln_gal_qty,
pln_dttm,
ROW_NUMBER ()
OVER (PARTITION BY pwr_num, pln_dttm
ORDER BY pln_stp_dtl_id DESC)
r1
FROM SEA_DWSTG.X_SNI_FUEL_PLN_STP_DTL_P)
WHERE r1 = 1)
LEFT OUTER JOIN
SEA_DWSTG.X_SNI_TRK_STP_P X_SNI_TRK_STP_P
ON X_SNI_FUEL_PLN_STP_DTL_P.PRVD_TRK_STP_CD =
X_SNI_TRK_STP_P.CMDTA_CD
ON X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID =
X_SNI_FUEL_CMPLY_DTL_P.FUEL_PUR_TXN_ID,
SEA_DWSTG.X_SNI_FUEL_PUR_TXN_PRD_P X_SNI_FUEL_PUR_TXN_PRD_P
WHERE (1 = 1)
AND (X_SNI_FUEL_PUR_TXN_PRD_P.FUEL_PUR_TXN_ID =
X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID
AND X_SNI_FUEL_PUR_TXN_PRD_P.PS_CURR_IND = 1
AND X_SNI_FUEL_PUR_TXN_P.PS_CURR_IND = 1)
PLAN_TABLE_OUTPUT
Plan hash value: 2124177374
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 5909K| 24G| | 26M (2)| 98:57:55 |
|* 1 | HASH JOIN RIGHT OUTER | | 5909K| 24G| 446M| 26M (2)| 98:57:55 |
| 2 | VIEW | | 690K| 438M| | 25M (2)| 96:08:32 |
|* 3 | HASH JOIN OUTER | | 932K| 586M| 232M| 25M (2)| 96:09:23 |
| 4 | VIEW | | 458K| 226M| | 25M (2)| 96:08:32 |
| 5 | NESTED LOOPS OUTER | | 458K| 162M| | 25M (2)| 96:08:32 |
| 6 | TABLE ACCESS FULL | X_SNI_FUEL_CMPLY_DTL_P | 30548 | 3430K| | 46 (3)| 00:00:01 |
| 7 | VIEW | | 15 | 3855 | | 845 (2)| 00:00:12 |
|* 8 | FILTER | | | | | | |
|* 9 | HASH JOIN SEMI | | 15 | 1995 | | 845 (2)| 00:00:12 |
|* 10 | TABLE ACCESS FULL | X_SNI_FUEL_PLN_STP_DTL_P | 15 | 1425 | | 150 (2)| 00:00:03 |
| 11 | VIEW | VW_NSO_1 | 151K| 5639K| | 694 (2)| 00:00:10 |
|* 12 | VIEW | | 151K| 7569K| | 694 (2)| 00:00:10 |
|* 13 | WINDOW SORT PUSHED RANK| | 151K| 3413K| 5376K| 694 (2)| 00:00:10 |
| 14 | TABLE ACCESS FULL | X_SNI_FUEL_PLN_STP_DTL_P | 151K| 3413K| | 150 (2)| 00:00:03 |
| 15 | TABLE ACCESS FULL | X_SNI_TRK_STP_P | 5609 | 772K| | 10 (0)| 00:00:01 |
|* 16 | HASH JOIN | | 5909K| 20G| 89M| 409K (1)| 01:31:27 |
|* 17 | TABLE ACCESS FULL | X_SNI_FUEL_PUR_TXN_PRD_P | 1032K| 77M| | 1011 (4)| 00:00:14 |
| 18 | VIEW | | 6959K| 23G| | 13071 (2)| 00:02:56 |
|* 19 | HASH JOIN OUTER | | 6959K| 5322M| 179M| 13071 (2)| 00:02:56 |
|* 20 | TABLE ACCESS FULL | X_SNI_FUEL_PUR_TXN_P | 1019K| 168M| | 2087 (3)| 00:00:28 |
|* 21 | TABLE ACCESS FULL | XO_E_PER_ASSIGNMENT_P | 297K| 178M| | 5061 (2)| 00:01:08 |
Predicate Information (identified by operation id):
1 - access("X_SNI_FUEL_PUR_TXN_P"."FUEL_PUR_TXN_ID"="X_SNI_FUEL_CMPLY_DTL_P"."FUEL_PUR_TXN_ID"(+))
3 - access("X_SNI_FUEL_PLN_STP_DTL_P"."PRVD_TRK_STP_CD"="X_SNI_TRK_STP_P"."CMDTA_CD"(+))
8 - filter("X_SNI_FUEL_CMPLY_DTL_P"."PLN_DT" IS NOT NULL)
9 - access("X_SNI_FUEL_PLN_STP_DTL_P"."PLN_STP_DTL_ID"="PLN_STP_DTL_ID" AND
"X_SNI_FUEL_PLN_STP_DTL_P"."PWR_NUM"="PWR_NUM" AND "X_SNI_FUEL_PLN_STP_DTL_P"."PLN_GAL_QTY"="PLN_GAL_QTY")
10 - filter("X_SNI_FUEL_PLN_STP_DTL_P"."PWR_NUM"="X_SNI_FUEL_CMPLY_DTL_P"."PWR_NUM" AND
"X_SNI_FUEL_PLN_STP_DTL_P"."PLN_DTTM"="X_SNI_FUEL_CMPLY_DTL_P"."PLN_DT")
12 - filter("R1"=1)
13 - filter(ROW_NUMBER() OVER ( PARTITION BY "PWR_NUM","PLN_DTTM" ORDER BY
INTERNAL_FUNCTION("PLN_STP_DTL_ID") DESC )<=1)
16 - access("X_SNI_FUEL_PUR_TXN_PRD_P"."FUEL_PUR_TXN_ID"="from$_subquery$_003"."FUEL_PUR_TXN_ID")
17 - filter("X_SNI_FUEL_PUR_TXN_PRD_P"."PS_CURR_IND"=1)
19 - access("X_SNI_FUEL_PUR_TXN_P"."DRV_NUM"="XO_E_PER_ASSIGNMENT_P"."DRV_NUM"(+))
filter("X_SNI_FUEL_PUR_TXN_P"."TXN_DTTM"<="XO_E_PER_ASSIGNMENT_P"."ASN_EFF_END_DT"(+) AND
"X_SNI_FUEL_PUR_TXN_P"."TXN_DTTM">="XO_E_PER_ASSIGNMENT_P"."ASN_EFF_STRT_DT"(+))
20 - filter("X_SNI_FUEL_PUR_TXN_P"."PS_CURR_IND"=1)
21 - filter("XO_E_PER_ASSIGNMENT_P"."DRV_NUM"(+) IS NOT NULL AND
"XO_E_PER_ASSIGNMENT_P"."ACTV_EMP_IND"(+)=1 AND "XO_E_PER_ASSIGNMENT_P"."PS_CURR_IND"(+)=1 AND
"XO_E_PER_ASSIGNMENT_P"."PRSN_TYP_RNK_NUM"(+)=1)
Note
- SQL plan baseline "SYS_SQL_PLAN_160e8c87962a1dc5" used for this statement
55 rows selected.Hello
I think you have some issues with your logic here. For example
LEFT OUTER JOIN
SEA_DWSTG.X_SNI_TRK_STP_P X_SNI_TRK_STP_P
ON X_SNI_FUEL_PLN_STP_DTL_P.PRVD_TRK_STP_CD = X_SNI_TRK_STP_P.CMDTA_CD
ON X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID = X_SNI_FUEL_CMPLY_DTL_P.FUEL_PUR_TXN_ID,
SEA_DWSTG.X_SNI_FUEL_PUR_TXN_PRD_P X_SNI_FUEL_PUR_TXN_PRD_P
WHERE (1 = 1)
AND (X_SNI_FUEL_PUR_TXN_PRD_P.FUEL_PUR_TXN_ID = X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID
AND X_SNI_FUEL_PUR_TXN_PRD_P.PS_CURR_IND = 1
AND X_SNI_FUEL_PUR_TXN_P.PS_CURR_IND = 1)You're using a left outer join but then you're specifying a where clause which means this has to be an inner join. And specifically what is this bit doing...
ON X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID = X_SNI_FUEL_CMPLY_DTL_P.FUEL_PUR_TXN_ID,
SEA_DWSTG.X_SNI_FUEL_PUR_TXN_PRD_P X_SNI_FUEL_PUR_TXN_PRD_PYou appear to be mixing ANSI join syntax with oracle style syntax i.e you've got a comma after X_SNI_FUEL_CMPLY_DTL_P.FUEL_PUR_TXN_ID, and then you're specifying another table. Why isn't this using ANSI syntax?
And what's the deal with mixing the left and right outer joins? Try to make it consistent.
You're accessing X_SNI_FUEL_PLN_STP_DTL_P twice but as far as I can tell you could avoid the second access in the subquery by bringing the ROW_NUMBER function into the main select and wrapping it in an inline view to do the filtering.
i.e.
SELECT
FROM
SELECT XO_E_PER_ASSIGNMENT_P.*,
X_SNI_FUEL_PUR_TXN_P.*
X_SNI_FUEL_PLN_STP_DTL_P.*
X_SNI_FUEL_CMPLY_DTL_P.*
X_SNI_TRK_STP_P.*
X_SNI_FUEL_PUR_TXN_PRD_P.*
ROW_NUMBER () OVER (PARTITION BY pwr_num, pln_dttm,pln_gal_qty ORDER BY pln_stp_dtl_id DESC) r1
FROM
SEA_DWSTG.XO_E_PER_ASSIGNMENT_P XO_E_PER_ASSIGNMENT_P
RIGHT OUTER JOIN
SEA_DWSTG.X_SNI_FUEL_PUR_TXN_P X_SNI_FUEL_PUR_TXN_P
ON X_SNI_FUEL_PUR_TXN_P.DRV_NUM = XO_E_PER_ASSIGNMENT_P.DRV_NUM
AND X_SNI_FUEL_PUR_TXN_P.TXN_DTTM BETWEEN XO_E_PER_ASSIGNMENT_P.ASN_EFF_STRT_DT AND XO_E_PER_ASSIGNMENT_P.ASN_EFF_END_DT
AND XO_E_PER_ASSIGNMENT_P.PS_CURR_IND = 1
AND XO_E_PER_ASSIGNMENT_P.DRV_NUM IS NOT NULL
AND XO_E_PER_ASSIGNMENT_P.ACTV_EMP_IND = 1
AND XO_E_PER_ASSIGNMENT_P.PRSN_TYP_RNK_NUM = 1
LEFT OUTER JOIN
SEA_DWSTG.X_SNI_FUEL_PLN_STP_DTL_P X_SNI_FUEL_PLN_STP_DTL_P
RIGHT OUTER JOIN
SEA_DWSTG.X_SNI_FUEL_CMPLY_DTL_P X_SNI_FUEL_CMPLY_DTL_P
ON (X_SNI_FUEL_PLN_STP_DTL_P.PWR_NUM = X_SNI_FUEL_CMPLY_DTL_P.PWR_NUM)
AND X_SNI_FUEL_PLN_STP_DTL_P.PLN_DTTM = X_SNI_FUEL_CMPLY_DTL_P.PLN_DT
AND (X_SNI_FUEL_PLN_STP_DTL_P.pln_stp_dtl_id, X_SNI_FUEL_PLN_STP_DTL_P.pwr_num, X_SNI_FUEL_PLN_STP_DTL_P.pln_gal_qty)
LEFT OUTER JOIN
SEA_DWSTG.X_SNI_TRK_STP_P X_SNI_TRK_STP_P
ON X_SNI_FUEL_PLN_STP_DTL_P.PRVD_TRK_STP_CD = X_SNI_TRK_STP_P.CMDTA_CD
ON X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID = X_SNI_FUEL_CMPLY_DTL_P.FUEL_PUR_TXN_ID,
SEA_DWSTG.X_SNI_FUEL_PUR_TXN_PRD_P X_SNI_FUEL_PUR_TXN_PRD_P
WHERE (1 = 1)
AND (X_SNI_FUEL_PUR_TXN_PRD_P.FUEL_PUR_TXN_ID = X_SNI_FUEL_PUR_TXN_P.FUEL_PUR_TXN_ID
AND X_SNI_FUEL_PUR_TXN_PRD_P.PS_CURR_IND = 1
AND X_SNI_FUEL_PUR_TXN_P.PS_CURR_IND = 1)
WHERE
r1 = 1From a performance perspective it's not helping that you're doing full table scans in a nested loop. I think you need to clarify some of your logic and remove the unnecessary outer joins and then you can look to address some of the performance issues.
David -
no matter what website, every time I move the mouse pointer over something to click on a line of black printing in a thin white box pops up at the bottom of the screen and shows me the http address of the item I'm about to click on. I want to shut this function off or remove it if possible. I find that the previous version did not do this, it seems to be unique to 6.0. It's VERY distracting and ANNOYING. How can I make it go away????
When you hover the mouse cursor over any link, it will display the link details at the bottom left of the screen; black font on white background.
This display can be very important as it shows the full address of the hyperlink. This can be particulary useful for checking on whether the link is bona-fida. For example; if you receive an email that looks like it is from a bank , hovering over the link will indicate instantly that the link is pretending to be something it isn't.
Another example that can be useful; a website link may say 'contact me' rather than give the actual email address. However, when you hover over the link, the actual email address will be displayed at the bottom of the screen. If you click on the link then Outlook usually opens, however I do not use Outlook, so displaying the email address is useful.
This display is something that has always been present and it is also displayed if you use IE.
However, I can see that this annoys you, so you could try the following:
[[https://addons.mozilla.org/en-US/firefox/addon/link-location-bar/?src=api]]
After installing the addon, you will need to restart Firefox, then go to History and Restore previous session to get all your tabs and webpages back.
You will now find that when you hover over any link, it will now display in your address bar on the right hand side.
If this helps, please say this question has been solved. -
We have had to change to a new router but the Macbook Pro still reverts to the old one. How can we lose this and make the new router the default?
Go to "Network" under system preferences and choose "advanced"
On the Wi-Fi tab you can sort the sequence of the preferred connections.
Then just move your new router to the top of the list. -
Can this simple script be made quicker? Color labels
All this does is turn all the selected items red, it works but it is not as qucik as selecting all the files right click and select color label.
In the manual method you see all the files turn red at the same time, whereas below you see it happen 1 by one.
tell application "Finder"
activate
set selected to selection
repeat with n_file in every item in selected
set label index of n_file to 2
end repeat
end tellHi,
If you want a faster script, you can use this script (python shell) :
-- none = 0, gray = 2, green = 4, purple = 6, blue = 8, yellow = 10, red = 12, orange = 14
set tColor to 12 -- red
tell application "Finder" to set sel to selection as alias list
repeat with i in sel
tell i to set contents to quoted form of POSIX path of it
end repeat
set oTID to text item delimiters
set text item delimiters to " "
set sel to sel as string -- arguments for the shell
set text item delimiters to oTID
do shell script "/usr/bin/env python -c 'from sys import argv; from xattr import xattr
color = chr(int(argv[1]))
nbr = len(argv)
for i in range( 2, nbr ):
try:
attrs = xattr(argv[i])
try:
finder_attrs = attrs[u\"com.apple.FinderInfo\"]
except:
finder_attrs = 32*chr(0)
finder_attrs = finder_attrs[:9] + color + finder_attrs[10:]
attrs.set(u\"com.apple.FinderInfo\", finder_attrs)
except:
print argv[i]' " & tColor & " " & sel
The shell return the files path that have not been processed (error : permissions, locked, ...).
Here is the bench on my old machine (156 selected files) :
- 9.4 seconds, script using the Finder
- 3.4 seconds, script using the Finder and (selection as alias list)
- 0.24 second, my script. -
I'm having trouble with a query because I need the Count results in the query below to refer only to the current column of the SQL Report:
select T1.id,
T1.name,
(select count(*) from T1 where typeid = 222
AND id = # T1.id#) "COUNT 1",
(select count(*) from T1 where typeid = 262
AND id = # T1.id#) "COUNT 2"
from T1
order by T1.name
Is this possible to do? If I substitute an actual ID instead of the #T1.id#, it returns the results that I want.
Thanks much,
NoraTry this -
select
a.id,
a.name,
(select count(*) from t1 b where b.typeid = 222 and b.id = a.id) 'COUNT 1',
(select count(*) from t1 b where b.typeid = 262 and b.id = a.id) 'COUNT 2',
from
t1 a
order by a.nameWithout more details about what you're trying to achieve it's hard to suggest (better) alternatives -
How can i speedup this query ?
Hi,
have a look at this query:
SELECT DISTINCT element_short_description
FROM sample_test_report, test_group, test_element_master
WHERE str_test_group_code = tgr_test_group_code
AND str_element_code = element_code
AND tgr_group_description = 'SINTER_CHEMICAL_ANALYSIS'
ORDER BY element_short_descriptionThing is that total number of rows present in "sample_test_report" tables are in lakh ...around 50 lakh.Other two tables have a few rows. This query is taking around 15 seconds for completion, can this query be made faster ?
Note that proper indexing have been done already.
Thanks.SELECT DISTINCT element_short_description
FROM sample_test_report, test_group, test_element_master
WHERE str_test_group_code = tgr_test_group_code
AND str_element_code = element_code
AND tgr_group_description = 'SINTER_CHEMICAL_ANALYSIS'
ORDER BY element_short_description
Can you provide us with explain plan?
I suggest you use table alias every time:
The alias is specified in the FROM clause after each table name.
Table aliases make your queries more readable.
Then gather statistics your tables:
BEGIN
DBMS_STATS.GATHER_TABLE_STATS('OWNER','TABLE',estimate_percent=>NULL,method_opt=>'FOR ALL INDEXED COLUMNS SIZE AUTO',DEGREE=>10,CASCADE=>TRUE,granularity=>'ALL');
END;
if your db version < 10g
ANALYZE TABLE employees COMPUTE STATISTICS;
ANALYZE TABLE employees ESTIMATE STATISTICS;
If necessary rebuild index, check indexes are use.
Unusable indexes are made valid by rebuilding them to recalculate the pointers.
Rebuilding an unusable index re-creates the index in a new location, and then drops the unusable index. This can be done either by using Enterprise Manager or through SQL commands:
ALTER INDEX HR.emp_empid_pk REBUILD;
ALTER INDEX HR.emp_empid_pk REBUILD ONLINE;
ALTER INDEX HR.email REBUILD TABLESPACE USERS;
Note: Rebuilding an index requires that free space be available for the rebuild. Verify that there is sufficient space before attempting the rebuild. Enterprise Manager checks space requirements automatically.
At the end, try join table separately and then concatenate them.
Your query will be work good.
Look at execution plan, if indexes are not used, use HINTs : There are many Oracle hints available to the developer for use in tuning SQL statements that are embedded in PL/SQL.
please refer to http://www.dba-oracle.com/t_sql_hints_tuning.htm and http://www.adp-gmbh.ch/ora/sql/hints/index.html
Good luck -
Shorten my spatial query statement
HI ,
Can anybody help me shorten this query statement?
Thanks!
select tile.filename,
SDO_GEOM.SDO_AREA(
SDO_GEOM.SDO_INTERSECTION(tile.geometry,
MDSYS.SDO_GEOMETRY(2003, 8307, NULL,
MDSYS.SDO_ELEM_INFO_ARRAY(1, 1003, 1),
MDSYS.SDO_ORDINATE_ARRAY(50,10, 70,10, 70,50, 50,50, 50,10)),
0.0005),0.0005) as area
from tile
WHERE tile.archiveId = 1
AND tile.dataType = 'Ortho'
AND
SDO_GEOM.SDO_AREA(
SDO_GEOM.SDO_INTERSECTION(tile.geometry,
MDSYS.SDO_GEOMETRY(2003, 8307, NULL,
MDSYS.SDO_ELEM_INFO_ARRAY(1, 1003, 1),
MDSYS.SDO_ORDINATE_ARRAY(50,10, 70,10, 70,50, 50,50, 50,10)),
0.0005),0.0005) > 0
ORDER BY tileid;
Message was edited by:
eecowYou can define your test geometry once in an inline view, and use sdo_relates (ie, anyinteract and not touch) to ensure area>0. Something like this...
select a.filename, a.tileid
SDO_GEOM.SDO_AREA(
SDO_GEOM.SDO_INTERSECTION(a.geometry, b.g,0.0005),0.0005) as area
from tile a,
(select MDSYS.SDO_GEOMETRY(2003, 8307, NULL,
MDSYS.SDO_ELEM_INFO_ARRAY(1, 1003, 1),
MDSYS.SDO_ORDINATE_ARRAY(50,10, 70,10, 70,50, 50,50, 50,10)) as g
from dual) b
where a.archiveid=1
and a.dataType = 'Ortho'
and sdo_anyinteract(a.geometry, b.g) = 'TRUE'
and sdo_touch(a.geometry, b.g) != 'TRUE'
order by a.tileid
I won't say it's necessarily optimized, but it's a little shorter. -
How I can change this query, so I can display the name and scores in one r
How I can change this query, so I can add the ID from the table SPRIDEN
as of now is giving me what I want:
1,543 A05 24 A01 24 BAC 24 BAE 24 A02 20 BAM 20in one line but I would like to add the id and name that are stored in the table SPRIDEN
SELECT sortest_pidm,
max(decode(rn,1,sortest_tesc_code)) tesc_code1,
max(decode(rn,1,score)) score1,
max(decode(rn,2,sortest_tesc_code)) tesc_code2,
max(decode(rn,2,score)) score2,
max(decode(rn,3,sortest_tesc_code)) tesc_code3,
max(decode(rn,3,score)) score3,
max(decode(rn,4,sortest_tesc_code)) tesc_code4,
max(decode(rn,4,score)) score4,
max(decode(rn,5,sortest_tesc_code)) tesc_code5,
max(decode(rn,5,score)) score5,
max(decode(rn,6,sortest_tesc_code)) tesc_code6,
max(decode(rn,6,score)) score6
FROM (select sortest_pidm,
sortest_tesc_code,
score,
row_number() over (partition by sortest_pidm order by score desc) rn
FROM (select sortest_pidm,
sortest_tesc_code,
max(sortest_test_score) score
from sortest,SPRIDEN
where
SPRIDEN_pidm =SORTEST_PIDM
AND sortest_tesc_code in ('A01','BAE','A02','BAM','A05','BAC')
and sortest_pidm is not null
GROUP BY sortest_pidm, sortest_tesc_code))
GROUP BY sortest_pidm;
Hi,
That depends on whether spriden_pidm is unique, and on what you want for results.
Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevamnt columns only) for all tables, and the results you want from that data.
If you can illustrate your problem using commonly available tables (such as those in the scott or hr schemas) then you don't have to post any sample data; just post the results you want.
Either way, explain how you get those results from that data.
Always say which version of Oracle you're using.
It looks like you're doing something similiar to the following.
Using the emp and dept tables in the scott schema, produce one row of output per department showing the highest salary in each job, for a given set of jobs:
DEPTNO DNAME LOC JOB_1 SAL_1 JOB_2 SAL_2 JOB_3 SAL_3
20 RESEARCH DALLAS ANALYST 3000 MANAGER 2975 CLERK 1100
10 ACCOUNTING NEW YORK MANAGER 2450 CLERK 1300
30 SALES CHICAGO MANAGER 2850 CLERK 950On each row, the jobs are listed in order by the highest salary.
This seems to be analagous to what you're doing. The roles played by sortest_pidm, sortest_tesc_code and sortest_test_score in your sortest table are played by deptno, job and sal in the emp table. The roles played by spriden_pidm, id and name in your spriden table are played by deptno, dname and loc in the dept table.
It sounds like you already have something like the query below, that produces the correct output, except that it does not include the dname and loc columns from the dept table.
SELECT deptno
, MAX (DECODE (rn, 1, job)) AS job_1
, MAX (DECODE (rn, 1, max_sal)) AS sal_1
, MAX (DECODE (rn, 2, job)) AS job_2
, MAX (DECODE (rn, 2, max_sal)) AS sal_2
, MAX (DECODE (rn, 3, job)) AS job_3
, MAX (DECODE (rn, 3, max_sal)) AS sal_3
FROM (
SELECT deptno
, job
, max_sal
, ROW_NUMBER () OVER ( PARTITION BY deptno
ORDER BY max_sal DESC
) AS rn
FROM (
SELECT e.deptno
, e.job
, MAX (e.sal) AS max_sal
FROM scott.emp e
, scott.dept d
WHERE e.deptno = d.deptno
AND e.job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY e.deptno
, e.job
GROUP BY deptno
;Since dept.deptno is unique, there will only be one dname and one loc for each deptno, so we can change the query by replacing "deptno" with "deptno, dname, loc" throughout the query (except in the join condition, of course):
SELECT deptno, dname, loc -- Changed
, MAX (DECODE (rn, 1, job)) AS job_1
, MAX (DECODE (rn, 1, max_sal)) AS sal_1
, MAX (DECODE (rn, 2, job)) AS job_2
, MAX (DECODE (rn, 2, max_sal)) AS sal_2
, MAX (DECODE (rn, 3, job)) AS job_3
, MAX (DECODE (rn, 3, max_sal)) AS sal_3
FROM (
SELECT deptno, dname, loc -- Changed
, job
, max_sal
, ROW_NUMBER () OVER ( PARTITION BY deptno -- , dname, loc -- Changed
ORDER BY max_sal DESC
) AS rn
FROM (
SELECT e.deptno, d.dname, d.loc -- Changed
, e.job
, MAX (e.sal) AS max_sal
FROM scott.emp e
, scott.dept d
WHERE e.deptno = d.deptno
AND e.job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY e.deptno, d.dname, d.loc -- Changed
, e.job
GROUP BY deptno, dname, loc -- Changed
;Actually, you can keep using just deptno in the analytic PARTITION BY clause. It might be a little more efficient to just use deptno, like I did above, but it won't change the results if you use all 3, if there is only 1 danme and 1 loc per deptno.
By the way, you don't need so many sub-queries. You're using the inner sub-query to compute the MAX, and the outer sub-query to compute rn. Analytic functions are computed after aggregate fucntions, so you can do both in the same sub-query like this:
SELECT deptno, dname, loc
, MAX (DECODE (rn, 1, job)) AS job_1
, MAX (DECODE (rn, 1, max_sal)) AS sal_1
, MAX (DECODE (rn, 2, job)) AS job_2
, MAX (DECODE (rn, 2, max_sal)) AS sal_2
, MAX (DECODE (rn, 3, job)) AS job_3
, MAX (DECODE (rn, 3, max_sal)) AS sal_3
FROM (
SELECT e.deptno, d.dname, d.loc
, e.job
, MAX (e.sal) AS max_sal
, ROW_NUMBER () OVER ( PARTITION BY e.deptno
ORDER BY MAX (sal) DESC
) AS rn
FROM scott.emp e
, scott.dept d
WHERE e.deptno = d.deptno
AND e.job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY e.deptno, d.dname, d.loc
, e.job
GROUP BY deptno, dname, loc
;This will work in Oracle 8.1 and up. In Oracle 11, however, it's better to use the SELECT ... PIVOT feature. -
Can we make this query shorter....?
hi there
Can any body help me in reducing the number of lines in this query ! I am using union to concat 3 queries. Select fields in every query are same and in where condition also most of the joins are same in every query. but in where clause only two conditions are different. Can we make this 3 queries into one becoze entire select statement fields are same and most of the where conditions are same !
Below is the query:
SELECT
au1.bu_id,
au1.first_nam,
au1.last_nam,
c.id,
c.first_nam_d,
c.last_nam_d,
c.birth_dte_d,
c.ssn_num_d,
se.id schd_event_id,
seu.dsc,
ssc.dsc,
sea.access_typ,
sea.ATTENDEE_TYP
FROM
CLIENT c,
APP_USER au1,
SCHD_STAT_CD ssc,
schd_event_url seu,
schd_evnt se,
SCHD_evnt_stat ses,
SCHD_EVNT_ATTENDEE sea,
CLIENT_APPT ca
WHERE
c.id = ca.client_id_k AND
au1.bu_id = se.APP_USER_BU_ID_PRIME_K AND
ssc.code = ses.SCHD_STAT_CD_CODE_K AND
seu.code = se.SCHD_EVNT_URL_CODE_K AND
se.id = ses.SCHD_EVNT_ID_K AND
se.id = sea.SCHD_EVNT_ID_K(+) AND
se.id = ca.SCHD_EVNT_ID_K AND
sea.ATTENDEE_TYP(+) = 'WORKER' AND
ses.create_ts = (select max(create_ts) FROM SCHD_evnt_stat ses1 WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
UNION
SELECT
au1.bu_id,
au1.first_nam,
au1.last_nam,
c.id,
c.first_nam_d,
c.last_nam_d,
c.birth_dte_d,
c.ssn_num_d,
se.id schd_event_id,
seu.dsc,
ssc.dsc,
FROM
CLIENT c,
APP_USER au1,
SCHD_STAT_CD ssc,
schd_event_url seu,
schd_evnt se,
SCHD_evnt_stat ses,
SCHD_EVNT_ATTENDEE sea,
CLIENT_APPT ca
WHERE
c.id = ca.client_id_k AND
au1.bu_id = se.APP_USER_BU_ID_PRIME_K AND
ssc.code = ses.SCHD_STAT_CD_CODE_K AND
seu.code = se.SCHD_EVNT_URL_CODE_K AND
se.id = ses.SCHD_EVNT_ID_K AND
se.id = sea.SCHD_EVNT_ID_K AND
se.id = ca.SCHD_EVNT_ID_K AND
sea.ATTENDEE_TYP = 'ROLE' AND
ses.create_ts = (select max(create_ts) FROM SCHD_evnt_stat ses1 WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
UNION
SELECT
au1.bu_id,
au1.first_nam,
au1.last_nam,
c.id,
c.first_nam_d,
c.last_nam_d,
c.birth_dte_d,
c.ssn_num_d,
se.id schd_event_id,
seu.dsc,
ssc.dsc,
sea.access_typ,
sea.ATTENDEE_TYP
FROM
CLIENT c,
APP_USER au1,
SCHD_STAT_CD ssc,
schd_event_url seu,
schd_evnt se,
SCHD_evnt_stat ses,
SCHD_EVNT_ATTENDEE sea,
CLIENT_APPT ca
WHERE
c.id = ca.client_id_k AND
au1.APP_ROLE_NAM_DESKTOP_K = sea.ATTENDEE_ID_K AND
ssc.code = ses.SCHD_STAT_CD_CODE_K AND
seu.code = se.SCHD_EVNT_URL_CODE_K AND
se.id = ses.SCHD_EVNT_ID_K AND
se.id = sea.SCHD_EVNT_ID_K AND
se.id = ca.SCHD_EVNT_ID_K AND
sea.ATTENDEE_TYP = 'ROLE' AND
ses.create_ts = (select max(create_ts) FROM SCHD_evnt_stat ses1 WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
Thanks in advance
prasanth a.s.Hi,
it is difficult to change this query without the possibility to test it in a worksheet. But try the following query.
I think it should be the same result as your 3 queries.
SELECT au1.bu_id,
au1.first_nam,
au1.last_nam,
c.id,
c.first_nam_d,
c.last_nam_d,
c.birth_dte_d,
c.ssn_num_d,
se.id schd_event_id,
seu.dsc,ssc.dsc,
decode(sea.access_typ,NULL,' ',sea.access_typ) AS sea.access_typ,
decode(sea.ATTENDEE_TYP,NULL,' ',sea.ATTENDEE_TYP) AS sea.ATTENDEE_TYP
FROM CLIENT c,
APP_USER au1,
SCHD_STAT_CD ssc,
schd_event_url seu,
schd_evnt se,
SCHD_evnt_stat ses,
SCHD_EVNT_ATTENDEE sea,
CLIENT_APPT ca
WHERE c.id = ca.client_id_k
AND au1.bu_id = se.APP_USER_BU_ID_PRIME_K
AND ssc.code = ses.SCHD_STAT_CD_CODE_K
AND seu.code = se.SCHD_EVNT_URL_CODE_K
AND se.id = ses.SCHD_EVNT_ID_K
AND se.id = ca.SCHD_EVNT_ID_K
AND ses.create_ts = (select max(create_ts)
FROM SCHD_evnt_stat ses1
WHERE ses1.SCHD_EVNT_ID_K = ses.SCHD_EVNT_ID_K)
AND
(sea.ATTENDEE_TYP(+) = 'WORKER' AND se.id = sea.SCHD_EVNT_ID_K(+)) --clause of the 1st query
OR
(sea.ATTENDEE_TYP = 'ROLE' AND se.id = sea.SCHD_EVNT_ID_K) --clause of the 2nd query
OR
(sea.ATTENDEE_TYP = 'ROLE' AND se.id = sea.SCHD_EVNT_ID_K AND au1.APP_ROLE_NAM_DESKTOP_K = sea.ATTENDEE_ID_K) --clause of the 3rd query
); -
How can this bad query be improved?
Db:11.2.0.3
We have a 3rd party app and the web app runs very slow. We want to make the 3rd party to fix the issue. for the
app login process, I did an AWR , found the problem query it runs 10 mins. Then I did the sqltrace
here is it:
select clndr_id , count(*)
from
task where (clndr_id = :"SYS_B_0") group by clndr_id union select clndr_id ,
count(*) from project where (clndr_id = :"SYS_B_1") group by clndr_id
call count cpu elapsed disk query current rows
Parse 1 0.01 0.00 0 0 0 0
Execute 1 0.01 0.00 0 0 0 0
Fetch 2 53.32 612.03 81650 58920 0 2
total 4 53.34 612.04 81650 58920 0 2
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 34 (PX)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
2 2 2 SORT UNIQUE (cr=58923 pr=81650 pw=22868 time=113329109 us cost=58277 size=24 card=2)
2 2 2 UNION-ALL (cr=58923 pr=81650 pw=22868 time=113329001 us)
1 1 1 SORT GROUP BY NOSORT (cr=58330 pr=81070 pw=22868 time=104312437 us cost=58128 size=7 card=1)
5589739 5589739 5589739 VIEW index$_join$_003 (cr=58330 pr=81070 pw=22868 time=619784236 us cost=57240 size=38875249 card=5553607)
5589739 5589739 5589739 HASH JOIN (cr=58330 pr=81070 pw=22868 time=617373467 us)
5590158 5590158 5590158 INDEX RANGE SCAN NDX_TASK_CALENDAR (cr=21676 pr=21676 pw=0 time=113637058 us cost=11057 size=38875249 card=5553607)(object id 24749)
6673774 6673774 6673774 INDEX FAST FULL SCAN NDX_TASK_PROJ_RSRC (cr=36651 pr=36526 pw=0 time=213370625 us cost=21921 size=38875249 card=5553607)(object id 217274)
1 1 1 SORT GROUP BY NOSORT (cr=593 pr=580 pw=0 time=9016527 us cost=149 size=17 card=1)
136390 136390 136390 INDEX FAST FULL SCAN NDX_PROJECT_CALENDAR (cr=593 pr=580 pw=0 time=165434 us cost=132 size=2315876 card=136228)(object id 154409)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
2 SORT (UNIQUE)
2 UNION-ALL
1 SORT (GROUP BY NOSORT)
5589739 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF 'TASK'
(TABLE)
5589739 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_TASK_CALENDAR' (INDEX)
5590158 SORT (GROUP BY NOSORT)
6673774 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_PROJECT_CALENDAR' (INDEX)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
Disk file operations I/O 2 0.00 0.00
db file sequential read 22235 1.61 138.66
direct path write 1620 3.25 177.42
db file scattered read 2313 1.89 238.98
direct path read 385 1.72 19.52
SQL*Net message from client 2 0.11 0.21Please make your comments.
Thanks in Advance.Salman Qureshi wrote:
Hi,
It looks to me that end result will give you distinct values because of distinct clndr_id. If my thinking is correct, can you use UNION ALL istead of UNION? This will reduce your query execution time by not spending time on removing duplication of results (UNION removes duplication).
Do you have fresh statistics on the tables/indexes involved in this query?
SalmanIt is a 3rd party app, not sure the query is exactly doing for, a good input though,
I have given a try as comparison.
Found
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
UNION
select clndr_id , count(*)
from
task where (clndr_id = :"SYS_B_0") group by clndr_id union select clndr_id ,
count(*) from project where (clndr_id = :"SYS_B_1") group by clndr_id
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 31.18 70.39 25288 58920 0 2
total 4 31.18 70.39 25288 58920 0 2
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 34 (PX)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
2 2 2 SORT UNIQUE (cr=58923 pr=25288 pw=25284 time=70390927 us cost=58277 size=24 card=2)
2 2 2 UNION-ALL (cr=58923 pr=25288 pw=25284 time=70390652 us)
1 1 1 SORT GROUP BY NOSORT (cr=58330 pr=25288 pw=25284 time=70309151 us cost=58128 size=7 card=1)
5589739 5589739 5589739 VIEW index$_join$_003 (cr=58330 pr=25288 pw=25284 time=70027453 us cost=57240 size=38875249 card=5553607)
5589739 5589739 5589739 HASH JOIN (cr=58330 pr=25288 pw=25284 time=68083254 us)
5590158 5590158 5590158 INDEX RANGE SCAN NDX_TASK_CALENDAR (cr=21676 pr=0 pw=0 time=2449897 us cost=11057 size=38875249 card=5553607)(object id 24749)
6673774 6673774 6673774 INDEX FAST FULL SCAN NDX_TASK_PROJ_RSRC (cr=36651 pr=0 pw=0 time=3097204 us cost=21921 size=38875249 card=5553607)(object id 217274)
1 1 1 SORT GROUP BY NOSORT (cr=593 pr=0 pw=0 time=81462 us cost=149 size=17 card=1)
136390 136390 136390 INDEX FAST FULL SCAN NDX_PROJECT_CALENDAR (cr=593 pr=0 pw=0 time=68732 us cost=132 size=2315876 card=136228)(object id 154409)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
2 SORT (UNIQUE)
2 UNION-ALL
1 SORT (GROUP BY NOSORT)
5589739 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF 'TASK'
(TABLE)
5589739 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_TASK_CALENDAR' (INDEX)
5590158 SORT (GROUP BY NOSORT)
6673774 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_PROJECT_CALENDAR' (INDEX)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
direct path write 3347 1.59 43.26
direct path read 130 0.20 0.32
SQL*Net message from client 2 0.23 0.27
********************************************************************************$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
UNION ALL
SQL ID: d6z3ag876m67h Plan Hash: 4277397671
select clndr_id , count(*)
from
task where (clndr_id = :"SYS_B_0") group by clndr_id union all select
clndr_id , count(*) from project where (clndr_id = :"SYS_B_1") group by
clndr_id
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.01 0.00 0 0 0 0
Fetch 2 31.77 89.93 22886 58920 0 2
total 4 31.78 89.94 22886 58920 0 2
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 34 (PX)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
2 2 2 UNION-ALL (cr=58932 pr=22887 pw=22868 time=89865448 us)
1 1 1 SORT GROUP BY NOSORT (cr=58339 pr=22887 pw=22868 time=89865428 us cost=57240 size=7 card=1)
5589739 5589739 5589739 VIEW index$_join$_003 (cr=58339 pr=22887 pw=22868 time=302390812 us cost=57240 size=38875249 card=5553607)
5589739 5589739 5589739 HASH JOIN (cr=58339 pr=22887 pw=22868 time=300505731 us)
5590158 5590158 5590158 INDEX RANGE SCAN NDX_TASK_CALENDAR (cr=21676 pr=0 pw=0 time=2275780 us cost=11057 size=38875249 card=5553607)(object id 24749)
6673774 6673774 6673774 INDEX FAST FULL SCAN NDX_TASK_PROJ_RSRC (cr=36651 pr=18 pw=0 time=3233656 us cost=21921 size=38875249 card=5553607)(object id 217274)
1 1 1 SORT GROUP BY NOSORT (cr=593 pr=0 pw=0 time=77989 us cost=132 size=17 card=1)
136390 136390 136390 INDEX FAST FULL SCAN NDX_PROJECT_CALENDAR (cr=593 pr=0 pw=0 time=55006 us cost=132 size=2315876 card=136228)(object id 154409)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
2 UNION-ALL
1 SORT (GROUP BY NOSORT)
5589739 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF 'TASK'
(TABLE)
5589739 INDEX MODE: ANALYZED (RANGE SCAN) OF 'NDX_TASK_CALENDAR'
(INDEX)
5590158 SORT (GROUP BY NOSORT)
6673774 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_PROJECT_CALENDAR' (INDEX)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
Disk file operations I/O 2 0.00 0.00
direct path write 2069 3.12 58.90
db file sequential read 18 0.78 4.35
direct path read 22 0.12 0.15
SQL*Net message from client 2 0.46 0.71
********************************************************************************found union all used 90s vs union 70s, and the logic and the physical reads are about the same.
Notice that we now have it ran less that 2 mins for the query vs 10 mins before.
The before was at the peak hrs and now it is off the peak. the disk read from peak's 81650 reduced to 22886.
that was the cause of slow -- I/O contentions at peak hrs.
How can we improve the query performance by increasing disk throughput?
SQL> show parameter mem
NAME TYPE VALUE
hi_shared_memory_address integer 0
memory_max_target big integer 4000M
memory_target big integer 3600M
shared_memory_address integer 0
SQL> show parameter db_b
NAME TYPE VALUE
db_block_buffers integer 0
db_block_checking string FALSE
db_block_checksum string TYPICAL
db_block_size integer 8192
SQL> -
Can somebody please simplify/tune this query?
Hi,
Can somebody please simplify this query?
It's a little urgent. Currently this query is taking 10 mins to run.
Thank you so much,
vasu
Purpose:
First Child Case Ind =
Get Parent Case for this case.
Get all child cases for this parent case.
Arrange them in ascending order by create Date.
If the first case (eldest case) is the case in question,
then Y
else N
Query:
SELECT THIS_CASE.OBJID THIS_CASE_OBJID,
PARENT.PARENT_OBJID,
(CASE
WHEN PARENT.FIRST_CHILD_OBJID = THIS_CASE.OBJID
THEN 'Y'
ELSE 'N'
END) IS_FIRST_CHILD_CASE
FROM SA.TABLE_CASE THIS_CASE,
(SELECT PARENT_OBJID,
CHILD_OBJID FIRST_CHILD_OBJID
FROM (SELECT PARENT_CASE.OBJID PARENT_OBJID,
CHILD_CASE.OBJID CHILD_OBJID,
CHILD_CASE.CREATION_TIME,
ROW_NUMBER() OVER (PARTITION BY PARENT_CASE.OBJID
ORDER BY CHILD_CASE.CREATION_TIME ASC) ROW_NUM
FROM SA.TABLE_CASE PARENT_CASE,
SA.TABLE_CASE CHILD_CASE
WHERE PARENT_CASE.OBJID = CHILD_CASE.CASE_VICTIM2CASE
WHERE ROW_NUM = 1
) PARENT
WHERE THIS_CASE.CASE_VICTIM2CASE = PARENT.PARENT_OBJID(+)Thank you for the quick reply.
Here is the explain plan:
Operation Object Name Rows Bytes Cost
SELECT STATEMENT Optimizer Mode=CHOOSE 2 M 32831
HASH JOIN OUTER 2 M 130 M 32831
TABLE ACCESS FULL SA.TABLE_CASE 2 M 24 M 21550
VIEW 307 K 11 M 10130
WINDOW SORT PUSHED RANK 307 K 7 M 10130
MERGE JOIN 307 K 7 M 8666
TABLE ACCESS BY INDEX ROWID SA.TABLE_CASE 307 K 4 M 1266
INDEX FULL SCAN SA.IND_CASE_VICTIM2CASE 307 K 738
SORT JOIN 2 M 19 M 7400
INDEX FAST FULL SCAN SA.PK_TABLE_CASE 2 M 19 M 785
Please let me know.
Thank you. -
Can someone please suggest me in tuning this query?
Can someone please suggest me in tuning this query?
select pemail.email_oid,
pemail.parent_oid,
md.message_data_oid,
ea.email_account_oid,
ef.email_folder_oid,
md.channel,
ea.email_account_name,
md.from_address,
md.to_address,
eref.ref_id ticket_number,
( select count(email_oid) from esg.email where email_oid = pemail.email_oid and email.attachment_flag = 'Y' ) attachment_flag_a,
( select count(email_oid) from esg.email cemail
where cemail.parent_oid = pemail.email_oid and
( cemail.email_type is null or cemail.email_type != 'S') and
cemail.vec_status not in ('G', 'D', 'P') and
cemail.vec_status in ('N', 'O') and
cemail.attachment_flag = 'Y' ) attachment_flag_b,
md.received_date,
pemail.vec_status,
pemail.email_type,
md.subject,
md.content,
pemail.tracking_id,
pemail.assignee_oid,
( select count(email_oid) from email cemail
where cemail.parent_oid = pemail.email_oid and
(cemail.email_type is null or cemail.email_type != 'S' ) and
cemail.vec_status not in ('G','D', 'P') and
cemail.vec_status in ('N', 'O' ) ) child_email_cnt,
( select count(0) from email
where vec_status = 'N' and
email_type = 'O' and
email_oid = pemail.email_oid and
vec_status in ('N', 'O' )) parent_newemail_cnt,
( select count(0) from email
where vec_status = 'N' and
email_type = 'O' and
parent_oid = pemail.email_oid and
vec_status in ('N', 'O' )) child_newemail_cnt
From esg.email pemail,
esg.message_data md,
esg.email_account ea,
esg.email_folder ef,
(select email_oid, ref_id from email_ref
where email_ref_oid in (select min(email_ref_oid) from email_ref where ref_tid = -9000 and ref_sid = 'ESG' and confirmed_flag = 'Y' group by email_oid )) eref
where
md.message_data_oid = pemail.message_data_oid and
pemail.email_folder_oid = ef.email_folder_oid(+) and
ef.email_account_oid = ea.email_account_oid(+) and
pemail.vec_status not in ('G','D','P') and
pemail.vec_status in ('N', 'O') and
pemail.email_oid = eref.email_oid(+) and
lower(md.channel) in ('[email protected]', '[email protected]') and
pemail.email_oid in
( select parent.email_oid from message_data md,
email parent
where md.message_data_oid = parent.message_data_oid and
parent.parent_oid is null and
pemail.email_type = 'O' and
lower(md.to_address) in
( select lower(contact_info) from esg.user_profile up,
esg.person p,
esg.person_contact pc
where up.user_oid = 802239 and
up.person_oid = p.person_oid and
p.person_oid = pc.person_oid and
pc.contact_type_oid = 6 )
union all
select child.parent_oid from message_data md,
email child
where md.message_data_oid = child.message_data_oid and
parent_oid is not null and
child.vec_status not in ('G','D','P') and
child.vec_status in ('N', 'O') and
child.email_type = 'O' and
lower(md.to_address) in
( select lower(contact_info) from esg.user_profile up,
esg.person p,
esg.person_contact pc
where up.user_oid = 802239 and
up.person_oid = p.person_oid and
p.person_oid = pc.person_oid and
pc.contact_type_oid = 6 )
ORDER BY RECEIVED_DATE DESC;
1st run
Elapsed: 00:00:03.39
2nd run
Elapsed: 00:00:01.80
SQL> Select TABLE_NAME,LAST_ANALYZED, num_rows from user_tables where table_name in ('EMAIL','MESSAGE_DATA', 'EMAIL_ACCOUNT','EMAIL_FOLDER','EMAIL_REF','USER_PROFILE','PERSON','PERSON_CONTACT');
TABLE_NAME LAST_ANAL NUM_ROWS
USER_PROFILE 11-JUN-11 385808
PERSON_CONTACT 11-JUN-11 698624
PERSON 11-JUN-11 405364
MESSAGE_DATA 11-JUN-11 1069
EMAIL_REF 11-JUN-11 559
EMAIL_FOLDER 11-JUN-11 563
EMAIL_ACCOUNT 11-JUN-11 563
EMAIL 11-JUN-11 101652
8 rows selected.
row count from all tables
email - 102063
message_data - 1069
email_account - 563
email_folder - 563
email_ref - 559
user_profile - 386055
person - 404057
person_contact - 698696
Thanks,
Suman M.======================================
Execution Plan
Plan hash value: 110416976
| Id | Operation | Name | Rows | Bytes | Cost |
| 0 | SELECT STATEMENT | | 1 | 1361 | 17 |
| 1 | SORT AGGREGATE | | 1 | 8 | |
|* 2 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 8 | 1 |
|* 3 | INDEX UNIQUE SCAN | PK_EMAIL | 1 | | 1 |
| 4 | SORT AGGREGATE | | 1 | 8 | |
|* 5 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 8 | 1 |
|* 6 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 3 | | 1 |
| 7 | SORT AGGREGATE | | 1 | 6 | |
|* 8 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 6 | 1 |
|* 9 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 3 | | 1 |
| 10 | SORT AGGREGATE | | 1 | 10 | |
|* 11 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 10 | 1 |
|* 12 | INDEX UNIQUE SCAN | PK_EMAIL | 1 | | 1 |
| 13 | SORT AGGREGATE | | 1 | 6 | |
|* 14 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 6 | 1 |
|* 15 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 3 | | 1 |
| 16 | SORT ORDER BY | | 1 | 1361 | 17 |
|* 17 | FILTER | | | | |
| 18 | NESTED LOOPS OUTER | | 1 | 1361 | 5 |
| 19 | NESTED LOOPS OUTER | | 1 | 1336 | 4 |
| 20 | NESTED LOOPS | | 1 | 1328 | 3 |
| 21 | NESTED LOOPS OUTER | | 1 | 53 | 2 |
| 22 | INLIST ITERATOR | | | | |
| 23 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 35 | 1 |
|* 24 | INDEX RANGE SCAN | IE_EMAIL | 55 | | 1 |
| 25 | VIEW PUSHED PREDICATE | | 1 | 18 | 1 |
| 26 | NESTED LOOPS | | 1 | 36 | 4 |
| 27 | VIEW | VW_NSO_1 | 11 | 143 | 3 |
| 28 | SORT GROUP BY | | 11 | 231 | 3 |
|* 29 | TABLE ACCESS BY INDEX ROWID| EMAIL_REF | 11 | 231 | 3 |
|* 30 | INDEX FULL SCAN | AK1_EMAIL_REF | 21 | | 3 |
|* 31 | TABLE ACCESS BY INDEX ROWID | EMAIL_REF | 1 | 23 | 1 |
|* 32 | INDEX UNIQUE SCAN | PK_EMAIL_REF | 1 | | 1 |
|* 33 | TABLE ACCESS BY INDEX ROWID | MESSAGE_DATA | 1 | 1275 | 1 |
|* 34 | INDEX UNIQUE SCAN | PK_MESSAGE_DATA | 1 | | 1 |
| 35 | TABLE ACCESS BY INDEX ROWID | EMAIL_FOLDER | 1 | 8 | 1 |
|* 36 | INDEX UNIQUE SCAN | PK_EMAIL_FOLDER | 1 | | 1 |
| 37 | TABLE ACCESS BY INDEX ROWID | EMAIL_ACCOUNT | 1 | 25 | 1 |
|* 38 | INDEX UNIQUE SCAN | PK_EMAIL_ACCOUNT | 1 | | 1 |
| 39 | UNION-ALL | | | | |
| 40 | NESTED LOOPS SEMI | | 1 | 116 | 5 |
| 41 | NESTED LOOPS | | 1 | 50 | 2 |
|* 42 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 14 | 1 |
|* 43 | INDEX UNIQUE SCAN | PK_EMAIL | 1 | | 1 |
| 44 | TABLE ACCESS BY INDEX ROWID | MESSAGE_DATA | 1069 | 38484 | 1 |
|* 45 | INDEX UNIQUE SCAN | PK_MESSAGE_DATA | 1 | | 1 |
|* 46 | VIEW | VW_NSO_3 | 1 | 66 | 3 |
|* 47 | FILTER | | | | |
| 48 | NESTED LOOPS | | 1 | 45 | 3 |
| 49 | NESTED LOOPS | | 1 | 15 | 2 |
| 50 | TABLE ACCESS BY INDEX ROWID | USER_PROFILE | 1 | 10 | 1 |
|* 51 | INDEX UNIQUE SCAN | PK_USER | 1 | | 1 |
|* 52 | INDEX UNIQUE SCAN | PK_PERSON | 405K| 1979K| 1 |
|* 53 | TABLE ACCESS BY INDEX ROWID | PERSON_CONTACT | 1 | 30 | 1 |
|* 54 | INDEX RANGE SCAN | FK_PERSON_CONTACT_PERSON | 2 | | 1 |
| 55 | NESTED LOOPS | | 1 | 114 | 5 |
| 56 | MERGE JOIN CARTESIAN | | 1 | 78 | 4 |
|* 57 | TABLE ACCESS BY INDEX ROWID | EMAIL | 1 | 12 | 1 |
|* 58 | INDEX RANGE SCAN | FK_EMAIL_PARENT | 1 | | 1 |
| 59 | BUFFER SORT | | 1 | 66 | 3 |
| 60 | VIEW | VW_NSO_2 | 1 | 66 | 3 |
| 61 | SORT UNIQUE | | 1 | 45 | |
| 62 | NESTED LOOPS | | 1 | 45 | 3 |
| 63 | NESTED LOOPS | | 1 | 15 | 2 |
| 64 | TABLE ACCESS BY INDEX ROWID| USER_PROFILE | 1 | 10 | 1 |
|* 65 | INDEX UNIQUE SCAN | PK_USER | 1 | | 1 |
|* 66 | INDEX UNIQUE SCAN | PK_PERSON | 405K| 1979K| 1 |
|* 67 | TABLE ACCESS BY INDEX ROWID | PERSON_CONTACT | 1 | 30 | 1 |
|* 68 | INDEX RANGE SCAN | FK_PERSON_CONTACT_PERSON | 2 | | 1 |
|* 69 | TABLE ACCESS BY INDEX ROWID | MESSAGE_DATA | 1 | 36 | 1 |
|* 70 | INDEX UNIQUE SCAN | PK_MESSAGE_DATA | 1 | | 1 |
Predicate Information (identified by operation id):
2 - filter("EMAIL"."ATTACHMENT_FLAG"='Y')
3 - access("EMAIL_OID"=:B1)
5 - filter("CEMAIL"."EMAIL_TYPE"<>'S' AND "CEMAIL"."VEC_STATUS"<>'G' AND
"CEMAIL"."VEC_STATUS"<>'D' AND "CEMAIL"."VEC_STATUS"<>'P' AND ("CEMAIL"."VEC_STATUS"='N'
OR "CEMAIL"."VEC_STATUS"='O') AND "CEMAIL"."ATTACHMENT_FLAG"='Y')
6 - access("CEMAIL"."PARENT_OID"=:B1)
8 - filter("CEMAIL"."EMAIL_TYPE"<>'S' AND "CEMAIL"."VEC_STATUS"<>'G' AND
"CEMAIL"."VEC_STATUS"<>'D' AND "CEMAIL"."VEC_STATUS"<>'P' AND ("CEMAIL"."VEC_STATUS"='N'
OR "CEMAIL"."VEC_STATUS"='O'))
9 - access("CEMAIL"."PARENT_OID"=:B1)
11 - filter("VEC_STATUS"='N' AND "EMAIL_TYPE"='O')
12 - access("EMAIL_OID"=:B1)
14 - filter("VEC_STATUS"='N' AND "EMAIL_TYPE"='O')
15 - access("PARENT_OID"=:B1)
17 - filter( EXISTS ( (SELECT "PARENT"."EMAIL_OID" FROM "EMAIL" "PARENT","MESSAGE_DATA"
"MD", (SELECT LOWER("CONTACT_INFO") "$nso_col_1" FROM "ESG"."PERSON_CONTACT"
"PC","ESG"."PERSON" "P","ESG"."USER_PROFILE" "UP" WHERE :B1='O' AND
"UP"."USER_OID"=802239 AND "UP"."PERSON_OID"="P"."PERSON_OID" AND
"P"."PERSON_OID"="PC"."PERSON_OID" AND "PC"."CONTACT_TYPE_OID"=6)
"VW_NSO_3" WHERE
"$nso_col_1"=LOWER("MD"."TO_ADDRESS") AND "MD"."MESSAGE_DATA_OID"="PARENT"."MESSAGE_DATA_O
ID" AND "PARENT"."EMAIL_OID"=:B2 AND "PARENT"."PARENT_OID" IS NULL
) UNION ALL (SELECT
"CHILD"."PARENT_OID" FROM "EMAIL" "CHILD","MESSAGE_DATA" "MD", (SELECT DISTINCT
LOWER("CONTACT_INFO") "$nso_col_1" FROM "ESG"."PERSON_CONTACT" "PC","ESG"."PERSON"
"P","ESG"."USER_PROFILE" "UP" WHERE "UP"."USER_OID"=802239 AND
"UP"."PERSON_OID"="P"."PERSON_OID" AND "P"."PERSON_OID"="PC"."PERSON_OID" AND
"PC"."CONTACT_TYPE_OID"=6) "VW_NSO_2" WHERE "MD"."MESSAGE_DATA_OID
"="CHILD"."MESSAGE_DATA_
OID" AND "$nso_col_1"=LOWER("MD"."TO_ADDRESS") AND "CHILD"."PARENT
_OID"=:B3 AND
"CHILD"."VEC_STATUS"<>'G' AND "CHILD"."VEC_STATUS"<>'D' AND "CHILD
"."VEC_STATUS"<>'P' AND
("CHILD"."VEC_STATUS"='N' OR "CHILD"."VEC_STATUS"='O') AND "CHILD"
."EMAIL_TYPE"='O' AND
"PARENT_OID" IS NOT NULL)))
24 - access("PEMAIL"."VEC_STATUS"='N' OR "PEMAIL"."VEC_STATUS"='O')
filter("PEMAIL"."VEC_STATUS"<>'G' AND "PEMAIL"."VEC_STATUS"<>'D' AND
"PEMAIL"."VEC_STATUS"<>'P')
29 - filter("CONFIRMED_FLAG"='Y')
30 - access("REF_SID"='ESG' AND "REF_TID"=(-9000))
filter("REF_TID"=(-9000) AND "REF_SID"='ESG')
31 - filter("EMAIL_OID"="PEMAIL"."EMAIL_OID")
32 - access("EMAIL_REF_OID"="$nso_col_1")
33 - filter(LOWER("MD"."CHANNEL")='[email protected]' OR
LOWER("MD"."CHANNEL")='[email protected]')
34 - access("MD"."MESSAGE_DATA_OID"="PEMAIL"."MESSAGE_DATA_OID")
36 - access("PEMAIL"."EMAIL_FOLDER_OID"="EF"."EMAIL_FOLDER_OID"(+))
38 - access("EF"."EMAIL_ACCOUNT_OID"="EA"."EMAIL_ACCOUNT_OID"(+))
42 - filter("PARENT"."PARENT_OID" IS NULL)
43 - access("PARENT"."EMAIL_OID"=:B1)
45 - access("MD"."MESSAGE_DATA_OID"="PARENT"."MESSAGE_DATA_OID")
46 - filter("$nso_col_1"=LOWER("MD"."TO_ADDRESS"))
47 - filter(:B1='O')
51 - access("UP"."USER_OID"=802239)
52 - access("UP"."PERSON_OID"="P"."PERSON_OID")
53 - filter("PC"."CONTACT_TYPE_OID"=6)
54 - access("P"."PERSON_OID"="PC"."PERSON_OID")
57 - filter("CHILD"."VEC_STATUS"<>'G' AND "CHILD"."VEC_STATUS"<>'D' AND
"CHILD"."VEC_STATUS"<>'P' AND ("CHILD"."VEC_STATUS"='N' OR "CHILD"
."VEC_STATUS"='O') AND
"CHILD"."EMAIL_TYPE"='O')
58 - access("CHILD"."PARENT_OID"=:B1)
filter("PARENT_OID" IS NOT NULL)
65 - access("UP"."USER_OID"=802239)
66 - access("UP"."PERSON_OID"="P"."PERSON_OID")
67 - filter("PC"."CONTACT_TYPE_OID"=6)
68 - access("P"."PERSON_OID"="PC"."PERSON_OID")
69 - filter("$nso_col_1"=LOWER("MD"."TO_ADDRESS"))
70 - access("MD"."MESSAGE_DATA_OID"="CHILD"."MESSAGE_DATA_OID")
Note
- cpu costing is off (consider enabling it)
Statistics
3103 recursive calls
0 db block gets
599654 consistent gets
2915 physical reads
0 redo size
1955 bytes sent via SQL*Net to client
3895 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
331 sorts (memory)
0 sorts (disk)
0 rows processed
=====================================
Maybe you are looking for
-
ITunes doesn't work after reinstallation following system restore
My Toshiba Satellite P100 uses Windows XP SP2. I don't think my hardware is a real issue in this instance, but I can post it if others think it's pertinent. I installed iTunes a couple weeks ago and things seemed to work pretty well. I began having i
-
Problem loading numbers after update
I have updated the numbers app on my iPad and it won't load now, if I remove numbers and reload it I will loose all the data in my spreadsheets is there a fix?
-
9i default TNS/DB in formsweb.cfg
I can't seem to make any use of "userid" work on the iAS middletier. It's as if I can't override the basic userid setting: I can't do it in a named config and I can't do it if I put it in the URL. No matter what I do I'm forced to enter username, pas
-
Errors with validate_geometry
does anyone know what means the errors 13350 and 13351 when using Validate_geometry (using 8.1.7) Is it really impossible to obtain the list of errors for validate_geometry? thanks for any information yves
-
Installing 9.0.3.0.0 on Solaris 10 x86
Hi I have a SPARC version of 9.0.3 that I have tried to install on a Windows server and the runInstaller file fails to execute (presumably because its looking for a SPARC chip not a Intel!) My question is, is there an X86 version of 9.0.3? I cannot f