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.
mysql
andig
source share