mysql - 在 mysql 中应用多个联接时提高性能

标签 mysql performance join multiple-tables

我有 3 张 table :

Nutritions_facts 表是静态表,包含所有营养的 Nutrition_id 和 Nutrition_ename Products_info 表,其中包含产品信息,列为product_id、product_ename、brand 产品营养事实 这是将产品及其营养成分及其值(value)联系起来的中介表。结构为product_id、nutritional_id、nutritional_value .. 每个产品可以有 1 行或更多行,具体取决于其所含营养成分的数量。 这是一个真实的测试示例

营养事实表

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

产品信息表

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

产品营养事实表

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)并执行查询

SELECT p.*
FROM products_info p
join product_nutrition_facts pn1 on
  pn1.product_id=p.product_id
  AND pn1.nutrition_id=3
  AND pn1.nutrition_value<=15
join product_nutrition_facts pn2 on
  pn2.product_id=p.product_id
  AND pn2.nutrition_id=4
  AND pn2.nutrition_value<=140
where
  p.brand =  'AL MARAI'

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

相关文章:

php - 从具有混合格式的列中提取包大小

mysql - 如何使用通配符删除/替换mysql中的部分字符串?

performance - 在 Akamai 中压缩内容

python - 找到两对总和为相同值的对

python - 在 Pandas 中使用 join 进行 vlookup

MySQL 查询 - 转换为左连接以显示零结果?

mysql - 无法使用 mysql 插入查询插入文本

mysql - 我如何破解这个 SQL Soccer Matches 作业?

c - 提高 SQLite 的每秒插入性能

ruby-on-rails - 如何在多态连接表上强制唯一性?