Tune the query with join and not exists
This is on 10g R2.
I have a query similar to :
Select A.*, C.*
From A inner join B on A.id = B.id
Left join C on A.kid = C.kid
Where not exists
(select * from D where A.fid = D.fid and A.stat = 2);
I want avoiding to use the NOT EXISTS in the last part of the query
I tried the autotrace explain of above and compared with others format and found no better execution plan than that. The explain plan indicated that there were long "table access full" operation on B, due to its little huge records, and a long operation of the "NESTED LOOPS OUTER". I had tried to replace the NOT EXISTS part with another LEFT JOIN in the FROM, but it went worse. So Anyone can suggest a better way? or it is the most efficient query I can get?
Here is the tkprof output
from baandb.ttfacr200201 a
inner join baandb.ttfgld106201 c on (a.t$ttyp = c.t$otyp and a.t$ninv = c.t$odoc) and c.t$leac like :"SYS_B_0"
left join baandb.ttfgld910201 d on c.t$dim2 = d.t$dimx and d.t$dtyp = :"SYS_B_1"
where not exists
(select * from baandb.tcisli205201 b
where a.t$ttyp = b.t$ityp and a.t$ninv = b.t$idoc)
and (a.t$trec = :"SYS_B_2" or a.t$trec = :"SYS_B_3" and t$tdoc = :"SYS_B_4")
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.01 0.01 0 0 0 0
Fetch 5 1.06 52.11 29925 45943 0 54
total 7 1.07 52.12 29925 45943 0 54
Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 31
Rows Row Source Operation
54 HASH JOIN RIGHT ANTI (cr=45943 pr=29925 pw=0 time=2317005 us)
9957 INDEX FAST FULL SCAN TCISLI205201$IDX1 (cr=39 pr=0 pw=0 time=54 us)(object id 16639)
10067 NESTED LOOPS OUTER (cr=45904 pr=29925 pw=0 time=68531937 us)
10067 HASH JOIN (cr=35837 pr=29925 pw=0 time=68471521 us)
10420 TABLE ACCESS FULL TTFACR200201 (cr=2424 pr=0 pw=0 time=20894 us)
33156 TABLE ACCESS FULL TTFGLD106201 (cr=33413 pr=29925 pw=0 time=117767552 us)
51 INDEX UNIQUE SCAN TTFGLD910201$IDX1 (cr=10067 pr=0 pw=0 time=53177 us)(object id 20402)
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 2 0.00 0.00 0 0 0 0
Execute 3 0.02 0.02 0 0 0 0
Fetch 6 1.06 52.11 29925 45943 0 55
total 11 1.08 52.14 29925 45943 0 55
Similar Messages
-
Need help in optimizing the query with joins and group by clause
I am having problem in executing the query below.. it is taking lot of time. To simplify, I have added the two tables FILE_STATUS = stores the file load details and COMM table that is actual business commission table showing records successfully processed and which records were transmitted to other system. Records with status = T is trasnmitted to other system and traansactions with P is pending.
CREATE TABLE FILE_STATUS
(FILE_ID VARCHAR2(14),
FILE_NAME VARCHAR2(20),
CARR_CD VARCHAR2(5),
TOT_REC NUMBER,
TOT_SUCC NUMBER);
CREATE TABLE COMM
(SRC_FILE_ID VARCHAR2(14),
REC_ID NUMBER,
STATUS CHAR(1));
INSERT INTO FILE_STATUS VALUES ('12345678', 'CM_LIBM.TXT', 'LIBM', 5, 4);
INSERT INTO FILE_STATUS VALUES ('12345679', 'CM_HIPNT.TXT', 'HIPNT', 4, 0);
INSERT INTO COMM VALUES ('12345678', 1, 'T');
INSERT INTO COMM VALUES ('12345678', 3, 'T');
INSERT INTO COMM VALUES ('12345678', 4, 'P');
INSERT INTO COMM VALUES ('12345678', 5, 'P');
COMMIT;Here is the query that I wrote to give me the details of the file that has been loaded into the system. It reads the file status and commission table to show file name, total records loaded, total records successfully loaded to the commission table and number of records that has been finally transmitted (status=T) to other systems.
SELECT
FS.CARR_CD
,FS.FILE_NAME
,FS.FILE_ID
,FS.TOT_REC
,FS.TOT_SUCC
,NVL(C.TOT_TRANS, 0) TOT_TRANS
FROM FILE_STATUS FS
LEFT JOIN
SELECT SRC_FILE_ID, COUNT(*) TOT_TRANS
FROM COMM
WHERE STATUS = 'T'
GROUP BY SRC_FILE_ID
) C ON C.SRC_FILE_ID = FS.FILE_ID
WHERE FILE_ID = '12345678';In production this query has more joins and is taking lot of time to process.. the main culprit for me is the join on COMM table to get the count of number of transactions transmitted. Please can you give me tips to optimize this query to get results faster? Do I need to remove group and use partition or something else. Please help!I get 2 rows if I use my query with your new criteria. Did you commit the record if you are using a second connection to query? Did you remove the criteria for file_id?
select carr_cd, file_name, file_id, tot_rec, tot_succ, tot_trans
from (select fs.carr_cd,
fs.file_name,
fs.file_id,
fs.tot_rec,
fs.tot_succ,
count(case
when c.status = 'T' then
1
else
null
end) over(partition by c.src_file_id) tot_trans,
row_number() over(partition by c.src_file_id order by null) rn
from file_status fs
left join comm c
on c.src_file_id = fs.file_id
where carr_cd = 'LIBM')
where rn = 1;
CARR_CD FILE_NAME FILE_ID TOT_REC TOT_SUCC TOT_TRANS
LIBM CM_LIBM.TXT 12345678 5 4 2
LIBM CM_LIBM.TXT 12345677 10 0 0Using RANK can potentially produce multiple rows to be returned though your data may prevent this. ROW_NUMBER will always prevent duplicates. The ordering of the analytical function is irrelevant in your query if you use ROW_NUMBER. You can remove the outermost query and inspect the data returned by the inner query;
select fs.carr_cd,
fs.file_name,
fs.file_id,
fs.tot_rec,
fs.tot_succ,
count(case
when c.status = 'T' then
1
else
null
end) over(partition by c.src_file_id) tot_trans,
row_number() over(partition by c.src_file_id order by null) rn
from file_status fs
left join comm c
on c.src_file_id = fs.file_id
where carr_cd = 'LIBM';
CARR_CD FILE_NAME FILE_ID TOT_REC TOT_SUCC TOT_TRANS RN
LIBM CM_LIBM.TXT 12345678 5 4 2 1
LIBM CM_LIBM.TXT 12345678 5 4 2 2
LIBM CM_LIBM.TXT 12345678 5 4 2 3
LIBM CM_LIBM.TXT 12345678 5 4 2 4
LIBM CM_LIBM.TXT 12345677 10 0 0 1 -
Need complex query with joins and AGGREGATE functions.
Hello Everyone ;
Good Morning to all ;
I have 3 tables with 2 lakhs record. I need to check query performance.. How CBO rewrites my query in materialized view ?
I want to make complex join with AGGREGATE FUNCTION.
my table details
SQL> select from tab;*
TNAME TABTYPE CLUSTERID
DEPT TABLE
PAYROLL TABLE
EMP TABLE
SQL> desc emp
Name
EID
ENAME
EDOB
EGENDER
EQUAL
EGRADUATION
EDESIGNATION
ELEVEL
EDOMAIN_ID
EMOB_NO
SQL> desc dept
Name
EID
DNAME
DMANAGER
DCONTACT_NO
DPROJ_NAME
SQL> desc payroll
Name
EID
PF_NO
SAL_ACC_NO
SALARY
BONUS
I want to make complex query with joins and AGGREGATE functions.
Dept names are : IT , ITES , Accounts , Mgmt , Hr
GRADUATIONS are : Engineering , Arts , Accounts , business_applications
I want to select records who are working in IT and ITES and graduation should be "Engineering"
salary > 20000 and < = 22800 and bonus > 1000 and <= 1999 with count for males and females Separately ;
Please help me to make a such complex query with joins ..
Thanks in advance ..
Edited by: 969352 on May 25, 2013 11:34 AM969352 wrote:
why do you avoid providing requested & NEEDED details?I do NOT understand what do you expect ?
My Goal is :
1. When executing my own query i need to check expalin plan.please proceed to do so
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9010.htm#SQLRF01601
2. IF i enable query rewrite option .. i want to check explain plan ( how optimizer rewrites my query ) ? please proceed to do so
http://docs.oracle.com/cd/E11882_01/server.112/e16638/ex_plan.htm#PFGRF009
3. My only aim is QUERY PERFORMANCE with QUERY REWRITE clause in materialized view.It is an admirable goal.
Best Wishes on your quest for performance improvements. -
Tunning the Query with Distinct Clause
Hi All,
I have the below query that returns 28113657 records
select src_Wc_id, osp_id, src_osp_id
from osp_complements o1
where exists (select 1 from wc_crossref wc
where wc.src_wc_id = o1.SRC_WC_ID
and wc.state = 'CA')
This query executes within a second...
But when i include a DISTINCT clause in the select statement, it takes more time ... (more than 20 mins)
I am trying to get it tunned. Please advice me with your knowledge to get it done
Thanks for your time
Kannan.Retrieving distinct rows requires a sort of all returned rows. 20 - 3 = ~17 mins for sorting 28 mln rows looks too much. You need to tune your instance in order to speed up sort operation. The amount of memory dedicated to sorts is controlled by PGA_AGGREGATE_TARGET parameter. If it's set to 0 (not recommended) then SORT_AREA_SIZE is used. The process of PGA tuning is quite complex and described in PGA Memory Management chapter of Performance Tuning Guide.
There is a workaround which allows to bypass sort operation, but it requires proper index and proper access by that index. The idea is that rows rertrieved via index are automatically ordered by indexed columns. If that and only that columns (possibly - in the same order as in the index, I don't know) are selected using DISTINCT then sort is not actually performed. Rows are already sorted due to access via index.
Hope this will help you.
Regards,
Dima -
Why can I only connect to the web with Safari and not Firefox or Chrome?
I recently reinstalled Snow Leopard and now I can't get online using Firefox, Chrome or any other browser besides Safari. I've never had this problem before now.
Firefox and Chrome might have different proxy settings, you should be able to let them use the same settings as Safari though.
-
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 -
How can I stop syncing my outlook calendar with iCloud and not lose the existing calendar data in outlook? The calendar data which downloads to Outlook was also wiped off the Gmail calendar after the iCloud sync, how can I restore the data to Gmail?
same issue, looks like nobody answered you from last JULY....
-
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!Hi,
fac30d8e-74d3-42aa-b643-e30a3780e00f wrote:
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!
As the others have said, the execution plan will give you a better idea about which is faster.
If you're trying to see how using (or not using) a sub-query affects performance, make the rest of the queries as similar as possible. For example, include field3 in both queries, or ignore field3 in both queries.
In this particular case, I guess the optimizer would do the same thing either way, but that's just a guess. I can't see your execution plans.
In general, simpler code is faster, and better in other ways, too. In this case
tableB
is simpler than
(SELECT field1, field2 FROM tableB)
Why do you want a sub-query in this example? -
How to tune the query and difference between CBO AND RBO.. Which is good
Hello Friends,
Here are some questions I have pls reply back with complete description and url if any ..
1)How Did you tune Query,
2)What approach you take to tune query? Do you use Hints?
3)Where did you tune the query and what are the issue with query?
4)What is difference between RBO and CBO? where u use RBO and CBO.
5)Give some information about hash join?
6) Using explain plan how do u know where the bottle neck in query .. how u will identify where the bottle neck is from explain plan .
thanks/KumarHi,
kumar73 wrote:
Hello Friends,
Here are some questions I have pls reply back with complete description and url if any ..
1)How Did you tune Query, Use EXPLAIN PLAN to see exactly where it is spending its time, and address those areas.
See the forum FAQ
SQL and PL/SQL FAQ
"3. How to improve the performance of my query?"
2)What approach you take to tune query? Do you use Hints?Hints can help.
Even more helpful is writing the SQL efficiently (avoiding multiple scans of the same table, filtering early, using built-in rather than user-defined functions, ...), creating and using indexes, and, for large tables, partitioning.
Table design can have a big impact on performace.
Look for ways to do part of what you need before the query. This includes denormalizing (when appropriate), the kind of pre-digesting that often takes place in data warehouses, function-based indexes, and, starting in Oracle 11, virtual columns.
3)Where did you tune the query and what are the issue with query?Either this question is a vague summary of the entire thread, or I don't understand it. Can you re-phrase this part?
4)What is difference between RBO and CBO? where u use RBO and CBO.Basically, use RBO if you have Oracle 7 or earlier. -
While upgrading to CF7 from CF5, I am creating the new collections from Cf Admin. After creating, I indexed it from cf admin and then trying to search for the documents in that collection, and i am getting the following error. This was working yesterday. I have cheched the logs etc and nothing seems helpful in discovering what the issue is.
Document count is 11,745 and the size is 25,948 kb, which is not too much for a collection. I have tried to restart Cold Fusion Search Services and then ColdFusion Application services, but all in vain.
Any help on this would be greatly appreciated.
Detail
The collection you specified does not exists or is not registered with the ColdFusion Search Service.
Message
The collection rc_collectiom does not exist.
StackTrace
coldfusion.tagext.search.CollectionDoesNotExistException: The collection rc_collectiom does not exist. at coldfusion.tagext.search.SearchTag.verifyLocale(SearchTag.java:819) at coldfusion.tagext.search.SearchTag.doSearch(SearchTag.java:200) at coldfusion.tagext.search.SearchTag.doStartTag(SearchTag.java:159) at coldfusion.runtime.CfJspPage._emptyTag(CfJspPage.java:1915) at cfeFull2dText2dReDirect2ecfm511389924.runPage(C:\Inetpub\wwwroot\External\FullText\eFull- Text-ReDirect.cfm:43) at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:152) at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:349) at coldfusion.runtime.CfJspPage._emptyTag(CfJspPage.java:1915) at cfApplication2ecfc179940445$funcONREQUEST.runFunction(C:\Inetpub\wwwroot\External\Applica tion.cfc:114) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:344) at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:290) at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:254) at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:56) at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:207) at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:169) at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:194) at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:146) at coldfusion.runtime.AppEventInvoker.invoke(AppEventInvoker.java:72) at coldfusion.runtime.AppEventInvoker.onRequest(AppEventInvoker.java:178) at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:215) at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:51) at coldfusion.filter.PathFilter.invoke(PathFilter.java:86) at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27) at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:69) at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:52) at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:2 8) at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22) at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:115) at coldfusion.CfmServlet.service(CfmServlet.java:107) at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:78) at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91) at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42) at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:257) at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:541) at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:204) at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:318) at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:426) at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:264) at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)So at last I figured this out myself and found a temporary resolution for this kind of problem. Majority of the times try creating the Collection through Cfcollection tag and then index it using cfindex tag, rather than performing these operations from the Cold Fusion 7 administrator.
The only reason I can think of which might have caused the issue is – I have thousands of documents in the collection which I indexed. So while indexing the page gets expired. So I tried again to index it. This time the collection got indexed fairly quickly and I got the response that the collection has been indexed successfully. So for the first time when I tried to index the collection, there might be some piece of code, which did not register the collection properly and hence giving this error.
When I did the same function using the code, with exactly same set of documents, it did not throw any error, even when I executed it for the first time. So the collection indexed properly.
I am not claiming that technically this theory is correct, but this seems to be the problem.
If someone knows technically what has happened at the back, please share it. For the time being, this is the fix – if you experience any problem in creating/indexing collections through cfadmin in CF7, do it thorough code. -
I'm running ColdFusion 8 Enterprise on linux. I'm able to
create collections and index them through cfadmin as well as in cfm
application pages, but when trying to search I get the error
"The collection you specified does not exist or is not
registered with the ColdFusion Search Service."
I'm using the collection name in cfsearch and not the full
path.Would I have been better off posting this in the General
Discussion section? Could the moderators move it if so,
please? -
iPhone 6 plus,64 bit, A8 processor, M8 motion sensor, 64 GB memory, OS 8.1;
I run in an area without wifi and poor cell reception. How do I keep my tunes on the iPhone 6 plus and not in the cloudHi Delecee,
You can easily do that by downloading the music before you get to that area of weak Wi-Fi and cellular signal by downloading the music directly from the Music app by tapping the cloud button next to songs. The other option is to sync the music with iTunes over USB to have the songs downloaded to your phone. Take a look at the links below to get your music downloaded to your iPhone.
Download past purchases
https://support.apple.com/en-us/HT201272
Sync your iPhone, iPad, and iPod with iTunes using USB
https://support.apple.com/en-us/HT201253
Take it easy,
-Norm G. -
my iCal calendar that I created with the color green keeps changing to purple. no matter how many times I change it to green, it turns it back to purple. Of course this only happens with iCloud and not "from my Mac". Any ideas on how to correct this? This seems like a trivial error, but it's super annoying.
I called Apple and they said that they know about this problem. It's a problem with iCloud which their Engineers know about. (there was a new iCloud release, hence...) No ETA for a fix yet. I asked that this be escalated so Engineering doesn't put it at the bottom of their fix list. I talked to a Senior Advisor and mentioned to them to have QA check their regression tests as this has happened before to me (the problem just didn't take this long to resolve). I like to use green too for important stuff, so I've resorted to creating a new calendar using the normal calendar green for events going forward. They could have picked another color besides green to have this problem with and I wouldn't have been so upset. ;-)
-
I have garagaband for iOs in my iPhone 4S, I connect my Squire iOs guitarr to the iPhone, I select a Guitar Amp at the GB app then I Only can tune the guitar but I m not able to make the guitar ring with the amps. May some one help me?
You got the new iphone????? I have same problem. I transferred audiobooks to device to find no audiobooks on device (despite it being in iTunes as if it was). Have you found a solution????? I even tried to change import settings on format transfer but hasn't worked.
-
i have an iphone 4 with ios 5.1.1 and i want to upgrade it to the latest ios 6 and not 7 because its too heavy for iphone 4 how can i do it?
You cannot. When you upgrade, you will get the latest version of iOS 7.
Maybe you are looking for
-
I have photos on my iPad (the first apple device in our household) and then added an iMac and a Macbook air to our network. We have an airport 2TB back up device. How do I get all of my photos on all devices. My original iPad has images that my 2 ne
-
CS2 clipping paths incompatible with CS3 indesign
Hi there I need to use photoshop images with clipping paths that were created in photoshop CS2 in a indesign CS3 doc (working on a mac OS X 10.5.4). I import the image ok, the trouble is every time I try to activate the clipping path I get the spinni
-
Prepopulate Lookup Field of parent From - using PrePopulationAdapater - OIM 11.1.2
Hi Experts, please help me in implementing the below requirement, (Need to prepopulate Lookup field present in parent form of disconnected application instance.) Requirement 1) Requestor logs into Identity Self service console 2) searches for a eligi
-
Hi Example in Vendor master for a particular field is maintained other language entries...it should be displayed at table level right ? Example: se16 -> LFA1-ACTSS = all should language entries should be listed ? Rgds MM
-
I installed the new iTunes and an update for my video iPod........now my video's won't play in iTunes. Have you a fix? Thanks.