473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

optimize DB2 query

I want to start a query with wildcard at the beginning of a string e.g.
"select column1 from table1 where name like '%string'", or at both ends
of the string, e.g. "select column1 from table1 where name like
'%string%'", the speed is a lot slower than when the wildcard is only
at the end of the string 'string%', is there a way to get the query
faster? please help!

Apr 17 '06 #1
4 2415
In article <11**********************@z34g2000cwc.googlegroups .com>,
be******@gmail.com says...
I want to start a query with wildcard at the beginning of a string e.g.
"select column1 from table1 where name like '%string'", or at both ends
of the string, e.g. "select column1 from table1 where name like
'%string%'", the speed is a lot slower than when the wildcard is only
at the end of the string 'string%', is there a way to get the query
faster? please help!


No, when you use the % at the start of a like predicate the optimizer
always decides to do a full table scan.
Apr 17 '06 #2
"Gert van der Kooij" <no****@nl.invalid> wrote in message
news:MP************************@news.xs4all.nl...

No, when you use the % at the start of a like predicate the optimizer
always decides to do a full table scan.


Not necessarily. DB2 may do a complete scan of the index (assuming there is
an index on that column) by reading the index leaf pages sequentially
instead of using the b-tree. If no index is available, or the index is not
much smaller than the table, then a table scan would probably be used.

In order to use the b-tree of an index, the left most part of the index
needs to be supplied in the predicate (LIKE 'XXX%'). The same is true if a
predicate only supplies the 2nd column in a multi-column index.
Apr 18 '06 #3
Depending on how much work you want to do and how much control you have
over the database, you can at least speed up the ... like '%string'
query.

To do this, you would need to add an additional column to the table,
and store the contents of the first column in reverse order (so, if the
original column had 'string', then the new column would have 'gnirts').
Then, when you need to query for like '%string', you would instead use
the new column and query for 'gnirts%'.

Of course, for this to work, you need complete control over loading the
table and probably programatic control over the querying of the table
as well. Not a trivial task.

-Chris

Apr 18 '06 #4
Thanks everyone for your posts. All tables are created by a 3rd party
application, I have no control on the structure of the table but I can
define how to use the columns to save contents I want. Chris, I'm very
interested in your suggestion, and will definitely like to take a try.

Apr 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
6
by: Bruce D | last post by:
Could someone please help to explain why the following query isn't using the index... explain select id from kbm where state = 'MA' table type possible_keys key key_len ref rows Extra...
3
by: Paul Janssen | last post by:
Hello! Can anyone help me out with the following situation: (a) a single query with 550 id's in the IN-clause resulting into 800+ seconds; (b) 550 queries with a single id in the IN-clause...
5
by: xeqister | last post by:
Greetings all, We have a complicated statement in DB2 which takes long hour to complete and we have created most of the indexes. Does anybody knows how to tune the following statement to optimize...
3
by: Reddy | last post by:
The sql query for my datagrid returns 100, 000 records. But the datagrid should display 20 records per page. I am using datagrid paging, but it is taking too much time for the page to load. Is...
4
by: Huaer.XC | last post by:
>From the following MySQL command: EXPLAIN SELECT * FROM t1 JOIN t2 ON (t1.id = t2.id) JOIN t3 ON t3.name = t1.name WHERE t1.id IN(123, 124); which result is:...
13
by: Frank Swarbrick | last post by:
IBM has a product for the VSE operating system called the VSAM Redirector. It allows you to use VSAM to access RDBMS tables/views as if they were actual VSAM files. We're doing a comparison right...
11
by: bravo | last post by:
hi i need to optimize the join query, which joins three tables say table1 ,table2 , table3 each having huge volume of records... the query is as select table1.id,table2.time,table3.Status from...
1
by: acornejo | last post by:
Hi All I've the following code I need to optimize. Currently tblOutgoing is about 250K registers and growing at a rate of about 20k records per day. This code takes me over 5 secs to run on each...
3
zabsmarty
by: zabsmarty | last post by:
Can any one help me to make my query code optimize and load faster. Please help me or any example what steps should we use to optimize. Thank You
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.