Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query optimisation

Status
Not open for further replies.

hessodreamy

Programmer
Feb 28, 2005
59
GB
I have inherited a fairly clunky database design for the company I work at. This query I'm running takes nearly 3 minutes to run, so I daren't un it on the website!

What I need to do is delete items from the products table where that item does not appear in an order. I was starting off with just selecting the items concerned:

Code:
select p.productID, p.prodName from tProducts p left join tOrderProds op ON p.productID = op.productID where supplierID = 'LO' and op.productID is null;

Could anyone suggest a way to run the query without killing the server?

Here's the definition:
Code:
CREATE TABLE tOrderProds (
  purchaseOrder int(11) unsigned DEFAULT '0' NOT NULL ,
  productID int(11) unsigned DEFAULT '0' NOT NULL ,
  prodQty int(11)  DEFAULT '0' NOT NULL ,
  salePrice float  DEFAULT '0' NOT NULL ,
  costPrice float  DEFAULT '0' NOT NULL ,
  KEY purchaseOrder (purchaseOrder)
);

CREATE TABLE tProducts (
  productID int(11)  NOT NULL auto_increment,
  prodCode varchar(10)  DEFAULT '' NOT NULL ,
  prodName varchar(100)  DEFAULT '' NOT NULL ,
  supplierID varchar(10)  DEFAULT '' NOT NULL ,
  supplierCode varchar(20)  DEFAULT '' NOT NULL ,
  nPrice float  DEFAULT '0' NOT NULL ,
  PRIMARY KEY (productID),
  KEY prodCode (prodCode)
);
 
you appear to be missing two quite important indexes --
Code:
alter table tProducts 
add index suppix(supplierID)

alter table tOrderProds 
add index prodix(productID)

r937.com | rudy.ca
 
Ah yes. Worked a treat. I was wary of adding extra indexes that wouldn't often be used, as I understood that too many indexes were as bad as not enough, but that's a 'discuss...' type issue I'll go read up on. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top