Need to Rewrite Distinct query...
Hi All,
Query A:
SELECT count(DISTINCT FIELD_SERVICE_REP) CNT1
FROM SOP_SERVICE_REQUEST_JOB ssrj, sop_service_request ssr
WHERE TO_DATE('06/12/2006', 'mm/dd/yyyy') BETWEEN
SSRJ.SCHEDULED_START_DATE AND SSRJ.SCHEDULED_END_DATE
AND field_service_rep IS NOT NULL
AND ssr.service_request_id = ssrj.service_request_id
Result of Query A:-
17 --> This is correct result
Query B:
SELECT FIELD_SERVICE_REP, count(FIELD_SERVICE_REP) CNT2
FROM SOP_SERVICE_REQUEST_JOB ssrj, sop_service_request ssr
WHERE TO_DATE('06/12/2006', 'mm/dd/yyyy') BETWEEN
SSRJ.SCHEDULED_START_DATE AND SSRJ.SCHEDULED_END_DATE
AND field_service_rep IS NOT NULL
AND ssr.service_request_id = ssrj.service_request_id
GROUP BY ssrj.field_service_rep
Result of Query B:-
FIELD_SERVICE_REP CNT2
1000036 9
105005937 1
113001460 1
113001757 3
113002205 2
113002476 4
113003027 2
113003166 1
218012616 1
218012630 1
312003325 1
500916804 1
500935725 2
501026039 6
501154894 1
501243382 3
501248836 6I need to rewrite Query A in such a way that I get rid of DISTINCT as it is consuming a lot of time. I tried to group by in Query B and count but it is not giving same result as Query A (as expected..) so how can I rewrite Query A or Query B to get result of Query A (basically count unique field service reps.) as correct result is the one shown in Query A.
Pls. note that original query is very large and it runs on set of dates and hence DISTINCT is taking too much time
Thanks in advance to all for ur help..
JP
Some sample data:
DROP TABLE SOP_SERVICE_REQUEST;
CREATE TABLE SOP_SERVICE_REQUEST(service_request_id number(6,0));
INSERT
INTO SOP_SERVICE_REQUEST
VALUES(100);
INSERT
INTO SOP_SERVICE_REQUEST
VALUES(101);
INSERT
INTO SOP_SERVICE_REQUEST
VALUES(102);
INSERT
INTO SOP_SERVICE_REQUEST
VALUES(103);
INSERT
INTO SOP_SERVICE_REQUEST
VALUES(104);
DROP TABLE SOP_SERVICE_REQUEST_job;
CREATE TABLE SOP_SERVICE_REQUEST_job(service_request_id number(6,0), SCHEDULED_START_DATE date,SCHEDULED_END_DATE date,field_service_rep number(6,0));
INSERT
INTO SOP_SERVICE_REQUEST_job
VALUES(100, '10-jun-06', '16-jun-06', 200);
INSERT
INTO SOP_SERVICE_REQUEST_job
VALUES(101, '10-jun-06', '16-jun-06', 201);
INSERT
INTO SOP_SERVICE_REQUEST_job
VALUES(102, '10-jun-06', '16-jun-06', 201);
INSERT
INTO SOP_SERVICE_REQUEST_job
VALUES(103, '10-jun-06', '16-jun-06', 202);
INSERT
INTO SOP_SERVICE_REQUEST_job
VALUES(104, '10-jun-06', '16-jun-06', 201);
select * from sop_service_request
select * from sop_service_request_job
prompt Query A:
SELECT count(FIELD_SERVICE_REP) CNT1
FROM SOP_SERVICE_REQUEST_JOB ssrj, sop_service_request ssr
WHERE TO_DATE('06/12/2006', 'mm/dd/yyyy')
BETWEEN SSRJ.SCHEDULED_START_DATE AND SSRJ.SCHEDULED_END_DATE
AND field_service_rep IS NOT NULL
AND ssr.service_request_id = ssrj.service_request_id;
prompt Query B:
SELECT FIELD_SERVICE_REP, count(FIELD_SERVICE_REP) CNT2
FROM SOP_SERVICE_REQUEST_JOB ssrj, sop_service_request ssr
WHERE TO_DATE('06/12/2006', 'mm/dd/yyyy')
BETWEEN SSRJ.SCHEDULED_START_DATE AND SSRJ.SCHEDULED_END_DATE
AND field_service_rep IS NOT NULL
AND ssr.service_request_id = ssrj.service_request_id
GROUP BY ssrj.field_service_rep;
Output:
DROP TABLE SOP_SERVICE_REQUEST succeeded.
CREATE TABLE succeeded.
1 rows inserted
1 rows inserted
1 rows inserted
1 rows inserted
DROP TABLE SOP_SERVICE_REQUEST_job succeeded.
CREATE TABLE succeeded.
1 rows inserted
1 rows inserted
1 rows inserted
1 rows inserted
Query A:
CNT1
4
1 rows selected
Query B:
FIELD_SERVICE_REP CNT2
201 2
200 1
202 1
3 rows selected
Similar Messages
-
Rewrite the query with out joins and group by
Hi,
This was an interview question.
Table Names: bookshelf_checkout
bookshelf
And the join condition between these two tables is title
We need to rewrite below query without using join condition and group by clause ?
SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out"
FROM bookshelf_checkout bc,bookshelf b
WHERE bc.title(+)=b.title
GROUP BY b.title;When I was in college, I read that most of the SELECT statements can be replaced by basic SQL operations (SET OPERATORS). Now I am trying to rewrite the query with SET operators but not able to get the exact result.
Kindly help me on this.
Thanks,
SuriSomething like this?
1 WITH books AS (
2 SELECT 'title 1' title FROM dual UNION ALL
3 SELECT 'title 2' FROM dual UNION ALL
4 SELECT 'title 3' FROM dual ),
5 bookshelf AS (
6 SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL
7 SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL
8 SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual )
9 SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs
10 UNION
11 (SELECT b.title, NULL FROM books b
12 MINUS
13* SELECT bs.title, NULL FROM bookshelf bs)
SQL> /
TITLE MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE)
title 1 14
title 2 44
title 3Lukasz -
I have SSRS parametarized report in that one data set have repeated values with query parameter . but while am mapping that query
parameter to report parameter i need to pass distinct values. How can i resolve thisHi nancharaiah,
If I understand correctly, you want to pass distinct values to report parameter. In Reporting Service, there are only three methods for parameter's Available Values:
None
Specify values
Get values from a query
If we utilize the third option that get values from a dataset query, then the all available values are from the returns of the dataset. So if we want to pass distinct values from a dataset, we need to make the dataset returns distinct values. The following
sample is for your reference:
Select distinct field_name from table_name
If you have any other questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
Hello
I have one query taking time to fetch the records. Table contains just 40,000 thousands records but lots of case statement in the query, I just remove lots of case statement there are lot more...
Can you please help me out to rewrite the query to fetch the records faster. Also Right now there are no indexes on the table....
SELECT O.cn,
O.BEN_LAST_NAME,
O.BEN_FIRST_NAME,
O.BEN_MI,
O.SSN,
O.DOB,
O.SEX,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 00 AND 34 THEN
1
ELSE
0
END AS AGE_GROUP0_34_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 35 AND 44 THEN
1
ELSE
0
END AS AGE_GROUP35_44_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 45 AND 54 THEN
1
ELSE
0
END AS AGE_GROUP45_54_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 55 AND 59 THEN
1
ELSE
0
END AS AGE_GROUP55_59_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 60 AND 64 THEN
1
ELSE
0
END AS AGE_GROUP60_64_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 65 AND 69 THEN
1
ELSE
0
END AS AGE_GROUP65_69_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 70 AND 74 THEN
1
ELSE
0
END AS AGE_GROUP70_74_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 75 AND 79 THEN
1
ELSE
0
END AS AGE_GROUP75_79_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 80 AND 84 THEN
1
ELSE
0
END AS AGE_GROUP80_84_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 85 AND 89 THEN
1
ELSE
0
END AS AGE_GROUP85_89_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 90 AND 94 THEN
1
ELSE
0
END AS AGE_GROUP90_94_F,
CASE
WHEN O.SEX = 'F' AND O.AGE BETWEEN 95 AND 00 THEN
1
ELSE
0
END AS AGE_GROUP95_GT_F,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 00 AND 34 THEN
1
ELSE
0
END AS AGE_GROUP0_34_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 35 AND 44 THEN
1
ELSE
0
END AS AGE_GROUP35_44_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 45 AND 54 THEN
1
ELSE
0
END AS AGE_GROUP45_54_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 45 AND 59 THEN
1
ELSE
0
END AS AGE_GROUP55_59_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 60 AND 64 THEN
1
ELSE
0
END AS AGE_GROUP60_64_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 65 AND 69 THEN
1
ELSE
0
END AS AGE_GROUP65_69_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 70 AND 74 THEN
1
ELSE
0
END AS AGE_GROUP70_74_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 75 AND 79 THEN
1
ELSE
0
END AS AGE_GROUP75_79_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 80 AND 84 THEN
1
ELSE
0
END AS AGE_GROUP80_84_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 85 AND 89 THEN
1
ELSE
0
END AS AGE_GROUP85_89_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 90 AND 94 THEN
1
ELSE
0
END AS AGE_GROUP90_94_M,
CASE
WHEN O.SEX = 'M' AND O.AGE BETWEEN 95 AND 00 THEN
1
ELSE
0
END AS AGE_GROUP95_GT_M,
O.YEAR,
CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
,mor222 m2 WHERE m1.map_id = hm.map_id_ref
AND m2.YEAR = hm.YEAR
AND o.cn = m2.cn
AND o.YEAR = hm.YEAR
AND m1.hcc_model_category1 = 1
AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC1,
CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
,mor222 m2 WHERE m1.map_id = hm.map_id_ref
AND m2.YEAR = hm.YEAR
AND o.cn = m2.cn
AND o.YEAR = hm.YEAR
AND m1.hcc_model_category1 = 2
AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC2,
CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
,mor222 m2 WHERE m1.map_id = hm.map_id_ref
AND m2.YEAR = hm.YEAR
AND o.cn = m2.cn
AND o.YEAR = hm.YEAR
AND m1.hcc_model_category1 = 5
AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC5,
CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
,mor222 m2 WHERE m1.map_id = hm.map_id_ref
AND m2.YEAR = hm.YEAR
AND o.cn = m2.cn
AND o.YEAR = hm.YEAR
AND m1.hcc_model_category1 = 7
AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC7,
CASE WHEN EXISTS (SELECT m1.hcc_model_category1 FROM HCC_MODEL_DIAGNOSIS M1 , HCC_MODEL_DIAGNOSIS_MAPPING HM
,mor222 m2 WHERE m1.map_id = hm.map_id_ref
AND m2.YEAR = hm.YEAR
AND o.cn = m2.cn
AND o.YEAR = hm.YEAR
AND m1.hcc_model_category1 = 8
AND m2.diag_code = m1.icd9cmscode) THEN 1 ELSE 0 END AS DISEASE_COEF_HCC8
FROM (SELECT T.cn,
T.BEN_LAST_NAME,
T.BEN_FIRST_NAME,
T.BEN_MI,
T.SSN,
TO_DATE(T.DOB, 'YYYYMMDD') AS DOB,
T.SEX,
TRUNC(MONTHS_BETWEEN(TO_DATE('01/01/' || T.YEAR, 'MM/DD/YYYY'),
TO_DATE(T.DOB, 'YYYYMMDD')) / 12) AS AGE,
T.YEAR
FROM mor111 T ) O Thanks in advance!
Kind regards,
NikOk, so I am going to be a little bit kinder then your first reply, but not much.
From what can tell you are doing medical management and are trying to select by AGE/GENDER CELLS and this is common when trying to deal with RBRVS.
This is by far some of the worst sql I have ever seen, sorry but it just is.
So please report this either as a new post or as a reply and:
1. At least post the table definitions and if you can some sample data.
2. Get rid of almost all the case selectors.
3. Describe the output you need.
As it is this is unreadable.
After that we can talk more.
Edited by: FlyingGuy on Oct 11, 2011 5:13 PM -
Help to rewrite the query --performance issue
Hi ,
Please help to rewrite the query since it's performance is not good.Especially second inline query(CASE statements are therein select caluse ..)is taking more cost.
Database Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bi
SELECT *
FROM
(SELECT q.*,
COUNT(*) OVER() AS record_count,
ROWNUM AS row_num
FROM
(SELECT ExName.examiner_code,
examiner_name,
:v_year,
:v_month,
count_fb,
NVL(count_entered_fb, 0) count_entered_fb,
NVL(count_sent_fb, 0) count_sent_fb,
NVL(count_edited_fb, 0) count_edited_fb,
NVL(count_complete_fb, 0) count_complete_fb,
NVL(count_withibcardiff_fb, 0) count_withibcardiff_fb
FROM
(SELECT examiner_code,
COUNT(*) AS count_fb
FROM
(SELECT
examiner_code,
paper_code,
assessment_school
FROM
( SELECT DISTINCT ce.examiner_code,
ce.paper_code,
ce.assessment_school
FROM
(SELECT
DISTINCT assessment_school,
paper_code,
examiner_code
FROM candidate_examiner_allocation cea
WHERE cea.element = 'Moderation of IA'
AND cea.year = :v_year
AND cea.month = :v_month
) ce,
subject_group sg,
subject_component sc
WHERE (:v_padded_examiner_code IS NULL
OR ce.examiner_code = :v_padded_examiner_code)
AND (:v_subject_group IS NULL
OR sg.group_number = :v_subject_group)
AND sg.year = :v_year
AND sg.month = :v_month
AND sc.year = :v_year
AND sc.month = :v_month
AND sc.paper_code = ce.paper_code
AND sc.subject = sg.subject
AND sc.lvl = sg.lvl
AND (:v_subject IS NULL
OR sc.subject = :v_subject)
AND (:v_lvl IS NULL
OR sc.lvl = :v_lvl)
) ea
GROUP BY examiner_code
) ExName,
(SELECT examiner_code,
COUNT(
CASE
WHEN UPPER(wfi.status) = 'ENTERED'
THEN 1
ELSE NULL
END) AS count_entered_fb,
COUNT(
CASE
WHEN UPPER(wfi.status) = 'SENT'
THEN 1
ELSE NULL
END) AS count_sent_fb,
COUNT(
CASE
WHEN UPPER(wfi.status) = 'EDITED'
THEN 1
ELSE NULL
END) AS count_edited_fb,
COUNT(
CASE
WHEN UPPER(wfi.status) = 'COMPLETE'
THEN 1
ELSE NULL
END) AS count_complete_fb,
COUNT(
CASE
WHEN UPPER(wfi.status) = 'WITH IBCARDIFF'
THEN 1
ELSE NULL
END) AS count_withibcardiff_fb
FROM ia_instances ia1,
workflow_instance wfi
WHERE wfi.instance_id = ia1.workflow_instance_id
AND ia1.year = :v_year
AND ia1.month = :v_month
GROUP BY ia1.year,
ia1.month,
examiner_code
) iaF,
(SELECT person_code,
title
|| ' '
|| firstname
|| ' '
|| lastname AS examiner_name
FROM person
WHERE :v_examiner_name IS NULL
OR UPPER(title
|| ' '
|| firstname
|| ' '
|| lastname) LIKE :v_search_examiner_name
) P
WHERE ExName.examiner_code = iaF.examiner_code (+)
AND ExName.examiner_code = p.person_code
ORDER BY ExName.examiner_code
) q
) rc
WHERE row_num >= :v_start_row
AND row_num <= (:v_start_row+(:v_max_row-1));explain plan
line 1: SQLPLUS Command Skipped: set linesize 130
line 2: SQLPLUS Command Skipped: set pagesize 0
PLAN_TABLE_OUTPUT
Plan hash value: 1581970599
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 276 | | 2187 (6)| 00:00:34 |
|* 1 | FILTER | | | | | | |
|* 2 | VIEW | | 1 | 276 | | 2187 (6)| 00:00:34 |
| 3 | WINDOW BUFFER | | 1 | 250 | | 2187 (6)| 00:00:34 |
| 4 | COUNT | | | | | | |
| 5 | VIEW | | 1 | 250 | | 2187 (6)| 00:00:34 |
| 6 | SORT ORDER BY | | 1 | 119 | | 2187 (6)| 00:00:34 |
| 7 | NESTED LOOPS | | 1 | 119 | | 2186 (6)| 00:00:34 |
|* 8 | HASH JOIN OUTER | | 1 | 92 | | 2185 (6)| 00:00:34 |
| 9 | VIEW | | 1 | 20 | | 51 (4)| 00:00:01 |
| 10 | SORT GROUP BY | | 1 | 7 | | 51 (4)| 00:00:01 |
| 11 | VIEW | | 1 | 7 | | 51 (4)| 00:00:01 |
| 12 | SORT UNIQUE | | 1 | 127 | | 51 (4)| 00:00:01 |
| 13 | NESTED LOOPS | | 1 | 127 | | 50 (2)| 00:00:01 |
|* 14 | HASH JOIN | | 1 | 68 | | 44 (3)| 00:00:01 |
|* 15 | TABLE ACCESS BY INDEX ROWID| SUBJECT_COMPONENT | 13 | 520 | | 40 (0)| 00:00:01 |
|* 16 | INDEX RANGE SCAN | SUBJECT_COMPONENT_ASSESS_TYPE | 1059 | | | 9 (0)| 00:00:01 |
|* 17 | INDEX RANGE SCAN | SUBJECT_GROUP_PK | 41 | 1148 | | 3 (0)| 00:00:01 |
|* 18 | INDEX RANGE SCAN | CEA_AUTOMATIC_ALLOCATION_STATS | 5 | 295 | | 6 (0)| 00:00:01 |
| 19 | VIEW | | 679 | 48888 | | 2133 (6)| 00:00:33 |
| 20 | SORT GROUP BY | | 679 | 25123 | | 2133 (6)| 00:00:33 |
|* 21 | HASH JOIN | | 52408 | 1893K| 1744K| 2126 (6)| 00:00:33 |
| 22 | TABLE ACCESS BY INDEX ROWID | IA_INSTANCES | 52408 | 1125K| | 688 (1)| 00:00:11 |
|* 23 | INDEX RANGE SCAN | IND_IA_INSTANCES | 49077 | | | 137 (2)| 00:00:03 |
| 24 | TABLE ACCESS FULL | WORKFLOW_INSTANCE | 1075K| 15M| | 960 (7)| 00:00:15 |
|* 25 | TABLE ACCESS BY INDEX ROWID | PERSON | 1 | 27 | | 1 (0)| 00:00:01 |
|* 26 | INDEX UNIQUE SCAN | PERSON_PK | 1 | | | 0 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter(TO_NUMBER(:V_START_ROW)<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1))
2 - filter("ROW_NUM">=TO_NUMBER(:V_START_ROW) AND "ROW_NUM"<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1))
8 - access("EXNAME"."EXAMINER_CODE"="IAF"."EXAMINER_CODE"(+))
14 - access("SC"."SUBJECT"="SG"."SUBJECT" AND "SC"."LVL"="SG"."LVL")
15 - filter((:V_SUBJECT IS NULL OR "SC"."SUBJECT"=:V_SUBJECT) AND ("SC"."LVL"=:V_LVL OR :V_LVL IS NULL))
16 - access("SC"."YEAR"=TO_NUMBER(:V_YEAR) AND "SC"."MONTH"=:V_MONTH)
17 - access("SG"."YEAR"=TO_NUMBER(:V_YEAR) AND "SG"."MONTH"=:V_MONTH)
filter(:V_SUBJECT_GROUP IS NULL OR "SG"."GROUP_NUMBER"=TO_NUMBER(:V_SUBJECT_GROUP))
18 - access("CEA"."YEAR"=TO_NUMBER(:V_YEAR) AND "CEA"."MONTH"=:V_MONTH AND "SC"."PAPER_CODE"="PAPER_CODE" AND
"CEA"."ELEMENT"='Moderation of IA')
filter("CEA"."ELEMENT"='Moderation of IA' AND (:V_PADDED_EXAMINER_CODE IS NULL OR
"EXAMINER_CODE"=:V_PADDED_EXAMINER_CODE))
21 - access("WFI"."INSTANCE_ID"="IA1"."WORKFLOW_INSTANCE_ID")
23 - access("IA1"."YEAR"=TO_NUMBER(:V_YEAR) AND "IA1"."MONTH"=:V_MONTH)
25 - filter(:V_EXAMINER_NAME IS NULL OR UPPER("TITLE"||' '||"FIRSTNAME"||' '||"LASTNAME") LIKE :V_SEARCH_EXAMINER_NAME)
26 - access("EXNAME"."EXAMINER_CODE"="PERSON_CODE")
53 rows selectedHi,
please find the below rigjt explan paln.
PLAN_TABLE_OUTPUT
SQL_ID 2ct41vyyzqyh7, child number 0
SELECT * FROM (SELECT q.*, COUNT(*) OVER() AS record_count, ROWNUM AS row_num FROM (SELECT
ExName.examiner_code, examiner_name, :v_year, :v_month, count_fb, NVL(count_entered_fb,
0) count_entered_fb, NVL(count_sent_fb, 0) count_sent_fb, NVL(count_edited_fb, 0) count_edited_fb,
NVL(count_complete_fb, 0) count_complete_fb, NVL(count_withibcardiff_fb, 0) count_withibcardiff_fb FROM
(SELECT examiner_code, COUNT(*) AS count_fb FROM (SELECT
examiner_code, paper_code, assessment_school FROM ( SELECT DISTINCT
ce.examiner_code, ce.paper_code, ce.assessment_school FROM (SELECT
DISTINCT assessment_school,
paper_code, examiner
Plan hash value: 651311258
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | | 2785 (100)| |
|* 1 | FILTER | | | | | | |
|* 2 | VIEW | | 4 | 1104 | | 2785 (7)| 00:00:43 |
| 3 | WINDOW BUFFER | | 4 | 1000 | | 2785 (7)| 00:00:43 |
| 4 | COUNT | | | | | | |
| 5 | VIEW | | 4 | 1000 | | 2785 (7)| 00:00:43 |
| 6 | NESTED LOOPS | | 4 | 476 | | 2785 (7)| 00:00:43 |
| 7 | MERGE JOIN OUTER | | 4 | 368 | | 2781 (7)| 00:00:43 |
| 8 | VIEW | | 4 | 80 | | 72 (3)| 00:00:02 |
| 9 | SORT GROUP BY | | 4 | 28 | | 72 (3)| 00:00:02 |
| 10 | VIEW | | 4 | 28 | | 72 (3)| 00:00:02 |
| 11 | SORT UNIQUE | | 4 | 508 | | 72 (3)| 00:00:02 |
| 12 | NESTED LOOPS | | 4 | 508 | | 71 (2)| 00:00:02 |
|* 13 | HASH JOIN | | 1 | 68 | | 44 (3)| 00:00:01 |
|* 14 | TABLE ACCESS BY INDEX ROWID| SUBJECT_COMPONENT | 13 | 520 | | 40 (0)| 00:00:01 |
|* 15 | INDEX RANGE SCAN | SUBJECT_COMPONENT_ASSESS_TYPE | 1059 | | | 9 (0)| 00:00:01 |
|* 16 | INDEX RANGE SCAN | SUBJECT_GROUP_PK | 41 | 1148 | | 3 (0)| 00:00:01 |
|* 17 | INDEX RANGE SCAN | CEA_AUTOMATIC_ALLOCATION_STATS | 30 | 1770 | | 27 (0)| 00:00:01 |
|* 18 | SORT JOIN | | 576 | 41472 | | 2709 (7)| 00:00:42 |
| 19 | VIEW | | 576 | 41472 | | 2708 (7)| 00:00:42 |
| 20 | SORT GROUP BY | | 576 | 21312 | | 2708 (7)| 00:00:42 |
|* 21 | HASH JOIN | | 52408 | 1893K| 1744K| 2701 (7)| 00:00:41 |
|* 22 | TABLE ACCESS FULL | IA_INSTANCES | 52408 | 1125K| | 1263 (6)| 00:00:20 |
| 23 | TABLE ACCESS FULL | WORKFLOW_INSTANCE | 1075K| 15M| | 960 (7)| 00:00:15 |
|* 24 | TABLE ACCESS BY INDEX ROWID | PERSON | 1 | 27 | | 1 (0)| 00:00:01 |
|* 25 | INDEX UNIQUE SCAN | PERSON_PK | 1 | | | 0 (0)| |
Predicate Information (identified by operation id):
1 - filter(TO_NUMBER(:V_START_ROW)<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1))
2 - filter(("ROW_NUM">=TO_NUMBER(:V_START_ROW) AND "ROW_NUM"<=TO_NUMBER(:V_START_ROW)+(TO_NUMBER(:V_MAX_ROW)-1)))
13 - access("SC"."SUBJECT"="SG"."SUBJECT" AND "SC"."LVL"="SG"."LVL")
14 - filter(((:V_SUBJECT IS NULL OR "SC"."SUBJECT"=:V_SUBJECT) AND ("SC"."LVL"=:V_LVL OR :V_LVL IS NULL)))
15 - access("SC"."YEAR"=TO_NUMBER(:V_YEAR) AND "SC"."MONTH"=:V_MONTH)
16 - access("SG"."YEAR"=TO_NUMBER(:V_YEAR) AND "SG"."MONTH"=:V_MONTH)
filter((:V_SUBJECT_GROUP IS NULL OR "SG"."GROUP_NUMBER"=TO_NUMBER(:V_SUBJECT_GROUP)))
17 - access("CEA"."YEAR"=TO_NUMBER(:V_YEAR) AND "CEA"."MONTH"=:V_MONTH AND "SC"."PAPER_CODE"="PAPER_CODE" AND
"CEA"."ELEMENT"='Moderation of IA')
filter(("CEA"."ELEMENT"='Moderation of IA' AND (:V_PADDED_EXAMINER_CODE IS NULL OR
"EXAMINER_CODE"=:V_PADDED_EXAMINER_CODE)))
18 - access("EXNAME"."EXAMINER_CODE"="IAF"."EXAMINER_CODE")
filter("EXNAME"."EXAMINER_CODE"="IAF"."EXAMINER_CODE")
21 - access("WFI"."INSTANCE_ID"="IA1"."WORKFLOW_INSTANCE_ID")
22 - filter(("IA1"."MONTH"=:V_MONTH AND "IA1"."YEAR"=TO_NUMBER(:V_YEAR)))
24 - filter((:V_EXAMINER_NAME IS NULL OR UPPER("TITLE"||' '||"FIRSTNAME"||' '||"LASTNAME") LIKE :V_SEARCH_EXAMINER_NAME))
25 - access("EXNAME"."EXAMINER_CODE"="PERSON_CODE")
66 rows selected -
Hi ,
Below query is taking more time (4mins) for 41 rows returning and there is no full table scan .Can some one help to rewrite the query.
SELECT
esma.candidate AS candidate_additional_sample,
ccr.raw_mark,
ccr.split_session_year AS year,
ccr.split_session_month AS month,
ccr.paper_code,
ccr.assessment_school AS school_code,
get_cand_session_number(ccr.split_session_year,ccr.split_session_month,ccr.candidate) AS candidate_session_number,
ccr.candidate AS candidate_code,
c.firstname AS candidate_firstname,
c.lastname AS candidate_lastname,
ema.moderator_code,
ema.examiner_code,
csr.self_taught
FROM
ibis.candidate_component_reg ccr
INNER JOIN ibis.candidate_examiner_allocation cea
ON ccr.split_session_year= cea.year
AND ccr.split_session_month=cea.month
AND ccr.assessment_school=cea.assessment_school
AND ccr.paper_code= cea.paper_code
AND ccr.candidate = cea.candidate
INNER JOIN ibis.examination_paper ep
ON ccr.split_session_year= ep.year
AND ccr.split_session_month=ep.month
AND ccr.paper_code=ep.paper_code
INNER JOIN ibis.examiner_moderator_allocation ema
ON ema.year =cea.year
AND ema.month =cea.month
AND ema.no_lang_code=ep.no_lang_code
AND ema.examiner_code=cea.examiner_code
AND ema.year =ccr.split_session_year
AND ema.month =ccr.split_session_month
INNER JOIN ibis.candidate_subject_registration csr
ON ccr.split_session_year=csr.year
AND ccr.split_session_month=csr.month
AND ccr.main_school=csr.main_school
AND ccr.candidate=csr.candidate
AND ccr.subject=csr.subject
AND ccr.subject_option=csr.subject_option
AND ccr.lvl=csr.lvl
AND ccr.language=csr.language
INNER JOIN ibis.candidate c
ON ccr.candidate=c.candidate
LEFT OUTER JOIN ibis.exam_sample_mark_additional esma
ON ccr.split_session_year = esma.year
AND ccr.split_session_month = esma.month
AND ep.no_lang_code = esma.nolang_code
AND ccr.candidate = esma.candidate
WHERE
ccr.split_session_year=:v_year
AND ccr.split_session_month=:v_month
AND ccr.subject=:v_subject
AND ccr.subject_option=nvl(:v_subject_option,ccr.subject_option)
AND ccr.language=nvl(:v_language, ccr.language)
AND ccr.component=:v_component
AND ccr.lvl=:v_lvl
AND ccr.raw_mark IS NOT NULL
AND cea.examiner_code = :v_examiner_code
AND ((ccr.sampled='Y'
AND csr.self_taught='N'
AND EXISTS (SELECT 1 FROM ibis.subject_component sc
WHERE sc.assessment_method_id = 11
AND sc.assessment_type='INTERNAL'
AND ccr.split_session_year=sc.year
AND ccr.split_session_month=sc.month
AND ccr.paper_code=sc.paper_code
AND ccr.subject=sc.subject
AND ccr.subject_option=sc.subject_option
AND ccr.lvl=sc.lvl
AND ccr.language=sc.language
AND ccr.component=sc.component)
AND NOT EXISTS (SELECT 1 FROM ibis.cw_portfolio_template cpt
WHERE ccr.split_session_year = cpt.year
AND ccr.split_session_month = cpt.month
AND ccr.subject = cpt.subject
AND ccr.subject_option = cpt.subject_option
AND ccr.component = cpt.component
AND cpt.combined = 'Y')
AND NOT EXISTS (SELECT 1 FROM ibis.school_sample_mark ssm
WHERE ssm.year= ccr.split_session_year
AND ssm.month = ccr.split_session_month
AND ssm.paper_code = ccr.paper_code
AND ssm.candidate =ccr.candidate
AND ssm.examiner_sample = 'Y'))
OR (ccr.sampled='N'
AND csr.self_taught='Y'
AND EXISTS (SELECT 1 FROM ibis.subject_component sc
WHERE sc.assessment_method_id = 11
AND ccr.split_session_year=sc.year
AND ccr.split_session_month=sc.month
AND ccr.paper_code=sc.paper_code
AND ccr.subject=sc.subject
AND ccr.subject_option=sc.subject_option
AND ccr.lvl=sc.lvl
AND ccr.language=sc.language
AND ccr.component=sc.component)
AND NOT EXISTS (SELECT 1 FROM ibis.cw_portfolio_template cpt
WHERE ccr.split_session_year = cpt.year
AND ccr.split_session_month = cpt.month
AND ccr.subject = cpt.subject
AND ccr.subject_option = cpt.subject_option
AND ccr.component = cpt.component
AND cpt.combined = 'Y'))
OR (ccr.sampled='N'
AND EXISTS (SELECT 1 FROM ibis.subject_component sc
WHERE sc.assessment_method_id = 11
AND ccr.split_session_year=sc.year
AND ccr.split_session_month=sc.month
AND ccr.paper_code=sc.paper_code
AND ccr.subject=sc.subject
AND ccr.subject_option=sc.subject_option
AND ccr.lvl=sc.lvl
AND ccr.language=sc.language
AND ccr.component=sc.component)
AND EXISTS (SELECT 1 FROM ibis.cw_portfolio_template cpt
WHERE ccr.split_session_year = cpt.year
AND ccr.split_session_month = cpt.month
AND ccr.subject = cpt.subject
AND ccr.subject_option = cpt.subject_option
AND ccr.component = cpt.component
AND cpt.combined = 'Y')));
explain plan
line 1: SQLPLUS Command Skipped: set linesize 130
line 2: SQLPLUS Command Skipped: set pagesize 0
PLAN_TABLE_OUTPUT
Plan hash value: 2594643366
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 2 | 1036 | 34 (3)| 00:00:01 |
|* 1 | FILTER | | | | | |
|* 2 | HASH JOIN OUTER | | 2 | 1036 | 34 (3)| 00:00:01 |
| 3 | VIEW | | 2 | 974 | 32 (0)| 00:00:01 |
| 4 | CONCATENATION | | | | | |
|* 5 | FILTER | | | | | |
|* 6 | TABLE ACCESS BY INDEX ROWID | CANDIDATE_SUBJECT_REGISTRATION | 1 | 58 | 3 (0)| 00:00:01 |
| 7 | NESTED LOOPS | | 1 | 485 | 16 (0)| 00:00:01 |
| 8 | NESTED LOOPS | | 1 | 427 | 13 (0)| 00:00:01 |
| 9 | NESTED LOOPS | | 1 | 357 | 11 (0)| 00:00:01 |
| 10 | NESTED LOOPS | | 1 | 129 | 8 (0)| 00:00:01 |
| 11 | NESTED LOOPS | | 1 | 80 | 5 (0)| 00:00:01 |
|* 12 | INDEX RANGE SCAN | PK_EXAMINER_MODERATOR_ALLOC | 1 | 39 | 3 (0)| 00:00:01 |
|* 13 | INDEX RANGE SCAN | RFL_EP1 | 1 | 41 | 2 (0)| 00:00:01 |
| 14 | TABLE ACCESS BY INDEX ROWID| CANDIDATE_EXAMINER_ALLOCATION | 1 | 49 | 3 (0)| 00:00:01 |
|* 15 | INDEX RANGE SCAN | CANDIDATE_ALLOCATION_EXAMINER | 1 | | 2 (0)| 00:00:01 |
|* 16 | TABLE ACCESS BY INDEX ROWID | CANDIDATE_COMPONENT_REG | 1 | 228 | 3 (0)| 00:00:01 |
|* 17 | INDEX RANGE SCAN | SPLIT_SESSION_CANDIDATE | 1 | | 2 (0)| 00:00:01 |
| 18 | TABLE ACCESS BY INDEX ROWID | CANDIDATE | 1 | 70 | 2 (0)| 00:00:01 |
|* 19 | INDEX UNIQUE SCAN | CANDIDATE_PK | 1 | | 1 (0)| 00:00:01 |
|* 20 | INDEX RANGE SCAN | Y_M_CAND_SUBJ_LVL_OPTION | 1 | | 2 (0)| 00:00:01 |
|* 21 | FILTER | | | | | |
|* 22 | TABLE ACCESS BY INDEX ROWID | CANDIDATE_SUBJECT_REGISTRATION | 1 | 58 | 3 (0)| 00:00:01 |
| 23 | NESTED LOOPS | | 1 | 485 | 16 (0)| 00:00:01 |
| 24 | NESTED LOOPS | | 1 | 427 | 13 (0)| 00:00:01 |
| 25 | NESTED LOOPS | | 1 | 357 | 11 (0)| 00:00:01 |
| 26 | NESTED LOOPS | | 1 | 129 | 8 (0)| 00:00:01 |
| 27 | NESTED LOOPS | | 1 | 80 | 5 (0)| 00:00:01 |
|* 28 | INDEX RANGE SCAN | PK_EXAMINER_MODERATOR_ALLOC | 1 | 39 | 3 (0)| 00:00:01 |
|* 29 | INDEX RANGE SCAN | RFL_EP1 | 1 | 41 | 2 (0)| 00:00:01 |
| 30 | TABLE ACCESS BY INDEX ROWID| CANDIDATE_EXAMINER_ALLOCATION | 1 | 49 | 3 (0)| 00:00:01 |
|* 31 | INDEX RANGE SCAN | CANDIDATE_ALLOCATION_EXAMINER | 1 | | 2 (0)| 00:00:01 |
|* 32 | TABLE ACCESS BY INDEX ROWID | CANDIDATE_COMPONENT_REG | 1 | 228 | 3 (0)| 00:00:01 |
|* 33 | INDEX RANGE SCAN | SPLIT_SESSION_CANDIDATE | 1 | | 2 (0)| 00:00:01 |
| 34 | TABLE ACCESS BY INDEX ROWID | CANDIDATE | 1 | 70 | 2 (0)| 00:00:01 |
|* 35 | INDEX UNIQUE SCAN | CANDIDATE_PK | 1 | | 1 (0)| 00:00:01 |
|* 36 | INDEX RANGE SCAN | Y_M_CAND_SUBJ_LVL_OPTION | 1 | | 2 (0)| 00:00:01 |
|* 37 | INDEX RANGE SCAN | EXAM_SAMP_MARK_ADD_IOT_PK | 36 | 1116 | 1 (0)| 00:00:01 |
|* 38 | TABLE ACCESS BY INDEX ROWID | CW_PORTFOLIO_TEMPLATE | 1 | 46 | 4 (0)| 00:00:01 |
|* 39 | INDEX RANGE SCAN | YEAR_MON_PORTFOLIO_TEMPLATE | 1 | | 3 (0)| 00:00:01 |
|* 40 | TABLE ACCESS BY INDEX ROWID | SCHOOL_SAMPLE_MARK | 1 | 34 | 4 (0)| 00:00:01 |
|* 41 | INDEX RANGE SCAN | CAND_PAPER_YEAR_MONTH | 1 | | 3 (0)| 00:00:01 |
|* 42 | TABLE ACCESS BY INDEX ROWID | SUBJECT_COMPONENT | 1 | 96 | 4 (0)| 00:00:01 |
|* 43 | INDEX RANGE SCAN | SUBJECT_COMPONENT_PK | 1 | | 3 (0)| 00:00:01 |
|* 44 | TABLE ACCESS BY INDEX ROWID | CW_PORTFOLIO_TEMPLATE | 1 | 46 | 4 (0)| 00:00:01 |
|* 45 | INDEX RANGE SCAN | YEAR_MON_PORTFOLIO_TEMPLATE | 1 | | 3 (0)| 00:00:01 |
|* 46 | TABLE ACCESS BY INDEX ROWID | SUBJECT_COMPONENT | 1 | 87 | 4 (0)| 00:00:01 |
|* 47 | INDEX RANGE SCAN | SUBJECT_COMPONENT_PK | 1 | | 3 (0)| 00:00:01 |
|* 48 | TABLE ACCESS BY INDEX ROWID | SUBJECT_COMPONENT | 1 | 87 | 4 (0)| 00:00:01 |
|* 49 | INDEX RANGE SCAN | SUBJECT_COMPONENT_PK | 1 | | 3 (0)| 00:00:01 |
|* 50 | TABLE ACCESS BY INDEX ROWID | CW_PORTFOLIO_TEMPLATE | 1 | 46 | 4 (0)| 00:00:01 |
|* 51 | INDEX RANGE SCAN | YEAR_MON_PORTFOLIO_TEMPLATE | 1 | | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - filter("from$_subquery$_011"."SAMPLED"='Y' AND "from$_subquery$_011"."SELF_TAUGHT"='N' AND NOT EXISTS
(SELECT /*+ */ 0 FROM "IBIS"."CW_PORTFOLIO_TEMPLATE" "CPT" WHERE "CPT"."COMPONENT"=:B1 AND
"CPT"."SUBJECT_OPTION"=:B2 AND "CPT"."SUBJECT"=:B3 AND "CPT"."MONTH"=:B4 AND "CPT"."YEAR"=:B5 AND
"CPT"."COMBINED"='Y') AND NOT EXISTS (SELECT /*+ */ 0 FROM "IBIS"."SCHOOL_SAMPLE_MARK" "SSM" WHERE
"SSM"."MONTH"=:B6 AND "SSM"."YEAR"=:B7 AND "SSM"."PAPER_CODE"=:B8 AND "SSM"."CANDIDATE"=:B9 AND
"SSM"."EXAMINER_SAMPLE"='Y') AND EXISTS (SELECT /*+ */ 0 FROM "IBIS"."SUBJECT_COMPONENT" "SC" WHERE
"SC"."COMPONENT"=:B10 AND "SC"."SUBJECT_OPTION"=:B11 AND "SC"."LANGUAGE"=:B12 AND "SC"."LVL"=:B13 AND
"SC"."SUBJECT"=:B14 AND "SC"."MONTH"=:B15 AND "SC"."YEAR"=:B16 AND "SC"."PAPER_CODE"=:B17 AND
"SC"."ASSESSMENT_METHOD_ID"=11 AND "SC"."ASSESSMENT_TYPE"='INTERNAL') OR "from$_subquery$_011"."SAMPLED"='N'
AND "from$_subquery$_011"."SELF_TAUGHT"='Y' AND NOT EXISTS (SELECT /*+ */ 0 FROM
"IBIS"."CW_PORTFOLIO_TEMPLATE" "CPT" WHERE "CPT"."COMPONENT"=:B18 AND "CPT"."SUBJECT_OPTION"=:B19 AND
"CPT"."SUBJECT"=:B20 AND "CPT"."MONTH"=:B21 AND "CPT"."YEAR"=:B22 AND "CPT"."COMBINED"='Y') AND EXISTS (SELECT
/*+ */ 0 FROM "IBIS"."SUBJECT_COMPONENT" "SC" WHERE "SC"."COMPONENT"=:B23 AND "SC"."SUBJECT_OPTION"=:B24 ANDEdited byCan you please post the following scripts:
1. Tables structure (with all indexes & constraints)
2. Tables data
3. SQL trace file
possibly everything that one can need to regenerate the scenario on his own DB...
OR
spend some time with Oracle Docs on Performance Tuning and then resolve it yourself.
Regards,
Ankit Rathi
http://theoraclelog.blogspot.in -
Need help with sql query dates
Hi,
I have a sql query where i need to extract some info between given dates. The where clause of this query is as follows:
WHERE CPD_BUS_UNIT=:ESI_PRM_1
AND CPD_VOUCHER_DATE >= :P_DATE_FROM
AND CPD_VOUCHER_DATE < (:P_DATE_TO+1)
When i execute the query in toad, i can view the data but not the execution plan.It gives an error ORA-00932-Inconsistent Datatypes.
But when i remove (+1) from :P_DATE_TO, i can c the execution plan and data. Will the data be different from the previous one.
Please suggest how to rewrite the query.Can you please give it a try?
WHERE CPD_BUS_UNIT=:ESI_PRM_1
AND CPD_VOUCHER_DATE >= :to_date(P_DATE_FROM)
AND CPD_VOUCHER_DATE < (to_date(:P_DATE_TO)+1) Regards -
Need help writing a query for following scenario
Hi all, I need some help writing a query for the following case:
One Table : My_Table
Row Count: App 5000
Columns of Interest: AA and BB
Scenario: AA contains some names of which BB contains the corresponding ID. Some
names are appearing more than once with different IDs. For example,
AA BB
Dummy 10
Me 20
Me 30
Me 40
You 70
Me 50
Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
I would appreciate any input. ThanksIs it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
WITH t AS (
SELECT 'me' aa, 10 bb FROM dual
UNION ALL
SELECT 'me' aa, 20 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
SELECT DISTINCT aa, bb
FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
WHERE cnt > 1; -
Hello,
I need to rewrite this SQL query to give the same result, but in more resorcefull manner.
Probably using aggregation, but I am not sure.
Can somebody give me a head start.
desc resource_bundles
Name Null Type
LOCALE NOT NULL CHAR(5)
BUNDLE_NAME NOT NULL VARCHAR2(32)
RESOURCE_NAME NOT NULL VARCHAR2(64)
VALUE_STRING VARCHAR2(2048 CHAR)
select a.bundle_name,a.resource_name, a.value_string as Czech,(select b.value_string
from resource_bundles b
where b.bundle_name = a.bundle_name
and b.resource_name = a.resource_name
and b.locale = 'en_US'
)as English,
(select c.value_string
from resource_bundles c
where c.bundle_name = a.bundle_name
and c.resource_name = a.resource_name
and c.locale = 'ru_RU'
) as Russian,
(select d.value_string
from resource_bundles d
where d.bundle_name = a.bundle_name
and d.resource_name = a.resource_name
and d.locale = 'it_IT'
) as Italian
from resource_bundles a
where a.locale = 'cs_CZ';Thank you
Edited by: BluShadow on 15-Apr-2011 12:20
added {noformat}{noformat} tagsIt is always helpful to provide the following:
1. Oracle version (SELECT * FROM V$VERSION)
2. Sample data in the form of CREATE / INSERT statements.
3. Expected output
4. Explanation of expected output (A.K.A. "business logic")
5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details.
Also see the third post in this thread:
{thread:id=2174552} -
Hi All,
DB version is 10.2.0.4
Could see latch: cache buffers chains on a sid which is running a query for hours together. The query and the explain plan is below
explain plan for SELECT MIN(LINE_ID),MIN(LINE_DETAIL_ID),MAX(LOT_ID) FROM IC_TRAN_PND WHERE DOC_TYPE = 'OMSO' AND DELETE_MARK= 0 AND COMPLETED_IND = 1 AND LOT_STATUS =
'GOOD' AND LOT_ID IN (SELECT LOT_ID FROM IC_LOTS_MST WHERE LOT_NO = :B1 );
Explained.
PLAN_TABLE_OUTPUT
Plan hash value: 2243190951
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 46 | 26 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 46 | | |
|* 2 | TABLE ACCESS BY INDEX ROWID | IC_TRAN_PND | 30677 | 838K| 4 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 138K| 6211K| 26 (0)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID| IC_LOTS_MST | 5 | 90 | 6 (0)| 00:00:01 |
|* 5 | INDEX RANGE SCAN | IC_LOTS_MST_I1 | 5 | | 3 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IC_TRAN_PND2 | 1 | | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - filter("LOT_STATUS"='GOOD')
5 - access("LOT_NO"=:B1)
6 - access("LOT_ID"="LOT_ID" AND "DELETE_MARK"=0 AND "DOC_TYPE"='OMSO' AND
"COMPLETED_IND"=1)
21 rows selected.Tried rewriting the query to
SELECT MIN(LINE_ID),MIN(LINE_DETAIL_ID),MAX(b.LOT_ID) FROM IC_TRAN_PND a,IC_LOTS_MST B WHERE a.DOC_TYPE = 'OMSO' AND a.DELETE_MARK= 0 AND a.COMPLETED_IND = 1 AND a.LOT_STATUS =
'GOOD' AND a.LOT_ID=b.LOT_ID and b.LOT_NO = :B1 Not sure if the above satifies the originial query conditions. The xplain plan remains the same for the second one too. How do i rewrite the query and avoid the latch buffer chains.
baskar.lhi,
explain plan for SELECT MIN (LINE_ID), MIN (LINE_DETAIL_ID), MAX (LOT_ID)
2 FROM IC_TRAN_PND A
3 WHERE DOC_TYPE = 'OMSO'
4 AND DELETE_MARK = 0
5 AND COMPLETED_IND = 1
6 AND LOT_STATUS = 'GOOD'
7 AND EXISTS
8 (SELECT 'X'
9 FROM IC_LOTS_MST B
10 WHERE B.LOT_NO = :B1
11 AND A.LOT_ID = B.LOT_ID);
Explained.
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 46 | 19 (6)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 46 | | |
|* 2 | TABLE ACCESS BY INDEX ROWID | IC_TRAN_PND | 30677 | 838K| 4 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 138K| 6211K| 19 (6)| 00:00:01 |
| 4 | SORT UNIQUE | | 5 | 90 | 6 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| IC_LOTS_MST | 5 | 90 | 6 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IC_LOTS_MST_I1 | 5 | | 3 (0)| 00:00:01 |
|* 7 | INDEX RANGE SCAN | IC_TRAN_PND2 | 1 | | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------Is that distinct will return duplicate rows too??
baskar.l -
Please, need help with a query
Hi !
Please need help with this query:
Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
order by a.create_dateTake a look on the syntax :
max(...) keep (dense_rank last order by ...)
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#i1000901
Nicolas. -
Please need help with this query
Hi !
Please need help with this query:
Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
order by a.create_datePerhaps something like this...
select id, create_date, loanid, rate, pays, gracetime, emailtosend, first_name, last_name, user_id
from (
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id,
max(create_date) over (partition by a.user_id, a.loadid) as max_create_date
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
where create_date = max_create_date
order by create_date -
3 Table Joins -- Need a more efficient Query
I need a 3 table join but need to do it more efficiently than I am currently doing. The query is taking too long to execute (in excess of 20 mins. These are huge tables with 10 mil + records). Here is what the query looks like right now. I need 100 distinct acctnum from the below query with all the conditions as requirements.
THANKS IN ADVANCE FOR HELP!!!
SELECT /*+ parallel */
FROM (SELECT /*+ parallel */ DISTINCT (a.acctnum),
a.acctnum_status,
a.sys_creation_date,
a.sys_update_date,
c.comp_id,
c.comp_lbl_type,
a.account_sub_type
FROM account a
LEFT JOIN
company c
ON a.comp_id = c.comp_id AND c.comp_lbl_type = 'IND',
subaccount s
WHERE a.account_type = 'I'
AND a.account_status IN ('O', 'S')
and s.subaccount_status in ('A','S')
AND a.account_sub_type NOT IN ('G', 'V')
AND a.SYS_update_DATE <= SYSDATE - 4 / 24)
where ROWNUM <= 100 ;Hi,
Whenever you have a question, post CREATE TABLE and INSERT statements for a little sample data, and the results you want from that data. Explain how you get those results from that data.
Simplify the problem, if possible. If you need 100 distinct rows, post a problem where you only need, say, 3 distinct rows. Just explain that you really need 100, and you'll get a solution that works for either 3 or 100.
Always say which version of Oracle you're using (e.g. 11.2.0.3.0).
See the forum FAQ: https://forums.oracle.com/message/9362002
For tuning problems, also see https://forums.oracle.com/message/9362003
Are you sure the query you posted is even doing what you want? You're cross-joining s to the other tables, producing all possible combinations of rows, and then picking 100 of those in no particular order (not even random order). That's not necessarily wrong, but it certainly is suspicious.
If you're only interested in 100 rows, there's probably some way to write the query so that it picks 100 rows from the big tables first. -
hi ,
Actual i write the query like this , and run this query in sql developer ,
it will take more than 15 mins
alloc_details table -- 2 lac records
doc_details -- 2 lac records
query :
SELECT
a.activity
, sum( a.lamt ) lamt
, sum( a.lamt ) camt
FROM
alloc_detail a
WHERE
a.alc_code <>'JOB'
And a.alc_value is not null
And EXISTS ( SELECT
distinct b.doc_no
FROM
doc_detail b
WHERE
a.org=b.org
And a.doc_no=b.doc_no
And a.doc_type=b.doc_type
And ( b.dr_act_code='11111' OR b.cr_act_code='111111')
And ( ( trunc( b.doc_date ) >='01-jan-08' ) )
And ( ( trunc( b.doc_date ) <= '31-jan-08' ) )
GROUP BY
a.alc_value
Anyother way to rewrite the query .Is this query working? a.activity seems to be a "not a group by expression"
select a.activity,sum(a.lamt) lamt,sum(a.lamt) camt /* 2 times same sum - a typo ? */
from alloc_detail a
where a.alc_code != 'JOB' /* not equal presumed */
and a.alc_value is not null
and exists(select distinct b.doc_no
from doc_detail b
where a.org = b.org
and a.doc_no = b.doc_no
and a.doc_type = b.doc_type
and (b.dr_act_code = '11111' or b.cr_act_code = '111111')
and trunc(b.doc_date) >= '01-jan-08'
and trunc(b.doc_date) <= '31-jan-08'
group by a.alc_valuehow about applying predicates first
select a.alc_value,sum(a.lamt) lamt,sum(a.camt) camt
from (select alc_value,lamt,camt,org,doc_no,doc_type
from alloc_detail
where alc_code != 'JOB'
and alc_value is not null
) a,
(select distinct org,doc_no,doc_type
from doc_detail
where doc_date >= to_date('01-jan-08','dd-mon-rr')
and doc_date <= to_date('31-jan-08','dd-mon-rr') + 1 - 1/24/60/60
and (dr_act_code = '11111' or cr_act_code = '111111')
) b
where a.org = b.org
and a.doc_no = b.doc_no
and a.doc_type = b.doc_type
group by a.alc_valueRegards
Etbin -
Need help th tuning query or re write the query--
Hi,
Need help to tune the below query or rewrite th query for reducing the execution time Please find the query and explain plan.
QUERY
explain plan FOR SELECT consumer_key,product_key,days_in_product,20100201 period_key FROM
(SELECT consumer_key,
product_key,
days_in_product,
row_number() over ( Partition BY consumer_key order by Days_in_product DESC) row_num
FROM
(SELECT consumer_key,
product_key,
SUM(no_ofdays) days_in_product
FROM
(SELECT pcv.consumer_key,
pcv.product_key,
pcv.product_consumer_valid_from,
pcv.product_consumer_valid_to,
DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from) period_start,
DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959) period_end,
CASE
WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
AND activation_date > to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
THEN 0
WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) BETWEEN 20100201000000 AND 20100228235959
AND activation_date BETWEEN to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') AND to_Date(product_consumer_valid_to,'YYYYMMDDHH24MISS')
THEN
--to_char(activation_date,'MON-YYYY')='PERIOD_ACTIVE' and activation_date >= to_Date(product_consumer_valid_from,'YYYYMMDDHH24MISS') then
(to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_date(TO_CHAR(activation_date,'YYYYMMDDHH24MISS'),'YYYYMMDDHH24MISS') )
WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) < 20100201000000
THEN (to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
WHEN to_number(TO_CHAR(cd.activation_date,'YYYYMMDDHH24MISS')) > 20100228235959
THEN 0
ELSE
--unusual situation
(to_date(DECODE (SIGN(20100228235959-product_consumer_valid_to),1,product_consumer_valid_to,20100228235959),'YYYYMMDDHH24MISS') - to_Date(DECODE (SIGN(20100201000000-product_consumer_valid_from),1,20100201000000,product_consumer_valid_from),'YYYYMMDDHH24MISS') )
END No_ofDays
FROM cimtran.product_consumer_validity pcv,
consumer_dimension cd
WHERE pcv.consumer_key =cd.consumer_key
AND product_consumer_valid_to >= 20100201000000
AND product_consumer_valid_from <= 20100228235959
--and product_consumer_valid_from > '20090801000000'
ORDER BY consumer_key,
product_key,
product_consumer_valid_from
) a
GROUP BY consumer_key,
product_key
ORDER BY consumer_key,
product_key
) WHERE row_num=1 ;EXPLAIN PLAN
"PLAN_TABLE_OUTPUT"
"Plan hash value: 3823907703"
"| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |"
"| 0 | SELECT STATEMENT | | 4665K| 231M| | 133K (1)| 00:31:08 |"
"|* 1 | VIEW | | 4665K| 231M| | 133K (1)| 00:31:08 |"
"|* 2 | WINDOW SORT PUSHED RANK| | 4665K| 173M| 232M| 133K (1)| 00:31:08 |"
"| 3 | VIEW | | 4665K| 173M| | 104K (1)| 00:24:18 |"
"| 4 | SORT GROUP BY | | 4665K| 182M| 729M| 104K (1)| 00:24:18 |"
"|* 5 | HASH JOIN | | 13M| 533M| 65M| 44241 (1)| 00:10:20 |"
"| 6 | TABLE ACCESS FULL | CONSUMER_DIMENSION | 2657K| 35M| | 4337 (1)| 00:01:01 |"
"|* 7 | TABLE ACCESS FULL | PRODUCT_CONSUMER_VALIDITY | 13M| 351M| | 15340 (2)| 00:03:35 |"
"Predicate Information (identified by operation id):"
" 1 - filter(""ROW_NUM""=1)"
" 2 - filter(ROW_NUMBER() OVER ( PARTITION BY ""CONSUMER_KEY"" ORDER BY "
" INTERNAL_FUNCTION(""DAYS_IN_PRODUCT"") DESC )<=1)"
" 5 - access(""PCV"".""CONSUMER_KEY""=""CD"".""CONSUMER_KEY"")"
" 7 - filter(""PRODUCT_CONSUMER_VALID_FROM""<=20100228235959 AND "
" ""PRODUCT_CONSUMER_VALID_TO"">=20100201000000)"I doubt that this query can be tuned without using indexes. There is a lot of unnecessary work specified in your query, like unnecessary intermediate sorting and selecting unused columns. The cost based optimizer recognized it and skips some of that unnecessary work, it seems. For clarity's sake, I would rewrite your query like below. Note that the query is untested:
select consumer_key
, max(product_key) keep (dense_rank last order by days_in_product) product_key
, max(days_in_product) days_in_product
, 20100201 period_key
from ( select pcv.consumer_key
, pcv.product_key
, sum
( case
when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) between 20100201000000 and 20100228235959
then
case
when cd.activation_date > to_date(pcv.product_consumer_valid_to,'yyyymmddhh24miss')
then
0
when cd.activation_date between to_date(pcv.product_consumer_valid_from,'yyyymmddhh24miss') and to_date(product_consumer_valid_to,'yyyymmddhh24miss')
then
to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
- to_date(to_char(activation_date,'yyyymmddhh24miss'),'yyyymmddhh24miss')
end
when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) < 20100201000000
then
to_date(to_char(pcv.product_consumer_valid_to),'yyyymmddhh24miss'))
- to_date(to_char(pcv.product_consumer_valid_from),'yyyymmddhh24miss'))
when to_number(to_char(cd.activation_date,'yyyymmddhh24miss')) > 20100228235959
then
0
end
) days_in_product
from cimtran.product_consumer_validity pcv
, consumer_dimension cd
where pcv.consumer_key = cd.consumer_key
and product_consumer_valid_to >= 20100201000000
and product_consumer_valid_from <= 20100228235959
group by consumer_key
, product_key
group by consumer_keyRegards,
Rob.
Maybe you are looking for
-
Problem Crystal Report on SAP 8.8
Hi all! I need to modify an addon developed for SAP2007 so that it works even on SAP 8.8. Now, i have an error when i try to show a report made with Crystal Report. I know that in SAP 8.8, Crystal Reports are seamlessly integrated in the core product
-
How to use a CSS file in a jspx page..
I'm using JDev 10.1.3.4. I would like to know how can i use a CSS file in a JSPX page.. there is this file: public_html\WEB-INF\temp\adf\styles\cache\oracle-desktop-10_1_3_4_0-en-ie-6-windows-s.css How do I make use of this is my jspx page? Also how
-
Error when running oracle report - Cannot locate the font file 'arial.ttf'
Hello I am running a report and I am getting the following error. Terminated with error: <br>REP-3316: Cannot locate the font file 'arial.ttf' REP-1924: Any help appreciated. David
-
Can I use a G3 iMac hard drive as an external on my Macbook?
Hopefully this didn't post twice, the forum is giving me problems when I hit post. So sorry if this is posted twice. Does anybody know if I would be able to use the internal hard drive on my G3 iMac (333mhz, OS 8.6) as an external on my late 2009 Mac
-
Registration problems using itunes Music Card
Hi - anyone out there having a problem registering with a music card? Seems music store has acknowledged redemption of card but will not allow registration because payment field demands credit card details which, naturally,I don't want to give! Also