Solved

need help on sql

Posted on 2014-10-23
10
182 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
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 73

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 73

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
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 76

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 73

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 76

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 73

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

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.

Question has a verified solution.

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

Suggested Solutions

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

803 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