Solved

need help on sql

Posted on 2014-10-23
10
178 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 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 334 total points
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 334 total points
Comment Utility
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 73

Accepted Solution

by:
sdstuber earned 166 total points
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
thanks a lot.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now