Solved

need help on sql

Posted on 2014-10-23
10
192 Views
Last Modified: 2014-10-28
Hi ,

I need to know the difference between using like operator or DBMS_LOB.INSTR for an CLOB column. I need to know which one is better and efficient in processing

Select *
from user_notes where lower(description) like '%abd%'

or
Select *
From user_notes where DBMS_LOB.INSTR(lower(description),'abc') >0;

Here description is an CLOB column
0
Comment
Question by:sam_2012
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 334 total points
ID: 40400214
Test each in your environment and see.

What do the explain plans show for each?

Set up a simple table, populate it with random data.

Then run tests against it.

Maybe create a loop of 1000 iterations, do the same query and check the stats?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40400219
neither one is good, but the LIKE should be more efficient because you won't be incurring sql - pl/sql context switches

but definitely test it for yourself
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40400240
note, if you have a specific substring that you will always be searching for, then you could create a function-based index that would be very efficient

CREATE INDEX idx_user_notes_abc ON user_notes (DBMS_LOB.INSTR(lower(description),'abc'));


but, this does not work if you will be searching for variable strings because you would need to create a separate index for every possible string.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 334 total points
ID: 40400252
I went ahead as set up the simple test case I was referring to.

Based on the runs, they appear to be about the same but your mileage may vary.

dbms_random.string is limited to varchar2 so I had to limit the possible data to 4000 characters.  If you want more, it is a small tweak to generate larger dummy data.

drop table tab1 purge;
create table tab1(col1 clob);

begin
	for i in 1..10000 loop
		insert into tab1 values(dbms_random.string('a',dbms_random.value(1,4000)));
	end loop;
	commit;
end;
/


-- individual stats
set lines 120

set autotrace on
select count(*) from tab1 where lower(col1) like '%abc%';

select count(*) from tab1 where dbms_lob.instr(lower(col1),'abc') > 0;

set autotrace off

*/

--loop timings
declare
	start_time timestamp;
	junk number;
begin
	start_time := systimestamp;
	for i in 1..10000 loop
		select count(*) into junk from tab1 where lower(col1) like '%abc%';
	end loop;
	dbms_output.put_line('Run time: ' || to_char(systimestamp-start_time));

	start_time := systimestamp;
	for i in 1..10000 loop
		select count(*) into junk from tab1 where dbms_lob.instr(lower(col1),'abc') > 0;
	end loop;
	dbms_output.put_line('Run time: ' || to_char(systimestamp-start_time));
end;
/

Open in new window

0
 
LVL 74

Accepted Solution

by:
sdstuber earned 166 total points
ID: 40400280
In my tests the elapsed time was also approximately the same

Howerver

the pl/sql route consumed more pga memory
the sql LIKE route consumed more db time

interestingly - the LIKE performed fewer gets and logical reads.  So it seems like it should have been faster but it wasn't measurable for me
0
 
LVL 32

Expert Comment

by:awking00
ID: 40402075
Couldn't a context index be in order here? I've had some pretty good experience with them although I've never combined them with a function like lower() on the indexed column.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40402082
awking00,

Yes, Oracle Text is the correct way to go and it is case insensitive so no need for lower.

Check out the askers previous questions, for some reason they have been asked to NOT use a Text index:
http://www.experts-exchange.com/Database/Oracle/Q_28528077.html
0
 
LVL 32

Expert Comment

by:awking00
ID: 40402121
slightwv,
That's an odd restriction. As you and sdstuber both indicated, "You can use this paint brush to drive that nail, but not this hammer."
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40402855
When I see people doing searches like this I suggest this exercise to try to relate to the problem.

Grab a dictionary, find all words that begin with "p".
Within a few seconds they have pinched off the pages with those words.
The alphabet makes a great index.  If your dictionary has labeled thumb tabs it's even easier.  Regardless, it's a trivial effort.

Now,  find all words that have a "p" in the middle.
When they inevitably responde: "I can't do that - I'd have to read the entire dictionary"

Then they have the answer as to why these searches don't work.
0
 

Author Closing Comment

by:sam_2012
ID: 40408990
thanks a lot.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question