RE: What does LIKE '%%' actually do in a WHERE clause?

From: Dunbar, Norman (Capgemini) <norman.dunbar.capgemini_at_environment-agency.gov.uk>
Date: Wed, 5 Oct 2011 15:20:58 +0000
Message-ID: <341078561D12A54D8FEA365F2489FD27EEBD_at_EATOLEXMBS01.PRODDS.NTNL>



Stephane,

>> Hey, something new. I assume that 'id' is the PK (eg not
>> null) and that it's the right type (varchar2 or char).
I'm not 100% sure if it is the PK, but it is indeed varchar2 and it is indexed.

>> I suspect that the optimizer:
>> 1) is smart enough to see that LIKE '%' does nothing and remove it
>> 2) NOT smart enough to see that LIKE '%%' does nothing more
>> 3) and as you noticed, goes for indexes instead of
>> scanning tables with possibly a large number of rows.
Sounds reasonable.

>> Kind of trick based on a side-effect that is a potential
>> time-bomb ... The fact that LIKE '%something' hits an index
>> is just a matter of relative cost of scanning the index vs
>> scanning the table. Such things can change.
Indeed, a test on 11g shows the same plan (and execution time) for % and %% - so it's looking like it's 9i related, maybe also 10g.

This is something that the developer/users run manually through a front end to the system (No, I didn't write it!) and they found this trick and found that it works fine (under 9i) - eventually, the developer's curiosity got the better of him and he asked a DBA.

<SNIP>

Cheers,
Norm.

Norman Dunbar
Contract Senior Oracle DBA
Capgemini Database Build Team
Internal : 7 28 2051
External : 0113 231 2051

Information in this message may be confidential and may be legally privileged. If you have received this message by mistake, please notify the sender immediately, delete it and do not copy it to anyone else.

We have checked this email and its attachments for viruses. But you should still check any attachment before opening it. We may have to make this message and any reply to it public if asked to under the Freedom of Information Act, Data Protection Act or for litigation. Email messages and attachments sent to or from any Environment Agency address may also be accessed by someone other than the sender or recipient, for business purposes.

If we have sent you information and you wish to use it please read our terms and conditions which you can get by calling us on 08708 506 506. Find out more about the Environment Agency at www.environment-agency.gov.uk

--
http://www.freelists.org/webpage/oracle-l
Received on Wed Oct 05 2011 - 10:20:58 CDT

Original text of this message