mysql eav连接查询优化

cnh2zyt3  于 2021-07-29  发布在  Java
关注(0)|答案(0)|浏览(276)

我正在创建一个电子商务网站,其中包括产品及其变种。所以我决定使用eav进行属性Map,因为我可能有不确定的数据,比如(大小、颜色、材质)。但现在,我正努力从给定的属性和产品id中找到变体细节。
我创建了一个样本数据集。。。

CREATE TABLE `ec_attributes` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(155) NOT NULL,
  `value` varchar(155) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `display_value` varchar(155) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `ec_attributes` (`id`, `name`, `value`, `status`, `display_value`) VALUES
(1, 'size', 'L', 1, 'Large'),
(2, 'size', 'S', 1, 'Small'),
(3, 'colour', 'RED', 1, 'Red'),
(5, 'size', 'M', 1, 'Medium');

CREATE TABLE `ec_variant_attributes` (
  `attribute_id` int NOT NULL,
  `variant_id` int NOT NULL,
  PRIMARY KEY(`attribute_id`, `variant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `ec_variant_attributes` (`attribute_id`, `variant_id`) VALUES
(1, 4),
(1, 5),
(3, 4);

CREATE TABLE `ec_product_variant` (
  `product_id` int NOT NULL,
  `variant_id` int NOT NULL,
  `slug` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `ec_product_variant`
--

INSERT INTO `ec_product_variant` (`product_id`, `variant_id`, `slug`) VALUES
(1, 1, 'variant_1'), 
(1, 4, 'variant_2');

... 和sqlfiddle一样http://sqlfiddle.com/#!9/226323/4,并添加了一个获取详细信息的查询 (The Created query is so worst) .
所以我的表结构如下:
ec\产品\变型表

+------------+------------+-----------+
| product_id | variant_id | slug      |
+------------+------------+-----------+
|          1 |          1 | variant_1 |
|          1 |          4 | variant_2 |
+------------+------------+-----------+

ec\变量\属性

+--------------+------------+
| attribute_id | variant_id |
+--------------+------------+
|            1 |          4 |
|            1 |          5 |
|            3 |          4 |
+--------------+------------+

电子商务属性

+----+--------+-------+--------+---------------+
| id | name   | value | status | display_value |
+----+--------+-------+--------+---------------+
|  1 | size   | L     |      1 | Large         |
|  2 | size   | S     |      1 | Small         |
|  3 | colour | RED   |      1 | Red           |
|  5 | size   | M     |      1 | Medium        |
+----+--------+-------+--------+---------------+

我需要的是基于给定请求的产品变量表中的变量详细信息:
产品id=“1”
size=“l”
colour=“红色”
预期结果是产品变量详细信息

| product_id | variant_id |      slug |
|------------|------------|-----------|
|          1 |          4 | variant_2 |

我有一个在小提琴生成的查询,我需要更优化的解决方案。有人能帮我吗?
创建的查询是

select * from ec_product_variant WHERE variant_id = (SELECT size.variant_id FROM ec_variant_attributes size JOIN ec_variant_attributes AS
color ON color.variant_id = size.variant_id AND color.attribute_id = (SELECT id FROM `ec_attributes` WHERE name = 'colour' and value='RED') AND  
size.attribute_id = (SELECT id FROM `ec_attributes` WHERE name = 'size' and value='L')
)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题