I don’t read the books, so this is pure speculation:
I believe there were two plots, and that the way they came together inadvertently killed Joffrey before he was supposed to die.
PLOT 1: Olenna and Margaery know that Joffrey was unmanageable (and perfectly capable of killing either of them at any time). They plotted to kill Joffrey–on a delayed basis. The poison wouldn’t have struck until at least one night had gone by–enabling Margaery to announce that the marriage had been consummated, and that she was pregnant with the new heir. (If she really hadn’t gotten pregnant by Joffrey on their wedding night she could have accomplished that feat in another way by another man.)
The reason for the rigmarole of the “bead from Sansa’s necklace” was basically insurance: if someone suspected Joffrey had been poisoned, it could be mentioned that Sansa had been seen at the wedding feast with a bead missing. Blame could thus be shifted to Sansa. Margaery would still be Queen Regent (for her unborn child. Perhaps the plotting would have extended to making sure that baby was a son, with a male infant ready to be substituted for any inconvenient girl baby. The Tyrells would have been in charge of the palace and could handle these matters discreetly.
Court fool Ser Dontos, who gave Sansa the necklace, was part of this, of course. His asking Sansa to leave was intended to provide yet another “evidence” that Sansa had been the poisoner; people would remember that she left the wedding feast early and would see that as “proof” of her guilt
But it all went wrong because of…
PLOT 2: This plot’s author is less clear, but my top candidate would be Oberyn: this plot was intended to make hundreds of wedding guests ill (but not dead) and thus create a major embarrassment for the Lannisters. This plot was: poisoned pie.
The shot of the dead doves was ambiguous: yes, they could have been killed by Joffrey’s sword…but there was suspiciously little blood. I think that the doves who didn’t eat the pie, or not much of it, flew off, but a few who ate a lot of the pie…died. The pie was intended to induce mass illness in the guests (which of course would kill the much-smaller-than-humans birds).
But what was completely unexpected was the effect of both drinking the bead-poisoned wine and eating from the pie (and did we see anyone else eat from the pie?)
Joffrey consumed both and the effect of the combination killed him immediately, rather than in a few days as planned. The timing of his death is unfortunate for the Tyrells, though no doubt they’ll try “Margaery should marry Tommen” (straight out of English history, of course, with Catharine of Aragon going from Prince Arthur to Prince Henry, future Henry VIII) as a Plan B.