Jump to content

How to improve this query


needs_upgrade

Recommended Posts

hello guys, i wanted to improve my query. it works but it's slow. how can i improve it? do i need to rewrite my query or change the table design? pls feel free to criticize. thanks a lot.

 

here's the mysql code:

$sql = "SELECT product_num FROM products WHERE product_id IN 
			(
			SELECT to_product_id FROM tconversions WHERE fr_product_id IN
				(
				SELECT product_id FROM products WHERE product_num = '$product_num'
				)
			)";

 

and here are the tables' descriptions:

 

CREATE TABLE IF NOT EXISTS `products` (
  `product_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
  `product_name` varchar(75) COLLATE latin1_general_ci NOT NULL,
  `product_num` varchar(6) COLLATE latin1_general_ci NOT NULL,
  `category_id` int(3) NOT NULL,
  `manufacturer_id` int(2) NOT NULL DEFAULT '0',
  `sell_price` decimal(9,2) NOT NULL DEFAULT '0.00',
  `active` int(1) NOT NULL COMMENT '1 if active, 0 if inactive',
  `lastsale` date NOT NULL,
  PRIMARY KEY (`product_id`)
) ENGINE=MyISAM

CREATE TABLE IF NOT EXISTS `tconversions` (
  `tconv_id` int(6) NOT NULL AUTO_INCREMENT,
  `fr_product_id` int(5) NOT NULL,
  `to_product_id` int(5) NOT NULL,
  `conv_units` decimal(8,2) NOT NULL,
  PRIMARY KEY (`tconv_id`)
) ENGINE=MyISAM;

 

Link to comment
https://forums.phpfreaks.com/topic/248675-how-to-improve-this-query/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.