How to optimize a dependent subquery with a constant expression?

I have the following request and plan:

SELECT data.* FROM data WHERE channel_id = 1 AND timestamp >= IFNULL(( SELECT UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(MAX(timestamp) / 1000, "%Y-%m-%d"), INTERVAL 1 day)) * 1000 FROM aggregate WHERE type = '3' AND aggregate.channel_id = data.channel_id ), 0) AND timestamp < UNIX_TIMESTAMP(DATE_FORMAT(NOW(), "%Y-%m-%d")) * 1000 id select_type table partitions type possible_keys key key_len ref rows filtered Extra '1' 'PRIMARY' 'data' NULL 'ref' 'data_unique IDX_ADF3F36372F5A1AA' 'IDX_ADF3F36372F5A1AA' '5' 'const' '860512' '11.11' 'Using where' '2' 'DEPENDENT SUBQUERY' 'aggregate' NULL 'ref' 'aggregate_unique IDX_B77949FF72F5A1AA' 'aggregate_unique' '7' 'volkszaehler.data.channel_id const' '1473' '100.00' 'Using index' 

The data table has a couple of million rows, all indexes are indexed:

 data: `channel_id`, `timestamp` aggregate: `type`, `channel_id`, `timestamp` 

The request becomes fast when aggregate.channel_id = data.channel_id is replaced with the actual channel_id value of the external request. And the dependent subquery becomes a simple subquery.

However, I would prefer not to do this to allow the request to work s> 1 channel_ids at a time.

Why doesn't MySQL (5.7 homebrew) recognize that this subquery is really independent (or not?) And how can it be optimized?

I already checked that deleting the IFNULL function or clicking on it does not IFNULL problem. I also failed to push the subquery to a different level, as suggested in Can I get mysql to execute the subquery first? since the link data.channel_id no longer known.

0
mysql
source share

No one has answered this question yet.

See similar questions:

6
Can I get mysql to execute a subquery first?

or similar:

2776
How can I prevent SQL injection in PHP?
3
Optimization ORDER BY
3
MySQL optimizes counting group selection by
2
MySQL ignores my index
one
How can I optimize my mysql query?
one
MySQL - slow query while saving view
0
SQLSTATE [23000]: Integrity constraint violation: 1062 Duplicate entry 'xxxx' for key 'group_key'
0
Any specific question for optimization? Otherwise, I'm going to install ElasticSearch
0
Mysql slow query optimization
0
Optimization of this MySQL query with a subquery inside

All Articles