The government’s sprawling system of background checks and security clearances is so unreliable it’s virtually impossible to adequately investigate the nearly 5 million Americans who have them and make sure they can be trusted with access to military and sensitive civilian buildings, an Associated Press review found.

Case after case has exposed problems for years, including recent instances when workers approved by the government have been implicated in mass shootings, espionage and damaging disclosures of national secrets.

In the latest violence, the Navy Yard gunman passed at least two background checks and kept his military security clearance despite red flags about violent incidents and psychological problems.

The review — based on interviews, documents and other data — found the government overwhelmed with the task of investigating the lives of so many prospective employees and federal contractors and then periodically re-examining them.

The system focuses on identifying applicants who could be blackmailed or persuaded to sell national secrets, not commit acts of violence.

And it relies on incomplete databases and a network of private vetting companies that earn hundreds of millions of dollars to perform checks but whose investigators are sometimes criminally prosecuted themselves for lying about background interviews that never occurred.

“It’s too many people to keep track of with the resources that they have, and too many people have access to information,” said Mark Riley, a Maryland lawyer who represents people who have been denied clearances or had them revoked.

The Pentagon knows there are problems. Defense Secretary Chuck Hagel has ordered a sweeping review of all military security and employee screening programs. “Something went wrong,” he said.

Separately, Congress has asked the inspector general at the U.S. Office of Personnel Management to investigate how a clearance was awarded to Aaron Alexis, the Navy IT contractor who killed 12 people Monday inside a Washington Navy Yard building before he died.

Just weeks ago, the Navy had warned employees under its new “insider threat” program that all personnel were responsible for reporting suspicious activity that could lead to terrorism, espionage or “kinetic actions” — a military euphemism for violence.

The Navy Yard itself reopened Thursday, but it was hardly business as usual. Workers who streamed by the red brick wall of the Navy Yard in the early morning sun said it was too soon to talk about the week’s violence.

FBI Director James Comey said investigators were still working through video evidence, but fresh details of the shootings were emerging.

Comey said Alexis entered the Navy Yard in a vehicle, parked in a deck across from Building 197, entered carrying a bag, went into a fourth-floor bathroom and came out carrying a Remington 870 shotgun. The shotgun was cut down at both ends — the stock sawed off and the barrel sawed off a bit — and ammunition was stowed in a cargo pocket on the outside of his pants.

Almost immediately Alexis started to shoot people on the fourth floor with no discernible pattern, Comey said. Alexis also went down to the lobby, shot a security guard and took the guard’s handgun, continuing his shooting until he was cornered later by a team of officers and killed after a sustained gunfire exchange.

“It appears to me that he was wandering the halls and hunting for people to shoot,” said Comey.

Alexis had worked for a Florida-based IT consulting firm called The Experts. He had been refreshing Pentagon computer systems, holding a military security clearance that would have expired five years from now.

Alexis’ employer said it had had no personnel problems with him and two separate background checks revealed only a traffic violation. But there were signs of trouble below the surface, and public records databases used in those kinds of searches can be spotty repositories of arrest records, court dockets and other information.

“The only thing that the security-clearance process is intended to protect is the security of the United States,” said Shlomo Katz, a government contracts lawyer who has been issued a clearance himself and is an expert on the process. “The system is not designed to protect the lives of our co-workers, and therefore I don’t view it as a failure of the system.”