Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Swamy.

Asked: January 22, 2017 - 6:06 am UTC

Last updated: January 23, 2017 - 3:17 am UTC

Version: oracle11g

Viewed 1000+ times

You Asked

Hi Tom how can i reduce select statement and how optimize the select query please help on this issue tom?thanks

SELECT pa_num,
partner_id,
product_line
FROM IF_LSP_PANUM_MAP ilp
WHERE icp.country_code=i_workflow_keys
AND NOT EXISTS(SELECT 1
FROM sq_partner partner, sq_lsp lsp
WHERE partner.partner_id = lsp.company_id
AND partner.country_code =ilp.country_code
AND partner.partner_id=ilp.partner_id
AND partner.active = 'Y'
)
AND EXISTS(SELECT 1
FROM sq_product_line pl
WHERE pl.PRODUCT_LINE_ID=ilp.product_line
)
UNION
SELECT pa_num,
partner_id,
product_line
FROM IF_LSP_PANUM_MAP ilp
WHERE icp.country_code=i_workflow_keys
AND NOT EXISTS(SELECT 1
FROM sq_partner partner, sq_lsp lsp
WHERE partner.partner_id = lsp.company_id
AND partner.country_code =ilp.country_code
AND partner.partner_id=ilp.partner_id
AND partner.active = 'Y'
)
AND NOT EXISTS(SELECT 1
FROM sq_product_line pl
WHERE pl.PRODUCT_LINE_ID=ilp.product_line
)
UNION
SELECT pa_num,
partner_id,
product_line
FROM IF_LSP_PANUM_MAP ilp
WHERE icp.country_code=i_workflow_keys
AND EXISTS(SELECT 1
FROM sq_partner partner, sq_lsp lsp
WHERE partner.partner_id = lsp.company_id
AND partner.country_code =ilp.country_code
AND partner.partner_id=ilp.partner_id
AND partner.active = 'Y'
)
AND NOT EXISTS(SELECT 1
FROM sq_product_line pl
WHERE pl.PRODUCT_LINE_ID=ilp.product_line
)
UNION
SELECT pa_num,
partner_id,
product_line
FROM IF_LSP_PANUM_MAP ilp
WHERE icp.country_code=i_workflow_keys
AND NOT EXISTS(SELECT 1
FROM sq_partner partner, sq_lsp lsp
WHERE partner.partner_id = lsp.company_id
AND partner.country_code =ilp.country_code
AND partner.partner_id=ilp.partner_id
AND partner.active = 'Y'
)
AND NOT EXISTS(SELECT 1
FROM sq_product_line pl
WHERE pl.PRODUCT_LINE_ID=ilp.product_line);

here we have three different table based on the above query .how can reduce the select statement and how optimized the above query?

and Connor said...

Run the sql statement with the gather_plan_statistics hint, ie

SELECT /*+ gather_plan_statistics */ pa_num,
partner_id,
product_line

and then run

select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'))

and compare the "E-rows" and "A-rows" to see if the optimizer is making good decisions throughout. Where the numbers differ dramatically, that's where you can focus your efforts.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Swamy K, January 22, 2017 - 1:11 pm UTC

how can i minimize the my select.give me any solution please
Connor McDonald
January 23, 2017 - 3:17 am UTC

Tell me this - I'm 5kg heavier than I wish I was.... Is it because

a) I ate too much over christmas
b) I need to exercise more
c) I have a metabolic condition
d) My scales need calibrating
e) My kids were touching the scales when I was on it
f) I love soda pop too much
g) I was wearing a heavy coat on the scales..

Could be one of them...could be none...could be all.

You *dont* tune queries by looking at the SQL... you tune queries by looking at

- the business requirement
- the data distribution
- the optimizer decisions
- the resource consumption

and so on.

I tried to give you a starting point....

What on earth

Andrew Sayer, January 22, 2017 - 2:48 pm UTC

It looks like the differences between each union query is the combination of exists and not exists, why?

A similar query could be

SELECT pa_num,
partner_id,
product_line
FROM IF_LSP_PANUM_MAP ilp
WHERE icp.country_code=i_workflow_keys
MINUS
SELECT pa_num,
partner_id,
product_line
FROM IF_LSP_PANUM_MAP ilp
WHERE icp.country_code=i_workflow_keys
AND EXISTS(SELECT 1
FROM sq_partner partner, sq_lsp lsp
WHERE partner.partner_id = lsp.company_id
AND partner.country_code =ilp.country_code
AND partner.partner_id=ilp.partner_id
AND partner.active = 'Y'
)
AND EXISTS(SELECT 1
FROM sq_product_line pl
WHERE pl.PRODUCT_LINE_ID=ilp.product_line
)

Or

with ipl as (SELECT pa_num,
partner_id,
product_line
FROM if_lsp_panum_map ilp
WHERE icp.country_code=i_workflow_keys
)
SELECT pa_num,
partner_id,
product_line
FROM ilp
MINUS
SELECT pa_num,
partner_id,
product_line
FROM ilp
WHERE EXISTS(SELECT 1
FROM sq_partner partner, sq_lsp lsp
WHERE partner.partner_id = lsp.company_id
AND partner.country_code =i_workflow_keys
AND partner.partner_id=ilp.partner_id
AND partner.active = 'Y'
)
AND EXISTS(SELECT 1
FROM sq_product_line pl
WHERE pl.PRODUCT_LINE_ID=ilp.product_line
)

So the access on if_lsp_panum_map is done once (if the CBO decides it should materialize the CTE)

As EXISTS,EXISTS is the only combination of existence checks that doesn't exist in your query.

How do you optimize that? Who knows?! You could delete all your rows in IF_LSP_PANUM_MAP, that would probably speed it up! Without knowing the distribution of your data, your requirements, your table definitions, it's impossible to help. Connor has provided you the easiest way of seeing where the time goes and what (if any) mistakes the optimizer has made, this will show you very clearly what steps you could take.

You'll have to decide (using your knowledge of your data) where the query should drive from, should it check every row in if_lsp_panum_map that fits your country_code predicate for existence in the other tables? Or should it figure out what values of product_line and/or partner_id and country_code it needs to look for in if_lsp_panum_map. The answer depends on how selective the filter is and how much work needs to be put in to finding the value. As usual, given enough information, the CBO will be able to make that decision for you just fine. You may find that due to the highly selective nature of your filters (country_code sounds like you want everything from one country, sounds like a lot of data to me) that full table scans and hash semi joins are the only appropriate way for the query to execute.
Connor McDonald
January 23, 2017 - 3:17 am UTC

nice input.

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.