troubleshooting Question

Oracle IF THEN query question

Avatar of toooki
toooki asked on
Oracle DatabaseDatabases
14 Comments2 Solutions353 ViewsLast Modified:
My Oracle (11gR2) table t1 is huge. But the number of records is substantially less when t1.f2 = 'R'.
select count(*) from t1; -- outputs 500K records
select count(*) from t1 where t1.f2 = 'R'; -- outputs 2000 records

I have a query with this structure below.  The query part under update is huge but have the same construct as below. Is there any way I could avoid duplication of code as below under IF and ELSE? I do not want to use dynamic sql.
The query execution (hoping) is faster under IF as it is updating only 2000 records. And under the ELSE it is updating 500K records. The condition below will make the query faster for case when myFlag = 0.

IF  (myFlag = 0) THEN
      UPDATE t1 SET t1.f1 = 10 WHERE t1.f2 = 'R';

ELSE
      UPDATE t1 SET t1.f1 = 10;

END IF;

Thank you.
ASKER CERTIFIED SOLUTION
Geert G
Oracle dba

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2009

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 2 Answers and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros