Current code uses:
This counts the number of mediums that a recording appears on, and also doesn't check for distinct mediums. This ends up being wrong for e.g. http://musicbrainz.org/recording/b6293447-05bf-4123-bfe5-099754348bbf, which appears on http://musicbrainz.org/release/b7fadf15-8121-4f46-b10d-a33b6ec35858 as both track 1 and track 7, or any release where a recording appears on two different mediums within the same release.
Also, the UNION there is more inefficient than it needs to be.