As many as one in five wells worldwide is at risk of running dry if groundwater levels drop by even a few meters, according to a new study appearing Thursday in the journal Science. Wells supply water for half the world’s irrigated agriculture, as well as drinking water to billions of people. But the aquifers that wells draw from have been imperiled in recent years as intense demand and lack of government management have allowed them to be drained. The scale of the threat has been difficult to calibrate, however, especially at a global level.

Researchers at the University of California, Santa Barbara produced their analysis by compiling construction records for almost 39 million groundwater wells in 40 countries, including their locations, depths, purposes, and construction dates. They found that between 6% and 20% are no more than 5 meters deeper than their local water tables, “suggesting that millions of wells are at risk of drying up if groundwater levels decline by only a few meters,” the authors wrote.

One solution when wells run dry is to dig deeper, but that often leads to poor water quality, according to the researchers. Well construction is also expensive, meaning that digging deeper isn’t always an option.

Global warming and sea level rise due to climate change are also contributing to the problem. “More severe multi-year to multi-decade droughts are projected in some regions,” said Debra Perrone, a co-lead author and an assistant professor with UCSB’s Environmental Studies Program, leading to greater groundwater demand and less replenishing of aquifers. Rising ocean levels, meanwhile, stand to inundate freshwater reserves, contaminating remaining supplies.

Increased flooding as a result of climate change, meanwhile, may actually be a boon to struggling aquifers. New research from Stanford University published Wednesday in the journal Science Advances suggests that stowing away flood water in California might be a way to recharge ground water.

Traditional storm runoff management employs surface-level reservoirs and damns, but newer methods for managing water supply adds infrastructure that helps funnel some of that underground. “Integrating managed aquifer recharge with floodwaters into already complex water management infrastructure offers many benefits,” said senior author David Freyberg, an associate professor of civil and environmental engineering at Stanford.

The authors said their research offers the first comprehensive analysis of where floods are likely to occur in the future in California, so the state can anticipate where to add to its existing infrastructure to aid groundwater recharging. The San Joaquin Valley, for instance, one of the most productive agriculture areas in the world, would be ripe for such infrastructure, the paper says. Its sits at the foot of the Sierra Nevada mountains and may face greater seasonal flooding.