mysql - Min, MAX SQL 查询优化

标签 mysql sql

我有以下查询,我真的需要帮助优化它:

SELECT min(p.price) as min_price, max(p.price) as max_price
FROM product p
INNER JOIN product_category pc
    ON p.id_product = pc.id_product AND
       p.id_project = 1 AND
       pc.id_category = 2 AND
       p.active = 1 

表格是:

CREATE TABLE `product` (
  `id_product` bigint(10) UNSIGNED NOT NULL,
  `id_project` int(11) NOT NULL,
  `reference` varchar(50) NOT NULL,
  `reference_internal` varchar(125) NOT NULL,
  `sku` varchar(50) NOT NULL,
  `price` float(12,2) NOT NULL,
  `old_price` float NOT NULL,
  `reduction_amount` float NOT NULL,
  `reduction_percent` float NOT NULL,
  `is_reduced` int(1) NOT NULL,
  `id_manufacturer` int(10) NOT NULL,
  `id_supplier` int(10) NOT NULL,
  `is_new` int(1) NOT NULL,
  `popularity` int(1) NOT NULL,
  `quantity` int(1) NOT NULL,
  `active` int(1) NOT NULL,
  `date_add` datetime NOT NULL,
  `date_upd` datetime NOT NULL,
  `id_category` int(11) NOT NULL,
  `indexed` int(1) NOT NULL,
  `id_color` int(13) NOT NULL,
  `rating` int(1) NOT NULL,
  `rating_count` int(11) NOT NULL,
  `viewed` int(5) NOT NULL,
  `a_id_product` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `product`
  ADD PRIMARY KEY (`id_product`),
  ADD KEY `id_product` (`id_product`),
  ADD KEY `reference` (`reference`),
  ADD KEY `reference_internal` (`reference_internal`),
  ADD KEY `sku` (`sku`),
  ADD KEY `reduction_amount` (`reduction_amount`),
  ADD KEY `reduction_percent` (`reduction_percent`),
  ADD KEY `is_reduced` (`is_reduced`),
  ADD KEY `id_manufacturer` (`id_manufacturer`),
  ADD KEY `id_supplier` (`id_supplier`),
  ADD KEY `is_new` (`is_new`),
  ADD KEY `popularity` (`popularity`),
  ADD KEY `date_add` (`date_add`),
  ADD KEY `date_upd` (`date_upd`),
  ADD KEY `id_category` (`id_category`),
  ADD KEY `indexed` (`indexed`),
  ADD KEY `id_color` (`id_color`),
  ADD KEY `price` (`price`),
  ADD KEY `rating` (`rating`),
  ADD KEY `a_id_product` (`a_id_product`),
  ADD KEY `t7` (`price`,`id_product`,`id_project`,`active`) USING BTREE;
-- AUTO_INCREMENT for dumped tables
-- AUTO_INCREMENT for table `product`
ALTER TABLE `product`
  MODIFY `id_product` bigint(10) UNSIGNED NOT NULL AUTO_INCREMENT;
COMMIT;

有product_categories表:

CREATE TABLE `product_category` (
  `id_product` int(10) DEFAULT NULL,
  `id_project` int(11) NOT NULL,
  `id_category` int(5) NOT NULL,
  `menu_order` int(5) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `product_category`
--
ALTER TABLE `product_category`
  ADD KEY `id_product` (`id_product`),
  ADD KEY `id_project` (`id_project`),
  ADD KEY `id_category` (`id_category`),
  ADD KEY `menu_order` (`menu_order`),
  ADD KEY `t1` (`id_product`,`id_category`),
  ADD KEY `t2` (`id_product`,`id_project`,`id_category`);
COMMIT;

解释语句 https://snag.gy/8ClFnh.jpg

enter image description here 我有 45 毫秒的执行时间,但数据库不是很大,我有大约 20.000 个产品,但是当它上线时我将有数百万个产品,0.0550 秒的执行时间对于这个数量的产品来说似乎很长,任何人都可以帮助优化建议?

我有 mysql 5.7。

最佳答案

如果您选择的行数很少(占总数的 0.5% 或更少),您的查询应该很容易优化。

下面的索引应该有帮助:

create index ix1_product on product (id_project, active, price);

create index ix2_procat on product_category (id_product, id_category);

关于mysql - Min, MAX SQL 查询优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51572585/

相关文章:

java - 如何使用 JOOQ 刷新数据库连接

c# - 将 Linq 表达式转换为 sql server 查询

php - 如何使用 PHP SQL 解析器将数组值转换为 MySQL 查询?

mysql - Ormlite RawRowMapper 在使用 MySQL 的长字段上被阻塞

php - 附加 where 子句

sql - 您如何知道 SQL 数据库何时需要更多规范化?

c# - Microsoft 桌面搜索 - 包含在 Windows Server 2008 上不返回结果

mysql - 在单个查询中获取数据

mysql - 棘手的 SQL 连接查询

php - 如何在查询中选择小时和分钟