Problem w/ SQL Select Statement in Oracle.

Status
Not open for further replies.

JamHandy

Ars Praefectus
5,756
Subscriptor
My problem is all attempts to exclude data listed in the info table have met w/ failure. My problem is further complicated as it takes almost 2 hours to complete a search (if it doesn't time out first) due to other activity on the server. I can get off some searches early in the morning, but all of my variations in trying to exclude these terms have failed. I have tried 'not like' or '!=' and neither works. I have mocked up a script below which shows what I am trying to do

Let's say I am trying to return as a result from the database all of the names of all the owners of Briggs and Stratton mowers sold which are not 30 HP and which were sold by SEARS. No problem getting all of the target mowers and excluding the 30 HP ones (incidentally, the actual database has nothing to do w/ mowers).

Needless to say this is getting a bit frustrating.

My database has 3 tables; name, main & info.

Name contains a unique ID number; id and the owners name; name
Main contains the unique ID number; id, filedat,
Info contains the unique ID number; id, the manufacter; manu, horse power; hp, & store

1 select unique(name),no,filedat,manu,model,store
2 from info e, main m, name p
3 where e.id=m.id and m.id=p.id
4 and hp not like '%30%'
5 and (manu like '%BRIGGS%' or manu like '%STRATTON%')
6 and (store != 'JOES' or store != 'KMART'
7 or store != 'HOMEDEPOT' or store != 'HECHINGERS')
8 order by name,model;

Tried many variations to the syntax with wildcards, w/o wildcards, spaces bteween the brackets and the excluded terms and I still return all of the records. I am open to any suggestions
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
Does oracle support != syntax?
I would have done the query more like:

1 select unique(name),no,filedat,manu,model,store
2 from info e, main m, name p
3 where e.id=m.id and m.id=p.id
4 and hp not like '%30%'
5 and (manu like '%BRIGGS%' or manu like '%STRATTON%')
6 and not store in ('JOES','KMART', 'HOMEDEPOT', 'HECHINGERS')
7 order by name,model;

and alternate query that could make a significant difference, based on what you said(see comments below for explanation):

1 select unique(name),no,filedat,manu,model,store
2 from info e, main m, name p
3 where e.id=m.id and m.id=p.id
4 and hp not like '%30%'
5 and (manu like '%BRIGGS%' or manu like '%STRATTON%')
6 and store = 'SEARS'
7 order by name,model;

A good portion of your perofmance problems is probably in the fact that you are doing some *major* string comparisons in there. That "%30%" in the hp table alone is going to cause a big big hit on the db.
Every one of those % is causing serious string compares. And you are doing it in 4 different fields.

Your string compares are probably causing what are known as table scans, basically, every single record in the table has to be checked.

Indexes aren't really going to affect the perfomance of this query because you don't have anything that isn't a string compare in the where clause, so the db can't reduce the working set of data. It has to check each record. So as your table grows, so does your query time, linearly with the table size. Twice as many records will take twice as long to scan.

Your return results could be off because of case comparison problems too. I don't rememeber if Oracle is case insensitive by default or not, so you may have to check that. I just realized too, you do know that the '%30%' it's not going to match just 30. It's going to match anything with 30 in it, i.e. 30, 230, 300 etc..

The first thing you should do is find out if you can reduce the string compare problem or address it some other way. BTW, the way your data elements are, if at all possible you need to seriously normalize your db, because it doesn't look like it is, right now. Normalization is a tremendous part of utilizing a relationaly database to it's fullest. If your data were normalized, this would be a trivial query that would execute fairly quickly, but because you have string searches, that's why it's slow. It's probably not db load that is causing your performance problem.

Ok, the other thing you should do is look into placing the results of your queries into temp tables and working with the data from there.

Oh, and your search logic approach seems a bit backwards to me. You say you want all mowers sold by sears. You seem to be getting this not by matching where store = sears, but by excluding where it doesn't = sears. Try approacing the search looking for store = instead of store doesn't =, and it will probably help some. The second query I listed shows what I mean. That change alone would reduce the string compares performed in that field by a factor of 4.

I just noticed too...is hp actually stored as a string? If so, is there a specific reason why? That field really should be stored as a numeric of some kind. Doing so would increase searches against it dramatically, and would also easily allow you to do range searches (i.e. hp < 150 AND hp > 30), where as a string is a major pain to do that(you have to convert to numeric then compare if it's a string to do the same thing, a process which would slow the search down a lot)

edit:grammar and error in SQL
edit 2: better explanation
edit 3: added alternative query
edit 4: questions about the data added
edit 5: another comment on the SQL
-J

[This message was edited by colema19 on December 06, 2001 at 10:32.]

[This message was edited by colema19 on December 06, 2001 at 10:35.]

[This message was edited by colema19 on December 06, 2001 at 10:42.]
 
First of all, colema19's SQL looks more likely to work to me, but as he says, your your database schema looks like it needs more work.

You really need to have seperate table for stores and manufacturers - i guess the reason you have to use LIKE for the manufacturers is because they are typed in and, the user doesn't always type the same thing in?

You should have a table manufaturers with a serial int column manu_id which is a primary key, and the name as a string, giving you

1 BRIGGS
2 STRATTON

etc.

Then you can have the manu column in info be an indexed foreign key, which should be very quick to look up.

Do the same everywhere you have repeated data.

Is HP always numeric? if so you should represent it numerically, not as a string.

Make sure you have indexes on anything you compare or sort.
Use primary and foreign keys wherever you do a join.

Getting all this sorted out will take a while, but no matter how overloaded, a well designed and indexed database should be pretty snappy for a simple select - it is all the text comparisons which are making it slow.

If you have to do these, it is best if you can manage to get it to 'BRIGGGS%' rather than '%BRIGGS%', as this allows the index to do a lot of your searching for you.

Or try and find out if oracle has an equivelant of SQL Servers full text search.

When you get your query working, run it through a performance analyser to see what is taking the time.

Edit: Well it seems Colema19 added a lot of what I said, but I'll leave it in anyway.

[This message was edited by Charley's Angel on December 06, 2001 at 10:55.]
 

JamHandy

Ars Praefectus
5,756
Subscriptor
A good portion of your perofmance problems is probably in the fact that you are doing some *major* string comparisons in there. That "%30%" in the hp table alone is going to cause a big big hit on the db. Every one of those % is causing serious string compares. And you are doing it in 4 different fields.

That's why I am trying to keep the wild cards out of my !=/not like exclusions. But the script ran very fast early this AM before everyone else started hitting the server doing other stuff, but .

Your string compares are probably causing what are known as table scans, basically, every single record in the table has to be
checked. Indexes aren't really going to affect the perfomance of this query because you don't have anything that isn't a string compare
in the where clause, so the db can't reduce the working set of data. It has to check each record. So as your table grows, so does
your query time, linearly with the table size. Twice as many records will take twice as long to scan.

Your return results could be off because of case comparison problems too. I don't rememeber if Oracle is case insensitive by defaultor not, so you may have to check that.

It is case sensitive, and everything in the database is uppercase.

The first thing you should do is find out if you can reduce the string compare problem or address it some other way. BTW, the way your data elements are, if at all possible you need to seriously normalize your db, because it doesn't look like it is, right now.

Normalization is a tremendous part of utilizing a relationaly database to it's fullest. If your data were normalized, this would be a trivial query that would execute fairly quickly, but because you have string searches, that's why it's slow. It's probably not db load that is causing your performance problem.

Unfortunately I cannot do much as I have to get autherization and funding to do any changes. It is an old database that we need to pull data from only occasionaly.

Ok, the other thing you should do is look into placing the results of your queries into temp tables and working with the data from there.

That is the next step I am anticipating.

Oh, and your search logic approach seems a bit backwards to me. You say you want all mowers sold by sears. You seem to be getting this not by matching where store = sears, but by excluding where it doesn't = sears. Try approacing the search looking for store = instead of store doesn't =, and it will probably help some. The second query I listed shows what I mean. That change alone would reduce the string compares performed in that field by a factor of 4.

I should of anticipated this suggestion. The data represented by store=SEARS in my example actually has numerous variations (more than 30), so it isn't as simple as I represented it (wish it was).

I just noticed too...is hp actually stored as a string?
Again, becuase i had to change the information it might be misleading. The data represented by hp has non numeric characters combined w/ numeric so it could no be setup as a number.

To sum up neither the != or not like syntax parse out what I need to parse out. The performance issue is secondary.

Thanks a ton for replying
 

Dandy Andy

Ars Tribunus Militum
2,458
Some of the possible reasons your performance is diabolical are as follows:

1) A leading '%' in the search failure will stop Oracle using any index that may exist on the column.

- 'BRIGGS%' will use a unique index, if one exists
- '%BRIGGS%' causes the database to perform a full table scan

2) '!=' and '<>' and 'NOT LIKE' also result in a full table scan. Oracle can use an index to search for a specific value, viz:

- WHERE store = 'SEARS'

but not to exclude values - it will need to search ALL values, and ignore those which are excluded. A full table scan is more efficient than an index scan in these circumstances. NOT EXISTS will allow you to use a correlated subquery to exclude the appropriate rows - this can be written to allow the use of an available index.

So, if we assume that there are unique indexes on id, hp, manu and store, then the following should be a bit zippier:

code:<HR>
Code:
<P>SELECT name, no, filedat, manu, model, store
  FROM info i, main m, name p
 WHERE i.id = m.id
   AND m.id = p.id
   AND manu LIKE 'BRIGGS%'
    OR manu LIKE 'STRATTON%'
   AND NOT EXISTS (SELECT 'X'
                     FROM info i2
                    WHERE i2.hp = '30'
                      AND i2.id = i.id)
   AND NOT EXISTS (SELECT 'X'
                     FROM info i3
                    WHERE i3.id = i.id
                      AND i3.store IN('JOES','KMART','HOMEDEPOT','HECHINGERS'))
 ORDER BY name, model ;<P>

The above works because Oracle will (should) perform index scans on both of the correlated subqueries and the main query, before merging the results. If you are using the C.B.O., then the exact execution plan will depend on the data in your tables - make sure your statistics are up to date. If you are using the R.B.O., then make sure the smallest table (i.e. fewest rows) is the last in the 'FROM' clause of the outer select.

Incidentally, the above is partly guesswork - I don't know EXACTLY what your data looks like!

;)
 

JamHandy

Ars Praefectus
5,756
Subscriptor
Incidentally, the above is partly guesswork - I don't know EXACTLY what your data looks like!

Sorry to be coy, but I had to come up with a quick example and I am afraid it was not complete enough to make any sense.

But I think you have hit on it. What I have here is repeating fields and must exclude every variation of the excluded terms I want to exclude. Can't get into any more detail now.

I will try your somewhat elegant solution and let you know how it works.

Thanks
 
Status
Not open for further replies.