Performance tuning of sql query with multiple joins
My query takes at least half an hour to execute and the number of records returned are approx 1 lakh records.
Structure of tables are:
tblSession : ID,option1,option2,option3,option4,option5,option6,option7,option8,option9.
tblOption : ID, labelID
tblLabelDetail : ID, LABELID, text
optionID 1 to optionID9 are Foreign keys to table tblOption.ID
My query is as below :
select
session.ID
,session.tstamp
,session.score
,session.hid1
,session.hID2
,session.hID3
,session.collectionID
,session.consumerID
,session.langID
,cons_cust.text1 as customCons_text1,
cons_cust.text2 as customCons_text2,
cons_cust.text3 as customCons_text3,
cons_cust.text4 as customCons_text4,
cons_cust.text5 as customCons_text5,
cons_cust.text6 as customCons_text6,
cons_cust.text7 as customCons_text7,
cons_cust.text8 as customCons_text8,
cons_cust.text9 as customCons_text9,
ld_cons1.text as customCons_option1GUID,
ld_cons2.text as customCons_option2GUID,
ld_cons3.text as customCons_option3GUID,
ld_cons4.text as customCons_option4GUID ,
ld_cons5.text as customCons_option5GUID,
ld_cons6.text as customCons_option6GUID,
ld_cons7.text as customCons_option7GUID,
ld_cons8.text as customCons_option8GUID,
ld_cons9.text as customCons_option9GUID,
--session
session_cust.text1 as session_cust_text1,
session_cust.text2 as session_cust_text2,
session_cust.text3 as session_cust_text3,
session_cust.text4 as session_cust_text4,
session_cust.text5 as session_cust_text5,
session_cust.text6 as session_cust_text6,
session_cust.text7 as session_cust_text7,
session_cust.text8 as session_cust_text8,
session_cust.text9 as session_cust_text9,
ld_sess1.text as session_cust_option1GUID,
ld_sess2.text as session_cust_option2GUID,
ld_sess3.text as session_cust_option3GUID,
ld_sess4.text as session_cust_option4GUID,
ld_sess5.text as session_cust_option5GUID,
ld_sess6.text as session_cust_option6GUID,
ld_sess7.text as session_cust_option7GUID,
ld_sess8.text as session_cust_option8GUID,
ld_sess9.text as session_cust_option9GUID,
session_cust.tStamp1,
session_cust.tStamp2
from mvSession session with (noexpand)
inner join tblCollection c on c.ID=session.collectionID AND c.templateID = 405
left join tblConsumer cons on cons.ID=session.consumerID and cons.sessionYM between 601 and 1412 and cons.sID=105
left join vCustomConsumer cons_cust on cons_cust.sessionYM between 601 and 1412 and cons_cust.sID=105 and cons_cust.ID=cons.ID
left join tbloption o_cons1 on o_cons1.id = cons_cust.option1 and o_cons1.sid = 105
left join tblLabelDetail ld_cons1 on ld_cons1.labelID = o_cons1.labelID and ld_cons1.langId = 1 and ld_cons1.eid = 107
left join tbloption o_cons2 on o_cons2.id = cons_cust.option2 and o_cons2.sid = 105
left join tblLabelDetail ld_cons2 on ld_cons2.labelID = o_cons2.labelID and ld_cons2.langId = 1 and ld_cons2.eid = 107
left join tbloption o_cons3 on o_cons3.id = cons_cust.option3 and o_cons3.sid = 105
left join tblLabelDetail ld_cons3 on ld_cons3.labelID = o_cons1.labelID and ld_cons3.langId = 1 and ld_cons3.eid = 107
left join tbloption o_cons4 on o_cons4.id = cons_cust.option4 and o_cons4.sid = 105
left join tblLabelDetail ld_cons4 on ld_cons4.labelID = o_cons4.labelID and ld_cons4.langId = 1 and ld_cons4.eid = 107
left join tbloption o_cons5 on o_cons5.id = cons_cust.option5 and o_cons5.sid = 105
left join tblLabelDetail ld_cons5 on ld_cons5.labelID = o_cons5.labelID and ld_cons5.langId = 1 and ld_cons5.eid = 107
left join tbloption o_cons6 on o_cons6.id = cons_cust.option6 and o_cons6.sid = 105
left join tblLabelDetail ld_cons6 on ld_cons6.labelID = o_cons6.labelID and ld_cons6.langId = 1 and ld_cons6.eid = 107
left join tbloption o_cons7 on o_cons7.id = cons_cust.option7 and o_cons7.sid = 105
left join tblLabelDetail ld_cons7 on ld_cons7.labelID = o_cons7.labelID and ld_cons7.langId = 1 and ld_cons7.eid = 107
left join tbloption o_cons8 on o_cons8.id = cons_cust.option8 and o_cons8.sid = 105
left join tblLabelDetail ld_cons8 on ld_cons8.labelID = o_cons8.labelID and ld_cons8.langId = 1 and ld_cons8.eid = 107
left join tbloption o_cons9 on o_cons9.id = cons_cust.option9 and o_cons9.sid = 105
left join tblLabelDetail ld_cons9 on ld_cons9.labelID = o_cons9.labelID and ld_cons9.langId = 1 and ld_cons9.eid = 107
left join vCustomSession session_cust on session_cust.sessionYM between 601 and 1412 and session_cust.sID=105 and session_cust.ID=session.ID
left join tbloption o_sess1 on o_sess1.id = session_cust.option1 and o_sess1.sid = 105
left join tblLabelDetail ld_sess1 on ld_sess1.labelID = o_sess1.labelID and ld_sess1.langId = 1 and ld_sess1.eid = 107
left join tbloption o_sess2 on o_sess2.id = session_cust.option2 and o_sess2.sid = 105
left join tblLabelDetail ld_sess2 on ld_sess2.labelID = o_sess2.labelID and ld_sess2.langId = 1 and ld_sess2.eid = 107
left join tbloption o_sess3 on o_sess2.id = session_cust.option3 and o_sess3.sid = 105
left join tblLabelDetail ld_sess3 on ld_sess3.labelID = o_sess2.labelID and ld_sess3.langId = 1 and ld_sess3.eid = 107
left join tbloption o_sess4 on o_sess4.id = session_cust.option4 and o_sess4.sid = 105
left join tblLabelDetail ld_sess4 on ld_sess4.labelID = o_sess4.labelID and ld_sess4.langId = 1 and ld_sess4.eid = 107
left join tbloption o_sess5 on o_sess5.id = session_cust.option5 and o_sess5.sid = 105
left join tblLabelDetail ld_sess5 on ld_sess5.labelID = o_sess5.labelID and ld_sess5.langId = 1 and ld_sess5.eid = 107
left join tbloption o_sess6 on o_sess6.id = session_cust.option6 and o_sess6.sid = 105
left join tblLabelDetail ld_sess6 on ld_sess6.labelID = o_sess6.labelID and ld_sess6.langId = 1 and ld_sess6.eid = 107
left join tbloption o_sess7 on o_sess7.id = session_cust.option7 and o_sess7.sid = 105
left join tblLabelDetail ld_sess7 on ld_sess7.labelID = o_sess7.labelID and ld_sess7.langId = 1 and ld_sess7.eid = 107
left join tbloption o_sess8 on o_sess8.id = session_cust.option8 and o_sess8.sid = 105
left join tblLabelDetail ld_sess8 on ld_sess8.labelID = o_sess8.labelID and ld_sess8.langId = 1 and ld_sess8.eid = 107
left join tbloption o_sess9 on o_sess9.id = session_cust.option9 and o_sess9.sid = 105
left join tblLabelDetail ld_sess9 on ld_sess9.labelID = o_sess9.labelID and ld_sess9.langId = 1 and ld_sess9.eid = 107
where session.sID=105 and session.tStamp >= 'Sep 1 2014 12:00AM' and session.tStamp < 'Dec 12 2014 12:00AM'
order by session.tStamp, session.ID
Is there a way , where i can simplify the joins with tbloption and tblLabelDetail and get my o/p in optimal time.
Regards
I have headed towards another approach ie. using unpivot and then pivot.
First i am converting option1-option9 into column , then doing PIVOT to get back the same record . But issue is that when i am doing pivoting i am getting NULL values.
My query is :
select * into #t1 from
select ID
, option1
, option2
, option3
, option4
, option5
, option6
, option7
, option8
, option9
from vCustomConsumer
where sid=105
and sessionYM = 1412
) SourceTable
UNPIVOT
optionID FOR Col IN
(option1
,option2
,option3
,option4
,option5
,option6
,option7
,option8
,option9 )
) AS unpvt
select t.ID,t.optionID,t.col,cast(ld.text as varchar(max)) as text into #t2
from #t1 t
left outer join tbloption o on o.ID = t.optionID
left outer join tblLabelDetail ld on ld.labelID = o.labelID and ld.langID=1
order by ID,col
select ID,option1
,option2
,[option3]
,option4
,option5
,option6
,option7
,option8
,option9
from
select ID,optionID,col,text
from #t2
)up
pivot
min(text)for col in
(option1
,option2
,[option3]
,option4
,option5
,option6
,option7
,option8
,option9
)as pvt
In my last query where i am using pivot, i am getting NULL values. When i check the data in temp table #t2 , it exists perfectly . But when pivoting i dont understand why it is returning most of the NULL values. I am getting data for only one column in single
row.
Below are some rows from result set finally obtained after pivoting :
ID
option1
option2
option3
option4
option5
option6
option7
option8
option9
62949026
NULL
0 to 200 seconds
NULL
NULL
NULL
NULL
NULL
NULL
NULL
62966000
NULL
NULL
4
NULL
NULL
NULL
NULL
NULL
NULL
62966032
NULL
NULL
4
NULL
NULL
NULL
NULL
NULL
NULL
63090372
NULL
NULL
NULL
NULL
EN
NULL
NULL
NULL
NULL
63090375
NULL
NULL
NULL
NULL
EN
NULL
NULL
NULL
NULL
Thanks,
Similar Messages
-
Sql query with multiple joins to same table
I have to write a query for a client to display business officers' names and title along with the business name
The table looks like this
AcctNumber
OfficerTitle
OfficerName
RecKey
90% of the businesses have exactly 4 officer records, although some have less and some have more.
There is a separate table that has the AcctNumber, BusinessName about 30 other fields that I don’t need
An individual account can have 30 or 40 records on the other table.
The client wants to display 1 record per account.
Initially I wrote a query to join the table to itself:
Select A.OfficerTtitle, A.OfficerName, B.OfficerTitle, B.OfficerName, C.OfficerTtitle, C.OfficerName, D.OfficerTitle, D.OfficerName where A.AcctNumber = B.AcctNumber and A.AcctNumber = C.AcctNumber and A.AcctNumber = D.AcctNumber
This returned tons of duplicate rows for each account ( number of records * number of records, I think)
So added
And A.RecKey > B.RecKey and B.RecKey > C. RecKey and C.RecKey . D.RecKey
This works when there are exactly 4 records per account. If there are less than 4 records on the account it skips the account and if there are more than 4 records, it returns multiple rows.
But when I try to l join this to the other table to get the business name, I get a row for every record on the other table
I tried select distinct on the other table and the query runs for ever and never returns anything
I tried outer joins and subqueries, but no luck so far. I was thinking maybe a subquery - if exists - because I don't know how many records there are on an account, but don't know how to structure that
Any suggestions would be appreciatedWelcome to the forum!
user13319842 wrote:
I have to write a query for a client to display business officers' names and title along with the business name
The table looks like this
AcctNumber
OfficerTitle
OfficerName
RecKey
90% of the businesses have exactly 4 officer records, although some have less and some have more.
There is a separate table that has the AcctNumber, BusinessName about 30 other fields that I don’t need
An individual account can have 30 or 40 records on the other table.
The client wants to display 1 record per account.As someone has already mentioned, you should post CREATE TABLE and INSERT statements for both tables (relevant columns only). You don't have to post a lot of sample data. For example, you need to pick 1 out of 30 or 40 rows (max) for the same account, but it's almost certainly enough if you post only 3 or 4 rows (max) for an account.
Also, post the results you want from the sample data that you post, and explain how you get those resutls from that data.
Always say which version of Oracle you're using. This sounds like a PIVOT problem, and a new SELECT .... PIVOT feature was introduced in Oracle 11.1. If you're using Oracle 11, you don't want to have to learn the old way to do pivots. On the other hand, if you have Oracle 10, a solution that uses a new feature that you don't have won't help you.
Whenever you have a question, please post CREATE TABLE and INSERT statements for some sample data, the results you want from that data, an explanation, and your Oracle version.
Initially I wrote a query to join the table to itself:
Select A.OfficerTtitle, A.OfficerName, B.OfficerTitle, B.OfficerName, C.OfficerTtitle, C.OfficerName, D.OfficerTitle, D.OfficerName where A.AcctNumber = B.AcctNumber and A.AcctNumber = C.AcctNumber and A.AcctNumber = D.AcctNumber Be careful, and post the exact code that you're running. The statement above can't be what you ran, because it doesn't have a FROM clause.
This returned tons of duplicate rows for each account ( number of records * number of records, I think)
So added
And A.RecKey > B.RecKey and B.RecKey > C. RecKey and C.RecKey . D.RecKey
This works when there are exactly 4 records per account. If there are less than 4 records on the account it skips the account and if there are more than 4 records, it returns multiple rows.
But when I try to l join this to the other table to get the business name, I get a row for every record on the other table
I tried select distinct on the other table and the query runs for ever and never returns anything
I tried outer joins and subqueries, but no luck so far. I was thinking maybe a subquery - if exists - because I don't know how many records there are on an account, but don't know how to structure that
Any suggestions would be appreciatedDisplaying 1 column from n rows as n columns on 1 row is called Pivoting . See the following link fro several ways to do pivots:
SQL and PL/SQL FAQ
Pivoting requires that you know exactly how many columns will be in the result set. If that number depends on the data in the table, then you might prefer to use String Aggregation , where the output consists of a huge string column, that contains the concatenation of the data from n rows. This big string can be formatted so that it looks like multiple columns. For different string aggregation techniques, see:
http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
The following thread discusses some options for pivoting a variable number of columns:
Re: Report count and sum from many rows into many columns -
Oracle hangs on query with multiple joins
To test the sotftware from the LMS-vendor that are out partners we use virtual machines where we install Windows 2003 servers and Oracle XE
While we were testing the software I've found that a particular query with multiple unions causes the CPU of the virtual machine totally claimed by oracle
The query causes oracle to hang.
I've found that the subcomponents of the query all return 0 rows (and response all immediately) combined into a query with at least 2 unions the query hangs the system
I'm not familiar with with Database Management at all
I've read something about SGA and PGA that could be the issue and tried to increase the target for both but it doesn't seem to do anything
Some characterics
Target Current
Maximum System Global Area (SGA) Size: 380 MB 380 MB
Program Global Area (PGA) Aggregate Target: 360 MB 38 MB
Current Configuration: (SGA + PGA): 740 MB 418 MB
Tablespaces Percent Used Allocated (MB) Used (MB) Datafiles
SYSAUX 98.21% 460.00 451.75 1
SYSTEM 73.09% 510.00 372.75 1
DATA 99.13% 440.00 436.19 1
UNDO 6.48% 160.00 10.38 1
USERS 1.63% 100.00 1.63 1
sort_area_size 65536
shared_pool_reserved_size 5452595 TRUE size in bytes of reserved area of shared pool
shared_pool_size 0 TRUE size in bytes of shared pool
What other parameters are important? How could I get a good picture to see what's really going on?
Some pointers, help would be appreciated.
Regards,
RemcoBelow is the base-query that is causing the problems
SELECT /* Public - Internal learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no order_number, e.id person_id, format_name(e.fname , e.lname , @@005) learner_name , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description item_desc, substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1 , r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN tpt_oe_order_items i ON i.reg_id = r.id INNER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id INNER JOIN tpt_company c ON e.company_id = c.id INNER JOIN tpt_offering_action oa on r.offering_action_id = oa.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name INNER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name INNER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name INNER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name WHERE e.type = 100 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003))
UNION
SELECT /* Public - External learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , e.id person_id, format_name(e.fname , e.lname , @@005) , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1, r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN tpt_oe_order_items i ON i.reg_id = r.id INNER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id INNER JOIN tpt_offering_action oa on r.offering_action_id = oa.id LEFT OUTER JOIN tpt_company c ON e.company_id = c.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name INNER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name INNER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name INNER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name WHERE e.type = 200 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003))
UNION
SELECT /* Public - Unassigned learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , null person_id, null , null company_id, null , ent.id entidd, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1, r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,'' org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,'' learner_first_name ,'' learner_last_name FROM tpt_registration r INNER JOIN tpt_oe_order_items i ON i.reg_id = r.id INNER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN tpt_offering_action oa on oa.id = r.offering_action_id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name INNER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name INNER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name INNER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name WHERE r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND r.student_id is null AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 AND @@003 is null
UNION
SELECT /* Private - Internal learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , e.id person_id, format_name(e.fname , e.lname , @@005) , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1 , r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN let_pvt_offering_request pvt_offreq ON pvt_offreq.class_id = r.class_id INNER JOIN tpt_offering_action oa on oa.id = r.offering_action_id LEFT OUTER JOIN tpt_oe_order_items i ON i.part_id = pvt_offreq.id LEFT OUTER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id LEFT OUTER JOIN tpt_company c ON e.company_id = c.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name LEFT OUTER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' LEFT OUTER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 LEFT OUTER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 WHERE e.type = 100 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003))
UNION
SELECT /* Private - External learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , e.id person_id, format_name(e.fname , e.lname , @@005) , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1 , r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN let_pvt_offering_request pvt_offreq ON pvt_offreq.class_id = r.class_id INNER JOIN tpt_offering_action oa on r.offering_action_id = oa.id LEFT OUTER JOIN tpt_oe_order_items i ON i.part_id = pvt_offreq.id LEFT OUTER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id LEFT OUTER JOIN tpt_company c ON e.company_id = c.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name LEFT OUTER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' LEFT OUTER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name AND orderList.locale_id = @@005 AND orderList.list_id = 'sysli000000000000129' LEFT OUTER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name AND approvalList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' WHERE e.type = 200 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003)) ORDER BY 34,35,28,29,31,30,33,32 -
Same sql query with multiple database links
Hi All,
i want to execute an sql query for a SELECT LIST Item. which should use database links in it's query.
i'm having a list of database links in the region. say :LOC which is having 10 items each linking to different databases.
i want to use following query for another item in same page.
select customer_name from working_lines@:LOC where
phone_no:phone_no
can i do this.
Thank you All.What we have done before is to create a report based on a PL/SQL procedure that returns a SQL statement.
The PL/SQL code grabs the "selected" database link from :LOC and parses the SQL QUERY you want to execute replacing some token (like [LINK]) with '@DBNAME'.
Lets say your SQL statement is coded like this:
mySQL := 'SELECT cust_name FROM working_lines[LINK] where phone_no = :Phone;'
Then all you would have to do to replace '[LINK]' with the proper DB alias would be:
mySQL := REPLACE(mySQL, '[LINK]', '@'||:LOC);
and then return the new SQL ...
if :LOC has something like 'V10GPROD' your SQL would look like :
SELECT cust_name FROM working_lines@V10GPROD where phone_no = :Phone;
The beauty of this is that you can also include a "null" option that means the select statement will be executed on THIS database...
Doug -
Performance tuning in SQL query using join of views
Hi,
Am trying to tune a query of the format
select ........ from view1,view2
where view1.keyfield = view2.keyfield
The base tables of the views view1 and view2 have indexes specified on 'keyfield'.
However, when I do an explain plan of the query, Full table scan of those base tables are performed.
Even tried using Hints in this query to force Oracle to use those indexes, but still not successful.
Any pointers on how to tune this kind of query would be highly welcome.
Regards,
BaishIf your query is really of the same form as you posted, then full table scans then a hash or merge join may be the most efficient plan.
You are asking for all of the rows from view1 and the matching rows from view2, because of this, Oracle dedcided that the cost of full table scans using multi-block reads is cheaper than using single block reads to get the rowids from the index, then single block reads to get the rows from the table.
FULL SCAN <> BAD
John -
SQL query with multiple tables - what is the most efficient way?
Hello I am learning PL/SQL. I have a simple procedure where I need to find number of employees and departments per location as per user input of location_id.
I have 3 Tables:
LOCATIONS
location_id (pk)
location_name
DEPARTMENTS
department_id (pk)
location_id (fk)
department_name
EMPLOYEES
employee_id (pk)
department_id (fk)
employee_name
1 Location can have 0-MANY Departments
1 Employee has 1 Department
Here is the query I came up with for PL/SQL procedure:
/*Ecount, Dcount are NUMBER variables */
SELECT SUM (EmployeeCount), COUNT(DepartmentNumber)
INTO Ecount, Dcount
FROM
(SELECT COUNT(employee_id) EmployeeCount, department_id DepartmentNumber
FROM employees
GROUP BY department_id
HAVING department_id IN
(SELECT department_id
FROM departments
WHERE location_id = userInput));
I do get the correct result, but I am just wondering if my query is on the right track and if there is a more "efficient" way of doing this.
Thanks in advance for helping a newbie out.Hi,
Welcome to the forum!
Something like this will be more efficient:
SELECT COUNT (employee_id) AS ECount
, COUNT (DISTINCT department_id) AS DCount
FROM employees
WHERE department_id IN ( SELECT department_id
FROM departments
WHERE location_id = :userInput
;You should also try a join instead of the IN subquery.
For efficiency, do only the things you need to do.
For example, you don't need a count of employees in each department, so don't compute one. That means you won't need the in-line view, so don't have one.
You don't need PL/SQL for this job, so don't use PL/SQL if you don't have to. (I realize this question was out of context, so you may have good reasons for doing this in PL/SQL.)
Do all filtering as early as possible. Don't waste effort computing things that won't be used .
A particular example of this is: Never use a HAVING clause when you can use a WHERE clause. What's the difference between a WHERE clause and a HAVING clause? The WHERE clause is applied before aggregate functions are computed, and the HAVING clause is applied after; there's no other difference. Therefore, if the HAVING clause isn't referencing an aggregate function, it could be done in a WHERE clause instead. -
Performance of an sql query with 16 million rows
Hello,
I have about 16 million records in a project table, lets call it the ENCOUNTER table.
I have an ENCOUNTER_NUMBER to be searched for which is VARCHAR2(30) and has a unique constraint on it. The results are to be ordered by ENCOUNTER_NUMBER.
The search string that the user enters is always considered to be a contains search and so a % is prefixed and suffixed to the search string.
I have created a functional index on UPPER(ENCOUNTER_NUMBER)
More the number of matches to the search query, the less the time taken. If there are 100 matches or more, the query returns within a second.
If there is only one match it takes atleast 10 seconds.
I am not sure at this stage why there is huge difference. Our client would be happy with 5 seconds.
Can anyone help me with this please, if at all it is possible to get the search quicker
Any help would be greatly appreciated.
Many thanks
MarilynYou are seeing differing response times to fetch the first n rows - not all the rows. So if your search is 'less selective' then you need to search fewer rows to find the first n rows meeting your criteria.
For example, if you have a search that 1% of your rows will match, then you probably need to scan about 10,000 rows to find the first 100 matching rows. If you specify a more selective predicate, matching 0.1% of the rows, then you will need to scan 100k rows to find your first 100 matches.
A generic 'contains' query, such as you describe, is difficult to implement with good performance. And it is often the result of someone not giving sufficient thought to the requirements: 'Well, it's too hard to figure out how people really need to use it, so I'll ask for this and I'll be covered regardless of what people really need.'
Can you negotiate/narrow down the requirements, such as:
- allow leading wildcards or trailing wildcards but not both
- allow/require other search criteria to be specified (not more double-wildcarded criteria, though)
- allow searching specific positions of the column (for example, if positions 4-6 have some specific meaning, then allow a search on that substring) -
SQL query with multiple condition
Below is the sample table
ID RATE DATE ORGAMNT PAYAMOUNT
1 3.45 12/23/2012 23000.45 19060.23
2. 5.50 04/12/2013 45000.00 36000.54
3. 6.44 04/16/200 60000.00 20000.00
Select a table with highest rate, Orgamnt with Date closer to current date.with
data_table as
(select 11186 hnum,11607400 oma,to_date('2/1/2006','mm/dd/yyyy') fpay,5.51 irate,10580934.67 apb,'WELLS FARGO BANK NA' hname from dual union all
select 11188,4801800,to_date('9/1/2006','mm/dd/yyyy'),6.3,4326951.51,'PNC BANK, NATIONAL ASSOCIATION' hname from dual union all
select 11189,1236100,to_date('8/1/2006','mm/dd/yyyy'),6.15,1147108.9,'HEARTLAND BANK' hname from dual union all
select 11192,1578400,to_date('4/1/2007','mm/dd/yyyy'),5.82,1470926.86,'WELLS FARGO BANK NA' hname from dual union all
select 11193,8150400,to_date('12/1/2008','mm/dd/yyyy'),6.75,7828776.28,'OPPENHEIMER MULTIFAMILY HSG' hname from dual union all
select 11195,3898000,to_date('5/1/2009','mm/dd/yyyy'),6.5,3752154.96,'BERKADIA COMMERCIAL MORTGAGE' hname from dual union all
select 11196,8040000,to_date('6/1/2009','mm/dd/yyyy'),7,7775734.57,'OPPENHEIMER MULTIFAMILY HSG' hname from dual union all
select 11197,37315000,to_date('12/1/2009','mm/dd/yyyy'),5.5,35851362.87,'RED MORTGAGE CAPITAL LLC' hname from dual union all
select 11199,3824000,to_date('2/1/2010','mm/dd/yyyy'),5.25,3673760.35,'WALKER AND DUNLOP LLC' hname from dual union all
select 11200,26927000,to_date('7/1/2010','mm/dd/yyyy'),4.7,25186682.65,'OAK GROVE COMMERCIAL MORTGAGE' hname from dual union all
select 11201,7208600,to_date('10/1/2010','mm/dd/yyyy'),4.59,6949026.64,'BERKADIA COMMERCIAL MORTGAGE' hname from dual union all
select 11204,9300000,to_date('2/1/2011','mm/dd/yyyy'),3.41,8927064.51,'PNC BANK, NATIONAL ASSOCIATION' hname from dual union all
select 11205,5200000,to_date('2/1/2011','mm/dd/yyyy'),3.56,4997518.18,'LOVE FUNDING CORPORATION' hname from dual union all
select 11206,6800000,to_date('5/1/2011','mm/dd/yyyy'),3.88,6576635.57,'WELLS FARGO BANK NA' hname from dual union all
select 11208,12240000,to_date('8/1/2011','mm/dd/yyyy'),3.84,11879882.17,'WELLS FARGO BANK NA' hname from dual union all
select 11209,12911500,to_date('10/1/2011','mm/dd/yyyy'),3.55,12542199.51,'M AND T REALTY CAPITAL CORP' hname from dual union all
select 11210,9596900,to_date('7/1/2011','mm/dd/yyyy'),5.38,9323421.23,'LANCASTER POLLARD MORTGAGE' hname from dual union all
select 11211,19951600,to_date('9/1/2011','mm/dd/yyyy'),3.88,19393524.45,'DEUTSCHE BANK BERKSHIRE MORTGAGE' hname from dual union all
select 11183,4230000,to_date('4/1/2009','mm/dd/yyyy'),6.55,4069938.75,'BERKADIA COMMERCIAL MORTGAGE' hname from dual union all
select 11212,50475000,to_date('1/1/2013','mm/dd/yyyy'),2.37,49852632.51,'LOVE FUNDING CORPORATION' hname from dual union all
select 11213,5496000,to_date('5/1/2012','mm/dd/yyyy'),3.1,5377541.15,'WELLS FARGO BANK NA' hname from dual
select hnum,oma,fpay,irate,apb,hname,r_oma,r_irate,r_fpay,0.5 * r_oma + 0.3 * r_irate + 0.2 * r_fpay r
from (select hnum,oma,fpay,irate,apb,hname,
dense_rank() over (order by oma desc) r_oma,
dense_rank() over (order by irate desc) r_irate,
dense_rank() over (order by (sysdate - fpay)) r_fpay
from data_table
order by r
HNUM
OMA
FPAY
IRATE
APB
HNAME
R_OMA
R_IRATE
R_FPAY
R
11197
37315000
12/01/2009
5.5
35851362.87
RED MORTGAGE CAPITAL LLC
2
9
12
6.1
11212
50475000
01/01/2013
2.37
49852632.51
LOVE FUNDING CORPORATION
1
20
1
6.7
11211
19951600
09/01/2011
3.88
19393524.45
DEUTSCHE BANK BERKSHIRE MORTGAGE
4
14
4
7
11200
26927000
07/01/2010
4.7
25186682.65
OAK GROVE COMMERCIAL MORTGAGE
3
12
10
7.1
11209
12911500
10/01/2011
3.55
12542199.51
M AND T REALTY CAPITAL CORP
5
17
3
8.2
11210
9596900
07/01/2011
5.38
9323421.23
LANCASTER POLLARD MORTGAGE
8
10
6
8.2
11196
8040000
06/01/2009
7
7775734.57
OPPENHEIMER MULTIFAMILY HSG
11
1
13
8.4
11208
12240000
08/01/2011
3.84
11879882.17
WELLS FARGO BANK NA
6
15
5
8.5
11193
8150400
12/01/2008
6.75
7828776.28
OPPENHEIMER MULTIFAMILY HSG
10
2
16
8.8
11186
11607400
02/01/2006
5.51
10580934.67
WELLS FARGO BANK NA
7
8
20
9.9
11204
9300000
02/01/2011
3.41
8927064.51
PNC BANK, NATIONAL ASSOCIATION
9
18
8
11.5
11201
7208600
10/01/2010
4.59
6949026.64
BERKADIA COMMERCIAL MORTGAGE
12
13
9
11.7
11206
6800000
05/01/2011
3.88
6576635.57
WELLS FARGO BANK NA
13
14
7
12.1
11183
4230000
04/01/2009
6.55
4069938.75
BERKADIA COMMERCIAL MORTGAGE
17
3
15
12.4
11195
3898000
05/01/2009
6.5
3752154.96
BERKADIA COMMERCIAL MORTGAGE
18
4
14
13
11188
4801800
09/01/2006
6.3
4326951.51
PNC BANK, NATIONAL ASSOCIATION
16
5
18
13.1
11213
5496000
05/01/2012
3.1
5377541.15
WELLS FARGO BANK NA
14
19
2
13.1
11205
5200000
02/01/2011
3.56
4997518.18
LOVE FUNDING CORPORATION
15
16
8
13.9
11199
3824000
02/01/2010
5.25
3673760.35
WALKER AND DUNLOP LLC -
In a SQL query whihc has join, How to reduce Multiple instance of a table
in a SQL query which has join, How to reduce Multiple instance of a table
Here is an example: I am using Oracle 9i
is there a way to reduce no.of Person instances from the following query? or can I optimize this query further?
TABLES:
mail_table
mail_id, from_person_id, to_person_id, cc_person_id, subject, body
person_table
person_id, name, email
QUERY:
SELECT p_from.name from, p_to.name to, p_cc.name cc, subject
FROM mail, person p_from, person p_to, person p_cc
WHERE from_person_id = p_from.person_id
AND to_person_id = p_to.person_id
AND cc_person_id = p_cc.person_id
Thnanks in advance,
Babu.SQL> select * from mail;
ID F T CC
1 1 2 3
SQL> select * from person;
PID NAME
1 a
2 b
3 c
--Query with only ne Instance of PERSON Table
SQL> select m.id,max(decode(m.f,p.pid,p.name)) frm_name,
2 max(decode(m.t,p.pid,p.name)) to_name,
3 max(decode(m.cc,p.pid,p.name)) cc_name
4 from mail m,person p
5 where m.f = p.pid
6 or m.t = p.pid
7 or m.cc = p.pid
8 group by m.id;
ID FRM_NAME TO_NAME CC_NAME
1 a b c
--Expalin plan for "One instance" Query
SQL> explain plan for
2 select m.id,max(decode(m.f,p.pid,p.name)) frm_name,
3 max(decode(m.t,p.pid,p.name)) to_name,
4 max(decode(m.cc,p.pid,p.name)) cc_name
5 from mail m,person p
6 where m.f = p.pid
7 or m.t = p.pid
8 or m.cc = p.pid
9 group by m.id;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 902563036
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 3 | 216 | 7 (15)| 00:00:01 |
| 1 | HASH GROUP BY | | 3 | 216 | 7 (15)| 00:00:01 |
| 2 | NESTED LOOPS | | 3 | 216 | 6 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| MAIL | 1 | 52 | 3 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL| PERSON | 3 | 60 | 3 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
4 - filter("M"."F"="P"."PID" OR "M"."T"="P"."PID" OR
"M"."CC"="P"."PID")
Note
- dynamic sampling used for this statement
--Explain plan for "Normal" query
SQL> explain plan for
2 select m.id,pf.name fname,pt.name tname,pcc.name ccname
3 from mail m,person pf,person pt,person pcc
4 where m.f = pf.pid
5 and m.t = pt.pid
6 and m.cc = pcc.pid;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 4145845855
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 112 | 14 (15)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 112 | 14 (15)| 00:00:01 |
|* 2 | HASH JOIN | | 1 | 92 | 10 (10)| 00:00:01 |
|* 3 | HASH JOIN | | 1 | 72 | 7 (15)| 00:00:01 |
| 4 | TABLE ACCESS FULL| MAIL | 1 | 52 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL| PERSON | 3 | 60 | 3 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
| 6 | TABLE ACCESS FULL | PERSON | 3 | 60 | 3 (0)| 00:00:01 |
| 7 | TABLE ACCESS FULL | PERSON | 3 | 60 | 3 (0)| 00:00:01 |
Predicate Information (identified by operation id):
1 - access("M"."CC"="PCC"."PID")
2 - access("M"."T"="PT"."PID")
3 - access("M"."F"="PF"."PID")
PLAN_TABLE_OUTPUT
Note
- dynamic sampling used for this statement
25 rows selected.
Message was edited by:
jeneesh
No indexes created... -
We have to inverstigate about a reporting solution where things are getting slow (may be material, database design, network matters).
I have red a lot in MSDN and some books about performance tuning on SQL Server 2008 R2 (or other) but frankly, I feel a little lost in all that stuff
I'am looking for practical steps in order to do the tuning. Someone had like a recipe for that : a success story...
My (brain storm) Methodology should follow these steps:
Resource bottlenecks: CPU, memory, and I/O bottlenecks
tempdb bottlenecks
A slow-running user query : Missing indexes, statistics,...
Use performance counters : there are many, can one give us the list of the most important
how to do fine tuning about SQL Server configuration
SSRS, SSIS configuration ?
And do the recommandations.
Thanks
"there is no Royal Road to Mathematics, in other words, that I have only a very small head and must live with it..."
Edsger W. DijkstraHello,
There is no clear defined step which can be categorized as step by step to performance tuning.Your first goal is to find out cause or drill down to factor causing slowness of SQL server it can be poorly written query ,missing indexes,outdated stats.RAM crunch
CPU crunch so on and so forth.
I generally refer to below doc for SQL server tuning
http://technet.microsoft.com/en-us/library/dd672789(v=sql.100).aspx
For SSIS tuning i refer below doc.
http://technet.microsoft.com/library/Cc966529#ECAA
http://msdn.microsoft.com/en-us/library/ms137622(v=sql.105).aspx
When I face issue i generally look at wait stats ,wait stats give you idea about on what resource query was waiting.
--By Jonathan KehayiasSELECT TOP 10
wait_type ,
max_wait_time_ms wait_time_ms ,
signal_wait_time_ms ,
wait_time_ms - signal_wait_time_ms AS resource_wait_time_ms ,
100.0 * wait_time_ms / SUM(wait_time_ms) OVER ( )
AS percent_total_waits ,
100.0 * signal_wait_time_ms / SUM(signal_wait_time_ms) OVER ( )
AS percent_total_signal_waits ,
100.0 * ( wait_time_ms - signal_wait_time_ms )
/ SUM(wait_time_ms) OVER ( ) AS percent_total_resource_waits
FROM sys.dm_os_wait_stats
WHERE wait_time_ms > 0 -- remove zero wait_time
AND wait_type NOT IN -- filter out additional irrelevant waits
( 'SLEEP_TASK', 'BROKER_TASK_STOP', 'BROKER_TO_FLUSH',
'SQLTRACE_BUFFER_FLUSH','CLR_AUTO_EVENT', 'CLR_MANUAL_EVENT',
'LAZYWRITER_SLEEP', 'SLEEP_SYSTEMTASK', 'SLEEP_BPOOL_FLUSH',
'BROKER_EVENTHANDLER', 'XE_DISPATCHER_WAIT', 'FT_IFTSHC_MUTEX',
'CHECKPOINT_QUEUE', 'FT_IFTS_SCHEDULER_IDLE_WAIT',
'BROKER_TRANSMITTER', 'FT_IFTSHC_MUTEX', 'KSOURCE_WAKEUP',
'LAZYWRITER_SLEEP', 'LOGMGR_QUEUE', 'ONDEMAND_TASK_QUEUE',
'REQUEST_FOR_DEADLOCK_SEARCH', 'XE_TIMER_EVENT', 'BAD_PAGE_PROCESS',
'DBMIRROR_EVENTS_QUEUE', 'BROKER_RECEIVE_WAITFOR',
'PREEMPTIVE_OS_GETPROCADDRESS', 'PREEMPTIVE_OS_AUTHENTICATIONOPS',
'WAITFOR', 'DISPATCHER_QUEUE_SEMAPHORE', 'XE_DISPATCHER_JOIN',
'RESOURCE_QUEUE' )
ORDER BY wait_time_ms DESC
use below link to analyze wait stats
http://www.sqlskills.com/blogs/paul/wait-statistics-or-please-tell-me-where-it-hurts/
HTH
PS: for reporting services you can post in SSRS forum
Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers -
BaseTableName blank when calling GetSchema on a query with multiple tables
I am using ODP.NET 11.2.0.3.0 and when calling GetSchemaTable on a DataReader that contains a join the returned SchemaTable has the BaseTableName and BaseColumnName fields blank - this is different than what I see with the Oracle OLE DB Provider and with how SQL Server's native provider works. I can't find any discussion of this - is this on purpose or is it a bug? why does the available schema information vary so drastically between a single table query and a query with multiple tables joined?
Thanks,
Bryan HintonHi Bryan,
I am also facing the same issue. Did u find any work around or any suggestions will be well appreciated.
Thanks,
Naresh. -
SQL query with Bind variable with slower execution plan
I have a 'normal' sql select-insert statement (not using bind variable) and it yields the following execution plan:-
Execution Plan
0 INSERT STATEMENT Optimizer=CHOOSE (Cost=7 Card=1 Bytes=148)
1 0 HASH JOIN (Cost=7 Card=1 Bytes=148)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'TABLEA' (Cost=4 Card=1 Bytes=100)
3 2 INDEX (RANGE SCAN) OF 'TABLEA_IDX_2' (NON-UNIQUE) (Cost=3 Card=1)
4 1 INDEX (FAST FULL SCAN) OF 'TABLEB_IDX_003' (NON-UNIQUE)
(Cost=2 Card=135 Bytes=6480)
Statistics
0 recursive calls
18 db block gets
15558 consistent gets
47 physical reads
9896 redo size
423 bytes sent via SQL*Net to client
1095 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
55 rows processed
I have the same query but instead running using bind variable (I test it with both oracle form and SQL*plus), it takes considerably longer with a different execution plan:-
Execution Plan
0 INSERT STATEMENT Optimizer=CHOOSE (Cost=407 Card=1 Bytes=148)
1 0 TABLE ACCESS (BY INDEX ROWID) OF 'TABLEA' (Cost=3 Card=1 Bytes=100)
2 1 NESTED LOOPS (Cost=407 Card=1 Bytes=148)
3 2 INDEX (FAST FULL SCAN) OF TABLEB_IDX_003' (NON-UNIQUE) (Cost=2 Card=135 Bytes=6480)
4 2 INDEX (RANGE SCAN) OF 'TABLEA_IDX_2' (NON-UNIQUE) (Cost=2 Card=1)
Statistics
0 recursive calls
12 db block gets
3003199 consistent gets
54 physical reads
9448 redo size
423 bytes sent via SQL*Net to client
1258 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
55 rows processed
TABLEA has around 3million record while TABLEB has 300 records. Is there anyway I can improve the speed of the sql query with bind variable? I have DBA Access to the database
Regards
IvanMany thanks for your reply.
I have run the statistic already for the both tableA and tableB as well all the indexes associated with both table (using dbms_stats, I am on 9i db ) but not the indexed columns.
for table I use:-
begin
dbms_stats.gather_table_stats(ownname=> 'IVAN', tabname=> 'TABLEA', partname=> NULL);
end;
for index I use:-
begin
dbms_stats.gather_index_stats(ownname=> 'IVAN', indname=> 'TABLEB_IDX_003', partname=> NULL);
end;
Is it possible to show me a sample of how to collect statisc for INDEX columns stats?
regards
Ivan -
Need help in improving the performance for the sql query
Thanks in advance for helping me.
I was trying to improve the performance of the below query. I tried the following methods used merge instead of update, used bulk collect / Forall update, used ordered hint, created a temp table and upadated the target table using the same. The methods which I used did not improve any performance. The data count which is updated in the target table is 2 million records and the target table has 15 million records.
Any suggestions or solutions for improving performance are appreciated
SQL query:
update targettable tt
set mnop = 'G',
where ( x,y,z ) in
select a.x, a.y,a.z
from table1 a
where (a.x, a.y,a.z) not in (
select b.x,b.y,b.z
from table2 b
where 'O' = b.defg
and mnop = 'P'
and hijkl = 'UVW';987981 wrote:
I was trying to improve the performance of the below query. I tried the following methods used merge instead of update, used bulk collect / Forall update, used ordered hint, created a temp table and upadated the target table using the same. The methods which I used did not improve any performance. And that meant what? Surely if you spend all that time and effort to try various approaches, it should mean something? Failures are as important teachers as successes. You need to learn from failures too. :-)
The data count which is updated in the target table is 2 million records and the target table has 15 million records.Tables have rows btw, not records. Database people tend to get upset when rows are called records, as records exist in files and a database is not a mere collection of records and files.
The failure to find a single faster method with the approaches you tried, points to that you do not know what the actual performance problem is. And without knowing the problem, you still went ahead, guns blazing.
The very first step in dealing with any software engineering problem, is to identify the problem. Seeing the symptoms (slow performance) is still a long way from problem identification.
Part of identifying the performance problem, is understanding the workload. Just what does the code task the database to do?
From your comments, it needs to find 2 million rows from 15 million rows. Change these rows. And then write 2 million rows back to disk.
That is not a small workload. Simple example. Let's say that the 2 million row find is 1ms/row and the 2 million row write is also 1ms/row. This means a 66 minute workload. Due to the number of rows, an increase in time/row either way, will potentially have 2 million fold impact.
So where is the performance problem? Time spend finding the 2 million rows (where other tables need to be read, indexes used, etc)? Time spend writing the 2 million rows (where triggers and indexes need to be fired and maintained)? Both? -
How could I replace hard coded value in my sql query with constant value?
Hi all,
Could anyone help me how to replace hardcoded value in my sql query with constant value that might be pre defined .
PROCEDURE class_by_day_get_bin_data
in_report_parameter_id IN NUMBER,
in_site_id IN NUMBER,
in_start_date_time IN TIMESTAMP,
in_end_date_time IN TIMESTAMP,
in_report_level_min IN NUMBER,
in_report_level_max IN NUMBER
IS
bin_period_length NUMBER(6,0);
BEGIN
SELECT MAX(period_length)
INTO bin_period_length
FROM bin_data
JOIN site_to_data_source_lane_v
ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
JOIN bin_types
ON bin_types.bin_type = bin_data.bin_type
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >= in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time < in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data.bin_type = 2
AND bin_data.period_length <= 60;
--Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
--report.
DELETE FROM edr_class_by_day_bin_data;
SELECT site_to_data_source_lane_v.site_id,
site_to_data_source_lane_v.site_lane_id,
site_to_data_source_lane_v.site_direction_id,
site_to_data_source_lane_v.site_direction_name,
bin_data_set.start_date_time,
bin_data_set.end_date_time,
bin_data_value.bin_id,
bin_data_value.bin_value
FROM bin_data
JOIN bin_data_set
ON bin_data.bin_serial = bin_data_set.bin_serial
JOIN bin_data_value
ON bin_data_set.bin_data_set_serial = bin_data_value.bin_data_set_serial
JOIN site_to_data_source_lane_v
ON bin_data.data_source_id = site_to_data_source_lane_v.data_source_id
AND bin_data_set.lane = site_to_data_source_lane_v.data_source_lane_id
JOIN (
SELECT CAST(report_parameter_value AS NUMBER) lane_id
FROM report_parameters
WHERE report_parameters.report_parameter_id = in_report_parameter_id
AND report_parameters.report_parameter_group = 'LANE'
AND report_parameters.report_parameter_name = 'LANE'
) report_lanes
ON site_to_data_source_lane_v.site_lane_id = report_lanes.lane_id
JOIN (
SELECT CAST(report_parameter_value AS NUMBER) class_id
FROM report_parameters
WHERE report_parameters.report_parameter_id = in_report_parameter_id
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS'
) report_classes
ON bin_data_value.bin_id = report_classes.class_id
JOIN edr_rpt_tmp_inclusion_table
ON TRUNC(bin_data_set.start_date_time) = TRUNC(edr_rpt_tmp_inclusion_table.date_time)
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >= in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time < in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data_set.start_date_time >= in_start_date_time
AND bin_data_set.start_date_time < in_end_date_time
AND bin_data.bin_type = 2
AND bin_data.period_length = bin_period_length;
END class_by_day_get_bin_data;In the above code I'm using the hard coded value 2 for bin type
bin_data.bin_type = 2But I dont want any hard coded number or string in the query.
How could I replace it?
I defined conatant value like below inside my package body where the actual procedure comes.But I'm not sure whether I have to declare it inside package body or inside the procedure.
bin_type CONSTANT NUMBER := 2;But it does't look for this value. So I'm not able to get desired value for the report .
Thanks.
Edited by: user10641405 on May 29, 2009 1:38 PMDeclare the constant inside the procedure.
PROCEDURE class_by_day_get_bin_data(in_report_parameter_id IN NUMBER,
in_site_id IN NUMBER,
in_start_date_time IN TIMESTAMP,
in_end_date_time IN TIMESTAMP,
in_report_level_min IN NUMBER,
in_report_level_max IN NUMBER) IS
bin_period_length NUMBER(6, 0);
v_bin_type CONSTANT NUMBER := 2;
BEGIN
SELECT MAX(period_length)
INTO bin_period_length
FROM bin_data
JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
site_to_data_source_lane_v.data_source_id
JOIN bin_types ON bin_types.bin_type = bin_data.bin_type
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >=
in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time <
in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data.bin_type = v_bin_type
AND bin_data.period_length <= 60;
--Clear the edr_class_by_day_bin_data temporary table and populate it with the data for the requested
--report.
DELETE FROM edr_class_by_day_bin_data;
INSERT INTO edr_class_by_day_bin_data
(site_id,
site_lane_id,
site_direction_id,
site_direction_name,
bin_start_date_time,
bin_end_date_time,
bin_id,
bin_value)
SELECT site_to_data_source_lane_v.site_id,
site_to_data_source_lane_v.site_lane_id,
site_to_data_source_lane_v.site_direction_id,
site_to_data_source_lane_v.site_direction_name,
bin_data_set.start_date_time,
bin_data_set.end_date_time,
bin_data_value.bin_id,
bin_data_value.bin_value
FROM bin_data
JOIN bin_data_set ON bin_data.bin_serial = bin_data_set.bin_serial
JOIN bin_data_value ON bin_data_set.bin_data_set_serial =
bin_data_value.bin_data_set_serial
JOIN site_to_data_source_lane_v ON bin_data.data_source_id =
site_to_data_source_lane_v.data_source_id
AND bin_data_set.lane =
site_to_data_source_lane_v.data_source_lane_id
JOIN (SELECT CAST(report_parameter_value AS NUMBER) lane_id
FROM report_parameters
WHERE report_parameters.report_parameter_id =
in_report_parameter_id
AND report_parameters.report_parameter_group = 'LANE'
AND report_parameters.report_parameter_name = 'LANE') report_lanes ON site_to_data_source_lane_v.site_lane_id =
report_lanes.lane_id
JOIN (SELECT CAST(report_parameter_value AS NUMBER) class_id
FROM report_parameters
WHERE report_parameters.report_parameter_id =
in_report_parameter_id
AND report_parameters.report_parameter_group = 'CLASS'
AND report_parameters.report_parameter_name = 'CLASS') report_classes ON bin_data_value.bin_id =
report_classes.class_id
JOIN edr_rpt_tmp_inclusion_table ON TRUNC(bin_data_set.start_date_time) =
TRUNC(edr_rpt_tmp_inclusion_table.date_time)
WHERE site_to_data_source_lane_v.site_id = in_site_id
AND bin_data.start_date_time >=
in_start_date_time - numtodsinterval(1, 'DAY')
AND bin_data.start_date_time <
in_end_date_time + numtodsinterval(1, 'DAY')
AND bin_data_set.start_date_time >= in_start_date_time
AND bin_data_set.start_date_time < in_end_date_time
AND bin_data.bin_type = v_bin_type
AND bin_data.period_length = bin_period_length;
END class_by_day_get_bin_data; -
Need help with SQL Query with Inline View + Group by
Hello Gurus,
I would really appreciate your time and effort regarding this query. I have the following data set.
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
Please Ignore '----', added it for clarity
I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
The following is my query. I am kind of lost.
select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
from (
select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
from INVOICE
group by sequence_id,check_date, check_number, invoice_number, vendor_number
) A, INVOICE B
where A.sequence_id = B.sequence_id
Thanks,
NickIt looks like it is a duplicate thread - correct me if i'm wrong in this case ->
Need help with SQL Query with Inline View + Group by
Regards.
Satyaki De.
Maybe you are looking for
-
Error occured while importing .der file in R/3 System
Hi Friends.. I tried to connect R/3 system with portal through SSO concept. for that I have downloaded (.der) file from portal. After that I tried to import that file through STRUSTSSO2 transaction in R/3 system, at that time I got error message like
-
I update all my apple products and found that photo cannot export to PC thereafter, who may tell me any problem
-
SAP Gui 6.40 BeX Analyser problem (Run Time Error)
Dear All Really need some help We get the following error in EXCEL 2003 when running transaction rrmx from SAP easy access: Run time error 91 Object variable or with block variable not set When debugging we get the following error message: call pAddi
-
Effect Pathfinder (Anyone get this to work??)
I am trying to create BAGEL THIN in the style of DELI THIN in one editable appearance stack. Cyan is a dummy color to help troubleshoot this. Cyan is the shape I am trying to subtract out of the black dropshadow. None of the pathfinder commands work.
-
Problems installing subsription Cloud Apps
Brand new, just out of the box, iMac OS 10.7.5. Install the apps, it says they are trials. I try to license them, log in with my correct account details, Application manager quits. I click reopen, it then tells me that the apps are instaleed. I retur