I first looped through all the river entries and collected their ID's. Thanks to this it's possible to do only one DB query instead of one query per each found river entry.