Я испытываю очень медленный тест загрузки из этого запроса MySQL с некоторыми WHERE
и HAVING
условиями:
SELECT * FROM test p
WHERE p.brandid=636
AND DATEDIFF(p.registration, '2019-01-01') >= 0
AND DATEDIFF(p.registration, '2019-05-22') <= 0
GROUP BY p.invoice_num
HAVING (p.net_amount BETWEEN 0 AND 1000)
AND (p.profit_amount BETWEEN 0 AND 1000)
AND (p.cost_amount BETWEEN 0 AND 1000)
ORDER BY p.registration DESC LIMIT 100 OFFSET 0
ниже CREATE STMT:
CREATE TABLE `test` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`original_id` bigint(11) DEFAULT NULL,
`invoice_num` bigint(11) NOT NULL,
`registration` timestamp NULL DEFAULT NULL,
`paid_amount` decimal(10,6) DEFAULT NULL,
`cost_amount` decimal(10,6) DEFAULT NULL,
`profit_amount` decimal(10,6) DEFAULT NULL,
`net_amount` decimal(10,6) DEFAULT NULL,
`customer_id` bigint(11) DEFAULT NULL,
`recipient_id` text,
`cashier_name` text,
`sales_type` text,
`sales_status` text,
`sales_location` text,
`invoice_duration` text,
`store_id` double DEFAULT NULL,
`is_cash` int(11) DEFAULT NULL,
`is_card` int(11) DEFAULT NULL,
`brandid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`,`invoice_num`),
KEY `idx_registration_compound` (`id`,`registration`),
KEY `invoiceNum_idx` (`invoice_num`)
) ENGINE=InnoDB AUTO_INCREMENT=47420958 DEFAULT CHARSET=latin1;
Я добавил составной индекс между полями id
и registration
. Но я думаю, что я мог бы улучшить его с помощью более правильной комбинации индексов.
ниже ОБЪЯСНЕНИЯ:
Является ли TIMEDIFF () проблемой для производительности?
Есть идеи, как улучшить тест?