We help IT Professionals succeed at work.

SQL Optimizer modified my SQLbut I don't understand the syntax that it created. Any SQL gurus out there to explain it please?

ajovan
ajovan asked
on
216 Views
Last Modified: 2013-12-12
I had an Oracle SQL SELECT query that was running very slowly (upwards of 6 minutes) that, as a result, was timing out the web server trying run said query.

One of the tools that ships with Toad for Oracle is a query optimizer.  I ran the code through the optimizer and it returned an new version of the SELECT query that ran in 300ms!  Very impressive.  The only problem is that I don't understand the syntax and I was hoping that someone could explain it to me?

The only thing that the optimizer did was to append a concatenation to the end of my comparisons (see code snippet below)

Basically it added:

|| ''

Can someone please explain what || '' does?

Typing in those characters into Experts/Goole search only serves to crash the search engine! LOL

Thanks!

Alex
--OLD VERSION
SELECT c.alm_cd
FROM gxadmin.alm c
WHERE '2008' = c.fscl_yr
AND ('04' = c.all_typ OR '05' = c.all_typ))
AND b.fscl_yr = t.fscl_yr 
AND b.fscl_yr = r.fscl_yr
... etc
 
-- NEW VERSION
 
SELECT c.alm_cd
FROM gxadmin.alm c
WHERE '2008' = c.fscl_yr
AND ('04' = c.all_typ OR '05' = c.all_typ))
AND b.fscl_yr = t.fscl_yr || ''
AND b.fscl_yr = r.fscl_yr || ''
... etc

Open in new window

Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks to both of you for answering so quickly!
I tried the to_char and it worked as well (slower that the || version: 4.5 seconds, but still a HUGE improvemnet over the 6+ minutes)

Interestingly, comparing source table columns they were all Char(4)...  I wonder if when you specify a string like '2008' that it takes it as a varchar as a default, thereby requiring the conversion?

Once again thanks VERY much - I've never seen that || '' used in that manner before - and like I said - it's practically IMPOSSIBLE to search on bitwise or's

PS - since you both had the same answer, 2 minutes apart, with sighlightly different skews on it I was really torn on whom to give the points to... Faced with the age old question, angellll or imitchie, - I split them.

Thanks again!
Alex
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>Interestingly, comparing source table columns they were all Char(4)... I wonder if when you specify a string like '2008' that it takes it as a varchar as a default, thereby requiring the conversion?

if that is the case, you probably have an index on the fields, and the "trick" is used to "skip" deliberately the index.
please compare the explain plan for the query with and without the || ''
Top Expert 2007

Commented:
Even though you have not showed all tables, my guess is that an explain would have shown that it is using the index on table t's fscl_yr. Making it || '' will cause it to ignore that one and force the query down the other table's index first.  I think TOAD is trying to skew Oracle's query plan

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.