We study composition operators on the Hardy space
H
2
\mathcal {H}^2
of Dirichlet series with square summable coefficients. Our main result is a necessary condition, in terms of a Nevanlinna-type counting function, for a certain class of composition operators to be compact on
H
2
\mathcal {H}^2
. To do that we extend our notions to a Hardy space
H
Λ
2
\mathcal {H}_{\Lambda }^2
of generalized Dirichlet series, induced in a natural way by a sequence of Beurling’s primes.