If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query optimisation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-05, 12:03
hessodreamy hessodreamy is offline
Registered User
 
Join Date: Mar 2005
Posts: 16
Query optimisation

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? I know left joins are slow, but can't think of another way to do it. BTW I'm stuck in the dark ages running 4.0.

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)
);
Reply With Quote
  #2 (permalink)  
Old 10-10-05, 19:40
bstjean bstjean is offline
Registered User
 
Join Date: Sep 2002
Location: Montreal, Canada
Posts: 219
Quote:
Originally Posted by hessodreamy
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? I know left joins are slow, but can't think of another way to do it. BTW I'm stuck in the dark ages running 4.0.

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)
);

Can you post the EXPLAIN of that SQL query as well as the SHOW INDEX on the tables?
Reply With Quote
  #3 (permalink)  
Old 10-10-05, 21:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On