Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Choco.

Asked: January 31, 2018 - 6:59 am UTC

Last updated: January 31, 2018 - 2:54 pm UTC

Version: 11g

Viewed 1000+ times

You Asked

Hello,



I am fairly new with ORacle SQL and am stuck at one point.

I am trying to run the following update statement on a daily fact table, and it is running for ever. Is there a way to optimise the below sql, do i need to do any preprocessing or add any idex?

Or is it just wrong to use such a long in-list on a substr function

UPDATE EDI_867_DLY_FACT a SET OVRD_CUST_CD = 3000, audt_upd_dt=sysdate
WHERE (EDI_SNDR_ID, CUST_CD) IN (SELECT SNDR_ID, SOLD_TO FROM EDI_RULE_SNDR WHERE RULE_KEY= 1)
AND SUBSTR(POC_ZIP_CD,1,3) IN ('500','501','502','503','504','505','506','507','511','513','514','515','520','522','523','524','525','526','527','528','620','622','628','630','631','633','634','635','636','640','641','644','645','647','648','650','651','652','653','654','655','656','657','658','660','661','662','665','666','667','668','670','671','672','673','674','675','676','678','679','680','681','684','685','686','687','688','689','716','717','719','720','721','722','725','726','728','740','741','743','744')
AND OVRD_INV_DT='23-SEP-16'


Appreciate all your help! Thanks in advance

and Chris said...

To help with SQL performance, we really need to see the execution plan. This contains details of what the query did.

You can generate one as follows:

set serveroutput off
update /*+ gather_plan_statistics */<your update>

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


You can find other ways to do this and read more about plans at: https://blogs.oracle.com/sql/how-to-create-an-execution-plan

do i need to do any preprocessing or add any index?

An index may be helpful, but we really need to see the plan to answer that.

Or is it just wrong to use such a long in-list on a substr function

An in-list is shorthand for a series of ORs. A large list (probably) means you're accessing more rows, which in turn makes an index less useful.

Substr isn't a problem in itself. But generally you need to create an index that includes substr exactly as you use it in your query.

Read more about indexes at:

https://blogs.oracle.com/sql/how-to-create-and-use-indexes-in-oracle-database

Rating

  (2 ratings)

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

Comments

Consider the model

Niall, January 31, 2018 - 1:29 pm UTC

Hi

In addition, it looks like you are storing a US ZIP code in the POC_ZIP_CD column, and then filtering on the sec centre (area). If you do this frequently you might consider storing that value in its own, numeric, column, calculating it at time of insert should be straightforward and make resulting query code easier to read and write. Whether an index makes sense still depends on the factors Chris described above.
Chris Saxon
January 31, 2018 - 2:54 pm UTC

Good suggestion, thanks for stopping by Niall.

Choco flake, February 15, 2018 - 6:25 am UTC


More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database