Ok, so you have three tables. tblLibrary with key field LibID,
tblCategory with key field LctLatCode, and tblLibCat with no key field,
but multiple listings for LibID and LctLatCode. Why can't you just
join these in the normal manner and eliminate the sub-queries all
together?
Something like this (I probably don't have the field names correct,
though):
SELECT tblLibrary.*, tblLibCat.LctCatCode
FROM (tblLibrary INNER JOIN tblLibCat ON tblLibrary.LibID =
tblLibCat.LibID) INNER JOIN tblCategory ON tblLibCat.LctCatCode =
tblCategory.LctCatCode
WHERE (((tblLibCat.LctCatCode)="*190000")) OR
(((tblLibCat.LctCatCode)="*180000")) OR
(((tblLibCat.LctCatCode)="*010102")) OR
(((tblLibCat.LctCatCode)="*010101"));
This eliminates the sub-queries and lets the joins take care of it. I
have never used a sub-query and don't really see the need to as long as
you can just join the tables together into a view.
I bet it is the sub-queries, not the OR statements that is slowing you
down. If you have 1000 records and you have subqueries, then you will
probably have 1000 * 1000 accesses to the database when you run the
query. The join should eliminate most of those at the fore-front.
Alex.
Stefan Kowalski wrote:[color=blue]
> "Alex" <alex-brewer@rocketmail.com> wrote in message
> news:1107197551.739558.126650@f14g2000cwb.googlegr oups.com...[color=green]
> >I don't know if this would help at all, but you may try re-wording[/color][/color]
your[color=blue][color=green]
> > LIKE statement. Would it make a difference if you used "07*"[/color][/color]
instead[color=blue][color=green]
> > of "07????" ?
> >
> > This may keep the SQL engine from having to perform operations on
> > fixed-length strings and that may speed things up. Once it figures[/color][/color]
out[color=blue][color=green]
> > that the third digit is not correct, it does not need to check the
> > other three.
> >
> > On a similar tach, you could try to parse the string before you do[/color][/color]
your[color=blue][color=green]
> > compare. Setup a few more key fields. Have one called "CatClass :
> > left(LctLibID,3)" and another called "CatLevel: [LctLibID]"[/color][/color]
LangBreak[color=blue][color=green]
> > your WHERE clauses up between these different key fields. One for[/color][/color]
each[color=blue][color=green]
> > logical level for your code.
> >
> > Just a few thoughts....
> >
> > Alex.[/color]
>
>
>
> Hi Alex
> Thank you for your thoughts. Unfortunately they won't quite be[/color]
enough to[color=blue]
> make speed acceptable. Even if I do all the table indexing and even[/color]
if I go[color=blue]
> for an exact match (so no lengths to worry about), the OR clause just[/color]
kills[color=blue]
> it:
> SELECT *
> FROM tblLibrary
> WHERE (LibID IN (SELECT LctLibID FROM tblLibCat WHERE[/color]
LctCatCode="010101"))[color=blue]
> AND
> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode="010102"))
> OR
> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode="180000"))
> AND
> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode="190000"))
>
> Trying to run this query, then scroll to the bottom record takes 5[/color]
minutes[color=blue]
> of locked pc, whereas the query using tblLibrary.Subject list is[/color]
almost[color=blue]
> instant. I think this is the approach I will take, but I wonder[/color]
whether[color=blue]
> anyone else currently uses this method. It is a problem which must[/color]
occur in[color=blue]
> other types of databases and although
>
http://www.mvps.org/access/queries/qry0016.htm uses a 'helper table'[/color]
to find[color=blue]
> all of X having required Y, it does not address the OR issue.
> Perhaps someone has a database with a similar 3-table structure, eg[/color]
people[color=blue]
> and skills. So you ask who knows (Access and VBA) OR (VB and SQL[/color]
Server)[color=blue]
> Somebody out there must be solving these types of queries.
>
>
>[color=green]
> >
> > Stefan Kowalski wrote:[color=darkred]
> >> I recently posted a question which was answered by Allen Browne[/color][/color][/color]
and[color=blue][color=green]
> > gave me[color=darkred]
> >> some tips to structure the tables. However, when it comes to[/color]
> > searching the[color=darkred]
> >> database, performance is unacceptably slow and I am now thinking[/color][/color][/color]
of[color=blue][color=green]
> > doing[color=darkred]
> >> the unthinkable with my tables and wonder whether anyone has used[/color][/color][/color]
(or[color=blue][color=green]
> > still[color=darkred]
> >> uses) this sort of approach.
> >>
> >> The database stores libraries and classifies the types of book[/color][/color][/color]
they[color=blue][color=green]
> > keep. So[color=darkred]
> >> I have the classic 3-table design: tblLibrary, tblCategory,[/color]
> > tblLibCat, which[color=darkred]
> >> is the junction table showing LibID and CatCode. Additionally,[/color][/color][/color]
the[color=blue][color=green]
> > CatCodes[color=darkred]
> >> are masked so that
> >> 050000 = Languages
> >> 050100 = European Languages
> >> 050101 = English
> >> 050102 = French
> >> 050103 = German
> >>
> >> This enables me to ask "who stocks books on any European[/color][/color][/color]
language?"[color=blue][color=green]
> > as LIKE[color=darkred]
> >> "0501??". This was going OK until I tried to find out who stocks[/color]
> > (maths and[color=darkred]
> >> physics) or (computers and philosophy). This did not sound an
> >> overly-complex question to ask but as you may be able to guess[/color][/color][/color]
from[color=blue][color=green]
> > the SQL,[color=darkred]
> >> my pc went on strike for the next ten minutes, protesting mainly[/color][/color][/color]
over[color=blue][color=green]
> > the OR[color=darkred]
> >> clause. There are, by the way, about 25,000 libraries with about[/color]
> > 80,000[color=darkred]
> >> junction table records.
> >>
> >> SELECT * FROM tblLibrary WHERE
> >> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode LIKE[/color]
> > "07????"))[color=darkred]
> >> AND
> >> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode LIKE[/color]
> > "09????"))[color=darkred]
> >> OR
> >> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode LIKE[/color]
> > "12????"))[color=darkred]
> >> AND
> >> (LibID IN (SELECT LctLibID FROM tblLibCat WHERE LctCatCode LIKE[/color]
> > "19????"))[color=darkred]
> >>
> >> So what I did (shock/horror) was to create a field[/color]
> > tblLibrary.LibSubjectList[color=darkred]
> >> which contained a comma-separated list of all the categories[/color][/color][/color]
stocked[color=blue][color=green]
> > e.g.[color=darkred]
> >> ",070111,120100,120200,170602," which is just the sort of thing[/color][/color][/color]
you[color=blue][color=green]
> > don't[color=darkred]
> >> normally do in a relational database, although I am keeping the[/color]
> > junction[color=darkred]
> >> table as being the master list so I can always re-sync the subject[/color]
> > list.[color=darkred]
> >> The query now becomes:
> >>
> >> SELECT * FROM tblLibrary WHERE
> >> (LibSubjectList LIKE "*,07????,*" AND LibSubjectList LIKE[/color]
> > "*,09????,*" )[color=darkred]
> >> OR
> >> (LibSubjectList LIKE "*,12????,*" AND LibSubjectList LIKE[/color]
> > "*,19????,*" )[color=darkred]
> >>
> >> and performance is lightning quick. In fact, I can't ask a[/color][/color][/color]
question[color=blue][color=green]
> > about[color=darkred]
> >> which books are held which is not answered almost immediately.[/color][/color][/color]
There[color=blue][color=green]
> > are[color=darkred]
> >> some drawbacks though:
> >> 1. The SubjectList is a 255-character indexed text field which[/color][/color][/color]
means[color=blue][color=green]
> > the[color=darkred]
> >> maximum number of subjects per library is 36. That is, 6[/color][/color][/color]
characters[color=blue][color=green]
> > per[color=darkred]
> >> subject plus a trailing comma multiplied by 36 then add a comma to[/color]
> > the[color=darkred]
> >> beginning of the list gives 253 characters. This limitation is[/color]
> > acceptable.[color=darkred]
> >> 2. I cannot enforce referential integrity. Although I can[/color][/color][/color]
always[color=blue][color=green]
> > re-sync[color=darkred]
> >> the subject list with the junction table and the classifications[/color]
> > should stay[color=darkred]
> >> virtually unchanged throughout the life of the database, it is[/color][/color][/color]
still[color=blue][color=green][color=darkred]
> >> something I need to be extremely cautious about. If the two lists[/color]
> > don't[color=darkred]
> >> match, all the searching goes wrong.
> >> 3. I have never used this approach before, but no other seems to[/color]
> > give[color=darkred]
> >> acceptable speed and I can't turn round and say (maths and[/color][/color][/color]
physics)[color=blue][color=green]
> > or[color=darkred]
> >> (computers and philosophy) is too difficult a question!
> >>
> >> Any comments anyone?[/color]
> >[/color][/color]