Two years ago, undercover narcotics detectives arrested 24-year-old Tony Ramey and charged him with selling $140 worth of heroin from a sidewalk in the 1800 block of Seventh Street NW.

For Ramey as well as judges, attorneys and police involved in the case, it was the beginning of an odyssey through the city's overcrowded Superior Court. In two years after his arrest, attorneys and witnesses in the case made 17 separate court appearances before seven judges attempting to bring Ramey to trial on a downgraded misdemeanor charge of heroin possession.

There were continuances because judges and lawyers were in trial in other cases, because witnesses were unavailable and because Ramey, at least twice, failed to show up. Not until April of this year did the case go to trial before a jury, and Ramey was convicted and sentenced to six months in jail.

Not all cases take as long as Ramey's, but there are enough like it to keep the court mired in thousands of pending matters, costing taxpayers millions in fees for witnesses, jurors and lawyers each year and frustrating victims, attorneys and judges with recurring delays.

A wide variety of factors, including appellate court decisions expanding the rights of defendants, have contributed to the overload that burdens the D.C. Superior Court, making it difficult to single out any one as most responsible for the crush. But more than any other single el-ement, officials on all sides cite the city's war on drugs.

In the last two years, arrests for drug offenses in Washington have doubled, while arrests for many other types of crimes leveled off or declined. Of about 25,000 people arrested last year, more than 6,500--or 26 percent--were arrested for drugs.

One recent citywide drug sweep netted more than 200 suspects in a single day.

As a result of this policy, thousands of new drug cases have appeared on court dockets, many of them complicated by additional charges of larceny, burglary or robbery. The new cases, according to Superior Court judges, quickly wiped out the results of a crash program they had taken in 1980 to reduce their criminal misdemeanor case backlogs.

At the same time, the City Council revised Washington's 40-year-old drug statute, enabling federal prosecutors to bring felony charges with stiffer penalties against drug offenders. The U.S. Attorney's office, seeking tougher sentences, reacted by transferring hundreds of drug cases from U.S. District Court to the local Superior Court.

The focus on drug offenders has unleashed on the court a problem apart from the sheer number of new cases: drug users are more likely to miss court appearances than other defendants, more likely to be rearrested before their first cases come to trial and more likely to fail measures designed to treat them, authorities say.

In the first three months of this year, for instance, 28 percent of all adults on supervised probation in the city were rearrested. Of those rearrested, around 40 percent were identified as drug users.

A separate study by the Institute for Law and Social Research (INSLAW) found that during a five-year period between 1973 and 1978, 61 percent of defendants identified as drug users were rearrested four or more times, compared with 23 percent of defendants who did not use drugs.

"All of these folks on drugs present a problem of stability to start with," said Judge Fred B. Ugast, chief of the court's criminal division. "They can't even control themselves."

Users also commit twice as many bail and probation violations, meaning thousands of additional court hearings each year. Probation officers complain that they sometimes must appear in court as many as 15 times because of alleged violations by a single defendant before probation is revoked or the judge cuts short the probationary term in despair.

A new mandatory sentencing measure setting minimum prison terms for violent crimes and drug offenses is expected to require more trials and add up to 300 prisoners to corrections facilities in the next year.

Dealing with all these problems is complicated for the court here because the city's justice system consists not only of the traditional four separate elements--police, prosecutors, judges and corrections--but also two levels of authority: local and federal.

Most crimes in the District are prosecuted by members of the U.S. Attorney's office, which answers to the Justice Department, rather than city residents. Judges are appointed by the president and the court's budget is set by Congress. Police answer to the mayor, but the mayor's budget must be passed by Congress, which often imposes its own law enforcement priorities.

Any action by one branch has a rippling effect throughout the entire system. Yet officials in each branch maintain fierce control over their own decision-making and are reluctant to change their policies to accommodate others.

The citywide drug sweep earlier this month demonstrated graphically the conflicting priorities of different law enforcement agencies.

Judges, fearing mass arrests would exacerbate overcrowding at the D.C. Jail, tried to have the sweep called off. Prosecutors, saying their resources are already stretched too thin, threatened to not process more than 100 drug possession arrests. Mayor Barry, faced with a possible contempt citation from a federal judge because of jail overcrowding, at the last minute ordered police to proceed with the sweep in an effort to deal with drug trafficking on the streets.

Police officials acknowledge that their arrests have overcrowded the courts but say they must respond first to the public and its concerns that city streets aren't safe.

Prosecutors concede that their efforts to put more offenders behind bars increase the burden on the court and corrections but insist they must enforce the law.

"We don't generate our cases," said U.S. Attorney Stanley S. Harris. "They come to us."

"We can't control the police bringing in the criminals off the street," said Superior Court Chief Judge H. Carl Moultrie I. "Our job is to dispense justice when they come in."

Meanwhile, judges are sending record numbers of defendants to jail on high money bonds or without any bond. Prosecutors say that such pre-trial time in jail encourages defendants to plead guilty, relieving the court's caseload as well as their own.

But lengthy delays in the court, which keep defendants in jail longer, have raised questions among civil libertarians and law enforcement authorities as to whether judges are abusing their authority to set bail and whether defendants' constitutional rights to speedy trials are being violated.

A provision of the city's bail law enables judges to order dangerous defendants held without bond pending trial. That law, however, requires that trials be held within 90 days. Consequently it is rarely invoked, prosecutors and judges say, because they have too many other cases to meet the 90-day requirement.

"If we applied a speedy trial law to all our cases," said Judge Tim Murphy, "half of them would have to be dismissed."

While defense lawyers say some defendants' rights are being trampled by pre-trial delays, numerous appellate decisions expanding those rights have helped drag out the proceedings in drug and other cases by causing an explosion in the number of issues that must be decided before cases can be tried.

Those decisions concern evidence, statements to police, witness identifications, police searches, previous criminal records and jury selection procedures--all creating a mountain of paperwork for judges.

"That's why you get a petty larceny case that takes a week and a half to try," said Judge Ricardo M. Urbina. "It's not because the factual presentation takes that long. It's because you have to litigate the case twice: once on the constitutional issues and then on the merits of the case."Next: What can be done?