We're having a problem with a query that keeps executing very very slowly
SELECT `e`.*,
IF(at_news_from_date.value_id > 0, at_news_from_date.value, at_news_from_date_default.value) AS `news_from_date`,
IF(at_news_to_date.value_id > 0, at_news_to_date.value, at_news_to_date_default.value) AS `news_to_date`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_website` AS `product_website`
ON product_website.product_id = e.entity_id AND product_website.website_id = 3
LEFT JOIN `catalog_product_entity_datetime` AS `at_news_from_date_default`
ON (`at_news_from_date_default`.`entity_id` = `e`.`entity_id`) AND
(`at_news_from_date_default`.`attribute_id` = '81') AND `at_news_from_date_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_datetime` AS `at_news_from_date`
ON (`at_news_from_date`.`entity_id` = `e`.`entity_id`) AND
(`at_news_from_date`.`attribute_id` = '81') AND (`at_news_from_date`.`store_id` = 9)
LEFT JOIN `catalog_product_entity_datetime` AS `at_news_to_date_default`
ON (`at_news_to_date_default`.`entity_id` = `e`.`entity_id`) AND
(`at_news_to_date_default`.`attribute_id` = '82') AND `at_news_to_date_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_datetime` AS `at_news_to_date`
ON (`at_news_to_date`.`entity_id` = `e`.`entity_id`) AND
(`at_news_to_date`.`attribute_id` = '82') AND (`at_news_to_date`.`store_id` = 9)
WHERE (((((IF(at_news_from_date.value_id > 0, at_news_from_date.value, at_news_from_date_default.value) <=
'2021-10-06 23:59:59') OR
(IF(at_news_from_date.value_id > 0, at_news_from_date.value, at_news_from_date_default.value) IS null)))))
AND (((((IF(at_news_to_date.value_id > 0, at_news_to_date.value, at_news_to_date_default.value) >=
'2021-10-06 00:00:00') OR
(IF(at_news_to_date.value_id > 0, at_news_to_date.value, at_news_to_date_default.value) IS null)))))
AND ((IF(at_news_from_date.value_id > 0, at_news_from_date.value, at_news_from_date_default.value) IS not null) OR
(IF(at_news_to_date.value_id > 0, at_news_to_date.value, at_news_to_date_default.value) IS not null))
The query provides a list of new products but takes minutes to execute. I'm not sure how to optimize this as all the conditions are needed to actually determine the new products.
It's actually taking minutes to execute.
Adding some profile results
And explain
Is it possible to somehow tweak the database to improve the execution time of this query?
Here's the structure on the used tables
-- auto-generated definition
create table catalog_product_entity
(
entity_id int unsigned auto_increment comment 'Entity ID'
primary key,
attribute_set_id smallint unsigned default 0 not null comment 'Attribute Set ID',
type_id varchar(32) default 'simple' not null comment 'Type ID',
sku varchar(64) null comment 'SKU',
has_options smallint default 0 not null comment 'Has Options',
required_options smallint unsigned default 0 not null comment 'Required Options',
created_at timestamp default current_timestamp() not null comment 'Creation Time',
updated_at timestamp default current_timestamp() not null on update current_timestamp() comment 'Update Time',
mv_product_id int unsigned null comment 'Megaventory Id',
mageworx_is_require smallint default 0 not null comment 'MageWorx Is Required',
is_replace_product_sku tinyint(1) default 0 not null comment 'Is Replace Product SKU',
created_by int null comment 'Created by Mass Product Import profiles id',
updated_by int null comment 'Created by Mass Product Import profiles id'
)
comment 'Catalog Product Table';
create index CATALOG_PRODUCT_ENTITY_ATTRIBUTE_SET_ID
on catalog_product_entity (attribute_set_id);
create index CATALOG_PRODUCT_ENTITY_SKU
on catalog_product_entity (sku);
-- auto-generated definition
create table catalog_product_website
(
product_id int unsigned not null comment 'Product ID',
website_id smallint unsigned not null comment 'Website ID',
primary key (product_id, website_id),
constraint CATALOG_PRODUCT_WEBSITE_WEBSITE_ID_STORE_WEBSITE_WEBSITE_ID
foreign key (website_id) references store_website (website_id)
on delete cascade,
constraint CAT_PRD_WS_PRD_ID_CAT_PRD_ENTT_ENTT_ID
foreign key (product_id) references catalog_product_entity (entity_id)
on delete cascade
)
comment 'Catalog Product To Website Linkage Table';
create index CATALOG_PRODUCT_WEBSITE_WEBSITE_ID
on catalog_product_website (website_id);
-- auto-generated definition
create table catalog_product_entity_datetime
(
value_id int auto_increment comment 'Value ID'
primary key,
attribute_id smallint unsigned default 0 not null comment 'Attribute ID',
store_id smallint unsigned default 0 not null comment 'Store ID',
entity_id int unsigned default 0 not null comment 'Entity ID',
value datetime null comment 'Value',
constraint CATALOG_PRODUCT_ENTITY_DATETIME_ENTITY_ID_ATTRIBUTE_ID_STORE_ID
unique (entity_id, attribute_id, store_id),
constraint CATALOG_PRODUCT_ENTITY_DATETIME_STORE_ID_STORE_STORE_ID
foreign key (store_id) references store (store_id)
on delete cascade,
constraint CAT_PRD_ENTT_DTIME_ATTR_ID_EAV_ATTR_ATTR_ID
foreign key (attribute_id) references eav_attribute (attribute_id)
on delete cascade,
constraint CAT_PRD_ENTT_DTIME_ENTT_ID_CAT_PRD_ENTT_ENTT_ID
foreign key (entity_id) references catalog_product_entity (entity_id)
on delete cascade
)
comment 'Catalog Product Datetime Attribute Backend Table';
create index CATALOG_PRODUCT_ENTITY_DATETIME_ATTRIBUTE_ID
on catalog_product_entity_datetime (attribute_id);
create index CATALOG_PRODUCT_ENTITY_DATETIME_STORE_ID
on catalog_product_entity_datetime (store_id);
Any ideas on how to speed it up is much appreciated

