By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
| version() |
|---|
| 5.7.35 |
| customer_id |
|---|
| 10 |
| 20 |
| 40 |
| 50 |
| 70 |
| 90 |
| customer_id | first_datetime |
|---|---|
| 10 | 2011-01-01 00:00:00 |
| 20 | 2012-01-01 00:00:00 |
| 40 | 2014-01-01 00:00:00 |
| 50 | 2015-01-01 00:00:00 |
| 70 | 2017-01-01 00:00:00 |
| 90 | 2019-01-01 00:00:00 |
| autoinc | store_id | customer_id | datetime | field_1 | field_2 |
|---|---|---|---|---|---|
| 1 | 100 | 10 | 2011-01-01 00:00:00 | aaa | bbb |
| 2 | 200 | 20 | 2012-01-01 00:00:00 | ccc | ddd |
| 4 | 400 | 40 | 2014-01-01 00:00:00 | ggg | hhh |
| 5 | 500 | 50 | 2015-01-01 00:00:00 | iii | jjj |
| 7 | 700 | 70 | 2017-01-01 00:00:00 | mmm | nnn |
| 9 | 900 | 90 | 2019-01-01 00:00:00 | qqq | rrr |
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | PRIMARY | store_customers | null | ALL | customer_id | null | null | null | 9 | 100.00 | null |
| 1 | PRIMARY | <derived2> | null | ref | <auto_key0> | <auto_key0> | 58 | db_1848834015.store_customers.customer_id,db_1848834015.store_customers.datetime | 2 | 100.00 | Using index |
| 2 | DERIVED | store_customers | null | ALL | customer_id | null | null | null | 9 | 100.00 | Using temporary; Using filesort |
| 2 | DERIVED | <derived3> | null | ref | <auto_key0> | <auto_key0> | 52 | db_1848834015.store_customers.customer_id | 2 | 100.00 | Using index |
| 3 | DERIVED | store_customers | null | index | null | customer_id | 52 | null | 9 | 100.00 | Using index |
| 4 | UNION | additional_customers | null | ALL | null | null | null | null | 1 | 100.00 | null |
| null | UNION RESULT | <union3,4> | null | ALL | null | null | null | null | null | null | Using temporary |
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | PRIMARY | store_customers | null | ALL | cust_date | null | null | null | 9 | 100.00 | null |
| 1 | PRIMARY | <derived2> | null | ref | <auto_key0> | <auto_key0> | 58 | db_1848834015.store_customers.customer_id,db_1848834015.store_customers.datetime | 2 | 100.00 | Using index |
| 2 | DERIVED | store_customers | null | index | cust_date | cust_date | 57 | null | 9 | 100.00 | Using index; Using temporary; Using filesort |
| 2 | DERIVED | <derived3> | null | ref | <auto_key0> | <auto_key0> | 52 | db_1848834015.store_customers.customer_id | 2 | 100.00 | Using index |
| 3 | DERIVED | store_customers | null | index | null | cust_date | 57 | null | 9 | 100.00 | Using index |
| 4 | UNION | additional_customers | null | ALL | null | null | null | null | 1 | 100.00 | null |
| null | UNION RESULT | <union3,4> | null | ALL | null | null | null | null | null | null | Using temporary |
| ANY_VALUE(sc.store_id) | customer_id | MIN(sc.datetime) | ANY_VALUE(sc.field_1) | ANY_VALUE(sc.field_2) |
|---|---|---|---|---|
| 100 | 10 | 2011-01-01 00:00:00 | aaa | bbb |
| 200 | 20 | 2012-01-01 00:00:00 | ccc | ddd |
| 400 | 40 | 2014-01-01 00:00:00 | ggg | hhh |
| 500 | 50 | 2015-01-01 00:00:00 | iii | jjj |
| 700 | 70 | 2017-01-01 00:00:00 | mmm | nnn |
| 900 | 90 | 2019-01-01 00:00:00 | qqq | rrr |