Problem with complex queries after migrating from MySQL 5.1 to MariaDB 10.4
I would suggest getting rid of the single-column indexes for device and person, and replace them with:
INDEX device_person_index (deviceid, person_id)

If you have queries with predicates for person_id but not deviceid, you can add another index for that:
INDEX device_person_index (person_id, deviceid)

The above suggestion holds for both tables. I find KEY in this context very confusing, so I replaced it with INDEX which I beleive is a synonym.
I don't know whether MySQL/Mariadb can do index anding, but a composite index is anyhow more efficient than two separate indexes.