The Soviet debacle in Afghanistan

After the United States faced defeat in the Vietnam War it seemed like both Cold War superpowers learned their lesson - do not interfere in unnecessary conflicts, especially when those are a civil war. However, in late 1979 the USSR repeated the American mistake, leading to the Soviet debacle in Afghanistan that would end in a defeat nearly a decade later. What went wrong and why is it sometimes considered a greater failure than Vietnam?