I have a query like this
select org, `Balance` from oss_collection_history och2
where och2.`DateFrom` =
(select min(`DateFrom`) from oss_collection_history och3 where och3.org = och2.org)
It works, but with my data set it takes about 4 seconds.
The explain output looks like this
id|select_type |table|partitions|type|possible_keys |key|key_len|ref |rows|filtered|Extra | --|------------------|-----|----------|----|----------------------------------|---|-------|------------|----|--------|-----------| 1|PRIMARY |och2 | |ALL | | | | |6651| 100|Using where| 2|DEPENDENT SUBQUERY|och3 | |ref |Org,oss_collection_history_Org_IDX|Org|22 |ca1.och2.Org| 443| 100|Using index|
I tried adding the following indeices, but doesn't appear to improve the performance
CREATE INDEX `oss_collection_history_Charges_IDX3`
ON oss_collection_history (`DateFrom`, `org`);
CREATE INDEX `oss_collection_history_Charges_IDX4`
ON oss_collection_history (`DateFrom`);
CREATE INDEX `oss_collection_history_Charges_IDX6`
ON oss_collection_history (`DateFrom`, `org`, `Balance`);