The gunman began his rampage by shooting an unarmed security guard outside a country-music bar in California, police said. After the attacker began firing on patrons inside, a sergeant with the sheriff’s office charged into the building to confront him and was cut down by gunfire.

The Ventura County mass shooting that left 12 dead Wednesday became a grim test case in a persistent debate about how places such as schools, nightclubs and houses of worship should steel themselves against shooters and how police should respond to them. The massacre, and others like it in recent months, show how difficult devising an effective strategy to head off an attack can be — and the exceptionally high cost it can exact on those on the front lines.

The debate has gained urgency during the past year, as President Trump and others have repeatedly said security guards — specifically armed ones — could have prevented the nation’s mass shootings; earlier this year, Trump tweeted his support for the controversial idea of arming teachers. At the same time, police departments across the country have adopted policies of aggressively confronting active shooters as the incidents unfold, with officers and SWAT teams hoping to limit the loss of civilian life by entering buildings as gunmen continue to shoot.

Both strategies have pitfalls. The security guard at Borderline Bar and Grill was among the gunman’s first targets, while an armed security guard and armed school resource officer were similarly ineffective at stopping the Pulse nightclub and Parkland High School mass shootings in Florida.

In addition to the death of veteran Sgt. Ron Helus of the Ventura County Sheriff’s Office on Wednesday, four police officers were shot while responding to a mass shooting at a Pittsburgh synagogue on Oct. 27, during which they exchanged gunfire with the assailant before taking a suspect into custody.

“The crux of the problem is gun violence, and we are all wrestling with that,” said Jason Villalba, a Republican member of the Texas House of Representatives who crafted a program for armed school marshals. “It’s important that we try to find solutions to these problems. It’s important to find a way to remove the politics.”

Trump most recently pushed the idea of placing armed security guards at vulnerable locations in comments after the Pittsburgh synagogue shooting that left 11 dead. “If there was an armed guard inside the temple, they would have been able to stop him,” Trump told reporters.

But several experts said there is little evidence to show armed security guards do much to curtail mass shootings. The Advanced Law Enforcement Rapid Response Training Center at Texas State University found that just 4 percent of 106 mass shootings it analyzed between 2000 and 2015 ended with the shooter being shot before police arrived on the scene.

“A very high percentage of these attackers are suicidal, so sometimes when they are shot and killed, that’s their desired outcome,” said Adam Lankford, a criminology professor at the University of Alabama. “The idea that he is going to be scared away by an armed security guard just doesn’t compute.”

Jaclyn Schildkraut, an associate professor of criminal justice at the State University of New York at Oswego, said taking down a shooter with a firearm is far more difficult than many realize. She pointed to a RAND Center of Quality Policing study that found New York City police officers hit their target in gunfights just 18 percent of the time.

But Villalba sees a place for armed guardians in schools — provided they have the right training. His Texas program, enacted in 2012, allows for school staff to have a gun under lock-and-key on campus, after they undergo 80 hours of training and a background check. The school marshal program has earned Trump’s praise.

“We are interested in providing a last line of defense when our children’s lives are threatened,” Villalba said.

Although police officers have taken a more aggressive stance toward active shooters, the Ventura County attack could prompt further evaluation of the tactic.

The dramatic shift in modern active-shooter response can be traced back to 47 bloody minutes inside Columbine High School in 1999. Nearly an hour passed between the opening burst of gunfire and when a SWAT team entered the school as victims bled and students made frantic calls for help.

In the chaos, a dispatcher had asked whether an officer could rescue a wounded teacher and was told the scene was not secure, the Denver Post reported. He was found dead by officers more than three hours after he was shot.

Those moments shook law enforcement and the FBI. First responders had been accustomed to dealing with hostages or demands and took a more distant, siege-like approach when responding to mass shootings.

But Columbine led police to try to blunt shooters from killing more victims or targeting those already wounded, said James Gagliano, a retired FBI supervisory special agent who served on the agency’s hostage rescue team.

In some cases, first responders were hesitant to mix with other agencies if they had not trained together, he said. Standardized training has helped shift the doctrine toward fast and violent responses.

“Now we get in formation, weapons drawn, and move to the sound of guns,” Gagliano said Thursday.

Active shootings typically last minutes before the shooter flees, is killed by police or commits suicide, he said, making a fast, deliberate response vital.

That has been reflected in modern police training, authorities noted after Wednesday’s killings. Helus, of the Ventura County Sheriff’s Office, joined a highway patrolman and entered the club in Thousand Oaks, Calif., minutes after authorities received the first 911 calls.

Once inside, Helus was raked by gunfire. The patrolman pulled him back and waited for backup.

Ventura County Sheriff Geoff Dean said he thinks the intervention probably saved lives inside, hailing Helus as a hero. Helus died in a hospital shortly afterward.

More than half of the nation’s mass shootings are underway when authorities arrive, the FBI said, and law enforcement has been forced to confront gunmen in most of those instances.

An expectation that officers push through the dangerous unknown appears to be a cultural norm. A school resource officer at Marjory Stoneman Douglas High School in Parkland, Fla., drew wide condemnation for hesitating to intervene as 17 people were killed inside the school in February.

And in 2016, officers in Orlando were criticized for not pursuing a gunman into the Pulse nightclub after he was holed up in a bathroom and shot more victims as police struggled to find a way inside.

But the dangers for law enforcement are clear. A third of encounters with active gunmen result in an officer getting shot, according to the FBI.

Gunmen often are willing to fight to the death, leaving officers with few options other than to pursue them with aggression “when the odds are not in your favor,” Gagliano said.

He said he is not aware of any officers or agents reviewing intervention strategies following the recent string of mass shootings.

“You certainly don’t want to send people into a meat grinder,” Gagliano said. “But you also know if you want to save lives, you have to breach the door.”