我在进行良好的 MySQL 查询方面非常糟糕。我创建了这个:
SELECT SQL_CALC_FOUND_ROWS
`products_stock`.`products_id`,
`products_stock`.`products_stock_attributes`,
`products_stock`.`products_stock_quantity`,
`products`.`manufacturers_id`,
`products_description`.`products_name`
FROM `products_stock`
LEFT JOIN `products`
ON `products_stock`.`products_id` = `products`.`products_id`
LEFT JOIN `products_description`
ON `products_stock`.`products_id` = `products_description`.`products_id`
LEFT JOIN `products_to_categories`
ON `products_stock`.`products_id` = `products_to_categories`.`products_id`
WHERE `products_stock`.`products_stock_quantity` >=3
AND `products`.`products_status` = 1
AND ISNULL(`products`.`products_image`) = false
AND `products`.`products_image` != ""
AND EXISTS(
select * from `allegro`
where `products_stock`.`products_id` = `allegro`.`product_id`
and `allegro`.`attributes` = `products_stock`.`products_stock_attributes`
) = false
products
表有大约 17k 行,
allegro
表有大约 3k 行。
查询Idea是选择所有产品,其中stock_quanity > 3,哪里是照片,哪里是allegro
表中没有产品id。
现在查询大约需要 10 秒。我不知道如何优化它。
@解决了
我已经为 allegro.product_id 和 allegro.attributes 添加了索引,现在查询时间不到半秒。谢谢大家的帮助