I had been working on "Preorder tree traversal algorithm" as a part of my daily task. What I came across was that for MySQL, we need to lock a table and after insertion or deletion of any entry, we need to unlock the particular table (I succeeded in my task开发者_如何学编程 though).... I just wonder why we do this? Apart from this case, where else do you think this can be used in case its frequently in use ?
You need to lock a table if you don't want its contents to change while you are doing whatever you are.
So in your case, I assume that you would not want the table to change while you do your traversal (I assume your table data forms some sort of tree).
So one option you have is: LOCK the table, COPY its contents to another table, UNLOCK it, and operate on the copy.
If your original table is small enough, you can even make your temp table in-memory.
Beware: locking tables can cause other scripts that use the table to wait! Be sure that your operations will not take a lot of time.
精彩评论