OTTAWA, July 6 (Reuters) - The value of Canadian energy exports surged in May even as the industry was coping with a major Alberta wildfire, but improved oil sales were not enough to prevent the second biggest trade deficit on record, Statistics Canada data showed on Wednesday.

The C$3.28 billion ($2.52 billion) deficit, the 21st in a row, was much larger than the C$2.70 billion shortfall predicted by analysts in a Reuters poll. The record high was the revised C$3.32 billion seen in April.

Exports fell by 0.7 percent as eight of 11 sub sectors declined, reflecting the Canadian economy’s continued challenges as it tries to adjust to the prolonged and complex effects of an oil price slump.

Yet even as wildfires in the energy-producing province of Alberta forced the shutdown of several oil sands projects, higher prices helped the value of energy exports rise by 7.1 percent, the highest month-on-month advance since May 2014. The value of exports of crude oil and crude bitumen surged 10.5 percent.

Statscan said early evidence indicated that Canadian refinery activity declined in May, freeing up crude oil supply for export. It said the remaining shortfall was largely met by a drawdown of Alberta inventories.

“It’s almost counterintuitive. It’s not what we were expecting to see at all,” said Peter Hall, chief economist at Export Development Canada.

Hall said a particular concern was a 21.0 percent drop in exports of unwrought precious metals and precious metal alloys, largely due to weaker demand in Britain and China. The sector is known for its volatility, he added.

Bank of Montreal chief economist Doug Porter said he was surprised by the weakness in non-energy exports, given the strong U.S. economy and a Canadian dollar that has slipped against its U.S. counterpart.

“I don’t think there’s a single reason to explain why exports have been disappointing,” he said.

Imports dropped by 0.8 percent, in part due to lower imports of aircraft and other transportation equipment and parts.

Exports to the United States, which accounted for 78.0 percent of Canada’s global total in May, rose by 3.6 percent while imports fell by 1.1 percent. As a result, Canada’s trade surplus with the U.S. widened to C$2.76 billion from C$1.31 billion in April.

The Bank of Canada, citing the wildfire impact, said last month that growth was likely flat or slightly negative in the second quarter. It sees an outsized third-quarter recovery. ($1=$1.30 Canadian)