Punishment is a key incentive to maintain cooperation. But it is costly, hence its effectiveness is under permanent debate. To overcome this difficulty, we here introduce a sampling punishment method and explore its consequences on public goods game and collective-risk social dilemma. In our model, just a small portion of participants are sampled and defectors are punished only if their portion exceeds a certain threshold. We show that the cooperation level can be effectively improved under low punishment threshold and small sample size when the punishment intensity is considerable. To identify the optimal conditions of sampling punishment, we monitor the punishment probability and related cost. We find that the adoption of a low punishment threshold and a small sample size under a considerable punishment intensity is the best, which always leads to a high cooperation level with a low related cost. Our observations are both valid in these two games.