开发者

Bulk select in Sybase

I have a few thousand col1, col2 distinct values. Col1 -> some primary key and Col 2 -> date.

For a third col 3, i have to query a large table, which gives me only few hundred results in most cases.

now my concern is how can write my where condition or use unions so that the number of times i am querying my DB decreases. my program i开发者_开发知识库s slow due to this.

Currently I was doing of doing something like below in my perl program using DBI.

select COL3 from Table where (COL1='v1' and COL2='Sep 25 2007 12:00AM' )  or (COL1='b3' and COL2='Sep 28 2007 12:00AM')
or (COL1='c1' and COL2='Sep 11 2007 12:00AM') and COL3='ABCD'
union 
select COL3 from Table where (COL1='v2' and COL2='Sep 28 2007 12:00AM') or (COL1='b2' and COL2='Oct  1 2007 12:00AM')
 or (COL1='c2' and COL2='Sep 28 2007 12:00AM') and COL3='ABCD'
 union 
select COL3 from Table where (COL1='v3' and COL2='Oct  1 2007 12:00AM') or (COL1='b1' and COL2='Sep 28 2007 12:00AM')
 or (COL1='c3' and COL2='Sep 24 2007 12:00AM') and COL3='ABCD'


A way of doing this is create a temporary table to hold all the col1 and col2 values you have. Insert the col1 and 2 values into the temp table and then do a query join between the temp table and Table (I am just doing for the first part of each or as the bitwith COL3='ABCD' will be similar

Parts of the code would be

create table #t
(
COL1 char(2) not null,
COL2 datetime not null
)

do the inserts

then

select col3 
  from Table
  inner join #t t on t.COL1 = Table.COL1 and t.COL2 = Table.COL2


For now I grouped things with count=25 removing union and I got good improvement in my program. Thanks, but if any better option is there, I am interested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜