Say there's a table description as below:
| spend_daily_level | CREATE TABLE `spend_daily_level` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`active` tinyint(1) NOT NULL,
`created_time` datetime(6) NOT NULL,
`updated_time` datetime(6) NOT NULL,
`date` date NOT NULL,
`system_value` decimal(16,2) NOT NULL,
`checked_value` decimal(16,2) NOT NULL,
`account_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `spend_daily_level_date_account_id_f38b1186_uniq` (`date`,`account_id`),
KEY `spend_daily_level_account_id_f6df4f99_fk_account_id` (`account_id`),
CONSTRAINT `spend_daily_level_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `account` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1DEFAULT CHARSET=utf8 |
+---------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| active | tinyint(1) | NO | | NULL | |
| created_time | datetime(6) | NO | | NULL | |
| updated_time | datetime(6) | NO | | NULL | |
| date | date | NO | MUL | NULL | |
| system_value | decimal(16,2) | NO | | NULL | |
| checked_value | decimal(16,2) | NO | | NULL | |
| account_id | int(11) | NO | MUL | NULL | |
+---------------+---------------+------+-----+---------+----------------+
The data in this table is abtained from third party API and saved daily. I want to optimize this table query but partition seems to be not supported with foreign key.
My question is how can I do some optimization in this case as the amount of datas increasing daily?
And there're two main querys I would use:
1
SELECT `spend_daily_level`.`account_id`,
`spend_daily_level`.`sale_leader_id`,
SUM(`spend_daily_level`.`system_value`) AS `sum_value`
FROM `spend_daily_level`
WHERE `spend_daily_level`.`active` = True
AND EXTRACT(MONTH FROM `spend_daily_level`.`date`) = 7
AND `spend_daily_level`.`date` BETWEEN '2020-01-01' AND '2020-12-31'
GROUP BY `spend_daily_level`.`account_id`,
`spend_daily_level`.`sale_leader_id`
2
SELECT sale_leader_id,
SUM(s.`system_value`)
FROM `spend_daily_level` s
WHERE DATE = "2020-05-29"
GROUP BY sale_leader_id
Thanks
active,month,date]. You can even try to encodemonthandactiveinto a signle column to get more perfomance.activecolumn and changing date column to timestamp, but it doesn't must to give you much difference.AND EXTRACT(MONTH FROM `spend_daily_level`.`date`) = 7 AND `spend_daily_level`.`date` BETWEEN '2020-01-01' AND '2020-12-31'withAND `spend_daily_level`.`date` BETWEEN '2020-07-01' AND '2020-07-31'. Test the index by(active, date). Query #2.spend_daily_level_date_account_id_f38b1186_uniqis enough. You may try also the index by(date, sale_leader_id).