Unix Technical Forum

A few SQL questions...

This is a discussion on A few SQL questions... within the Sybase forums, part of the Database Server Software category; --> Greetings experts, I have an sql search that requires 'OR' search on two different _fields_ eg. (Sun -- Sybase ...


Go Back   Unix Technical Forum > Database Server Software > Sybase

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 04:10 PM
Rostislav Lyudmirsky
 
Posts: n/a
Default A few SQL questions...

Greetings experts,

I have an sql search that requires 'OR' search on two different _fields_
eg. (Sun -- Sybase databases)
select f1, f2, ..., fn
from tableA, tableB
where (tableA.fld_1 = 'abc' OR tableA.fld_2 = 'abc')
and tableA.fld_i = tableB.fld_j

Without OR, it takes 1 sec to get data from tableA with 500,000 records.
With OR, it takes almost 15 - 20 seconds even though both of the search
fields are indexed.

Is there a way to speed up this search?
---------------
Another question... what is the difference between WHERE and HAVING?
select ... from ... where ...
select ... from ... having ...
They're both used for searching, is there any need to use one or the other?

Thank you for your help.
P.S. I would appreciate if you CC your reply to my email: rlyudmirsky@abac.com
Thanks.
-----------------------------------------------------------------------------
Rostislav "Steve" Lyudmirsky - rlyudmirsky@abac.com - http://rvl.netfirms.com
-----------------------------------------------------------------------------
"The Universe... is a pretty big place. It's bigger than anything, anyone has
ever dreamed of before. So, if it's just us... it seems like an awful waste
of space." -- Ellie Arroway (Contact)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 04:10 PM
Rob Verschoor
 
Posts: n/a
Default Re: A few SQL questions...

The classic trick is to split such a query into two parts and connect
these in a union:

select f1, f2, ..., fn from tableA, tableB
where tableA.fld_1 = 'abc' and tableA.fld_i = tableB.fld_j
union
select f1, f2, ..., fn from tableA, tableB
where tableA.fld_2 = 'abc' and tableA.fld_i = tableB.fld_j

Each of these queries will be optimised separately. The assumption is
that each individual query will be fast, but due to the OR, a less
efficient query plan is chosen (often a table scan). When splitting it
into two queries as above, we'd therefore hope to see improved
performance.

HTH,

Rob
-------------------------------------------------------------
Rob Verschoor

Certified Sybase Professional DBA for ASE 12.5/12.0/11.5/11.0
and Replication Server 12.5

Author of "Tips, Tricks & Recipes for Sybase ASE" and
"The Complete Sybase ASE Quick Reference Guide"
Online orders accepted at http://www.sypron.nl/shop

mailto:rob@YOUR.SPAM.sypron.nl.NOT.FOR.ME
http://www.sypron.nl
Sypron B.V., P.O.Box 10695, 2501HR Den Haag, The Netherlands
-------------------------------------------------------------

"Rostislav Lyudmirsky" <rlyudmirsky@abac.com> wrote in message
news:69fb089d.0312220813.31856693@posting.google.c om...
> Greetings experts,
>
> I have an sql search that requires 'OR' search on two different

_fields_
> eg. (Sun -- Sybase databases)
> select f1, f2, ..., fn
> from tableA, tableB
> where (tableA.fld_1 = 'abc' OR tableA.fld_2 = 'abc')
> and tableA.fld_i = tableB.fld_j
>
> Without OR, it takes 1 sec to get data from tableA with 500,000

records.
> With OR, it takes almost 15 - 20 seconds even though both of the

search
> fields are indexed.
>
> Is there a way to speed up this search?
> ---------------
> Another question... what is the difference between WHERE and HAVING?
> select ... from ... where ...
> select ... from ... having ...
> They're both used for searching, is there any need to use one or the

other?
>
> Thank you for your help.
> P.S. I would appreciate if you CC your reply to my email:

rlyudmirsky@abac.com
> Thanks.
> --------------------------------------------------------------------

---------
> Rostislav "Steve" Lyudmirsky - rlyudmirsky@abac.com -

http://rvl.netfirms.com
> --------------------------------------------------------------------

---------
> "The Universe... is a pretty big place. It's bigger than anything,

anyone has
> ever dreamed of before. So, if it's just us... it seems like an

awful waste
> of space." -- Ellie Arroway (Contact)


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 04:10 PM
Pablo Sanchez
 
Posts: n/a
Default Re: A few SQL questions...

"Rob Verschoor" <rob@DO.NOT.SPAM.sypron.nl.REMOVE.THIS.DECOY> wrote
in news:3fe72774$0$64977$1b62eedf@news.euronet.nl:

> select f1, f2, ..., fn from tableA, tableB
> where tableA.fld_1 = 'abc' and tableA.fld_i = tableB.fld_j
> union
> select f1, f2, ..., fn from tableA, tableB
> where tableA.fld_2 = 'abc' and tableA.fld_i = tableB.fld_j


Just to add to Rob's point, read up on the difference between UNION
and UNION ALL. If you can avoid the de-duplication phase, you're that
much further along.

Thx!
--
Pablo Sanchez - Blueoak Database Engineering, Inc
http://www.blueoakdb.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:13 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com