Kevin
Kevin

Reputation: 195

MySQL POP Last Row and Delete it

I have a queue system in MySQL, I wish to "POP" the last row and delete it at the same time. I have multiple scripts that would be selecting from the queue table, so it cannot retrieve the same row twice.

Is there a better command than SELECT then DELETING the ROW ID?

Upvotes: 0

Views: 1517

Answers (1)

Webeng
Webeng

Reputation: 7113

Simple answer: No :)

Explanation: You mentioned you need two actions to take place:

  1. Obtain the last row
  2. Delete the last row

That would require two queries to enter your database. the SELECT operator is the tool given to you to obtain any row you want. It of course does matter how you use the SELECT operator. If WHERE name = 'sam' is being executed on a column that is not indexed, then the speed of the query could suffer. The amount it would suffer depends on the size of your database of course and it might be negligable, however that is why the conditional at the end of your query usually is executed on an indexed field like the PRIMARY KEY for instance.

As for deleting the row, DELETE is the only tool you have, and I'm sure that even if there was another tool that it's efficiency wouldn't be better than using DELETE, since our dear friends that developed SQL would surely notice the enhanced efficiency and modify DELETE to be just as efficient.

Upvotes: 1

Related Questions