我有3张桌子:

Nutritions_facts表是具有所有营养Nutrition_id和Nutrition_ename的静态表
具有产品信息的Products_info表,其列为product_id,product_ename,brand
product_nutrition_facts
 这是将产品与其营养成分及其价值联系起来的调解员表。结构为product_id,营养_id,营养值..每个产品可以具有1行或更多行,具体取决于其拥有的营养成分的数量。
这是一个真实的测试示例

营养表

 nutrition_id |nutrition_ename
       1 | caloreis
       2 | fat
       3 | sugar
       4 | salt


Products_info表

product_id| product_ename           | brand
    1 | Nutella Hazelnut Cocoa | Nutella
    2 | Nutella Jar            | Nutella


product_nutrition_facts表

product_id | nutrition_id | nutrition_value
     1 |            1 |             200
     1 |            2 |              15
     1 |            3 |               2
     1 |            4 |              11
     2 |            1 |             200
     2 |            2 |              15
     2 |            3 |              12
     2 |            4 |              11


我需要查询返回的产品名称,其中糖的值小于或等于15,盐的值小于或等于140

我建立了一个查询,该查询返回正确的值,但处理时间很长。有人可以建议修改以提高性能。

SELECT DISTINCT p.product_id, p.brand, p.e_name, p.image_low
FROM products_info p
JOIN product_nutrition_facts pn ON p.product_id = pn.product_id
WHERE p.brand =  'AL MARAI'
AND (
(
p.product_id
IN (

SELECT product_id
FROM product_nutrition_facts pn
WHERE pn.nutrition_id =3
AND pn.nutrition_value <=15
)
 )
 AND (
 p.product_id
IN (

SELECT product_id
FROM product_nutrition_facts pn
WHERE pn.nutrition_id =4
AND pn.nutrition_value <=140
)
)
)
AND (
pn.nutrition_id =3
OR pn.nutrition_id =4
)


编辑

 CREATE TABLE `products_info` (
 `product_id` int(11) NOT NULL AUTO_INCREMENT,
 `image_low` varchar(400) DEFAULT NULL,
  `e_name` varchar(200) DEFAULT NULL,
 PRIMARY KEY (`product_id`),
 UNIQUE KEY `product_id_UNIQUE` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=249292 DEFAULT CHARSET=utf8


CREATE TABLE `product_nutrition_facts` (
 `prod_nut_id` int(11) NOT NULL AUTO_INCREMENT,
 `product_id` int(11) DEFAULT NULL,
 `nutrition_id` int(11) DEFAULT NULL,
 `nutrition_value` varchar(25) DEFAULT NULL,
 `unit_id` int(11) DEFAULT NULL,
 `parent` int(11) DEFAULT NULL,
 `serving_size` varchar(145) DEFAULT NULL,
 `serving_size_unit` int(11) DEFAULT NULL,
 `no_nutrition_facts` int(11) NOT NULL,
 `added_by` varchar(145) DEFAULT NULL,
 `last_update` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 `inserted_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
 `updated_by` varchar(150) NOT NULL,
 PRIMARY KEY (`prod_nut_id`),
 UNIQUE KEY `prod_nut_id_UNIQUE` (`prod_nut_id`),
 KEY `nutrition_id_fk_idx` (`nutrition_id`),
 KEY `unit_Fk_idx` (`unit_id`),
 KEY `unit_fk1_idx` (`serving_size_unit`),
 KEY `product_idFK` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=580809 DEFAULT CHARSET=utf8

 CREATE TABLE `nutrition_facts` (
 `nutrition_id` int(11) NOT NULL AUTO_INCREMENT,
 `nutrition_aname` varchar(145) DEFAULT NULL,
 `nutrition_ename` varchar(145) DEFAULT NULL,
 `alternative_name` varchar(145) DEFAULT NULL,
  `unit` varchar(8) NOT NULL,
 `daily_value` int(11) NOT NULL,
 `nut_order` int(2) NOT NULL,
 `is_child` int(1) NOT NULL,
 `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`nutrition_id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8

最佳答案

尝试添加索引product_nutrition_facts (nutrition_id,nutrition_value,product_id)product_nutrition_facts (product_id,nutrition_id,nutrition_value)products_info (brand)并执行查询

关于mysql - 在mysql中应用多个联接时提高性能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35312713/

10-13 02:03