Don't believe it, you might not even be able to delete MySQL!

Get technical dry goods and industry information for the first time!

Don't believe it, you might not even be able to delete MySQL!

This topic is a bit exaggerated, but it is actually a very real problem. You will add, delete, modify, and check, is it possible to use MySQL?

The question I want to talk about today is, if you know MySQL's Delete syntax, does it mean that you will delete data if you can write a delete statement? Let's look at an example first.

Assume that there is a table and the first 10,000 pieces of data need to be deleted. There are the following three SQL statements, which one would you choose? why?

Don't believe it, you might not even be able to delete MySQL!

It is estimated that many people will choose option 1. Because, what can be done with a SQL statement, why bother to disassemble it? And that's how it is usually used.

The problem is that in Option 1, you are locally, and there is nothing wrong with using the test environment. But if you use it in a production environment, think about it, is it a long transaction, a big transaction. Will it block other operations? Will it cause the system to crash?

Although it seems that the SQL statement is correct. Simple, effective, and rude, but the transaction is relatively long, the lock time will be longer, which will cause other clients to wait for resources for a long time. To be more serious, this operation may cause many other operations to time out, and then expand the damage. A similar incident happened once in our e-commerce system, and the customer service phone was suddenly crowded!

Let's talk about scheme 2, delete 500 data each time, it seems that the realization is more complicated, but it is safe and reliable. In serialized execution, a relatively long transaction is divided into multiple relatively short transactions. Each transaction takes up a relatively short lock time, and other clients wait for corresponding resources for a relatively short time. Such an operation also means that resources are fragmented (using different fragments of resources for each execution), which can improve concurrency.

Option 3, open a bunch of threads. People create lock competition for themselves, which intensifies concurrency. Multiple transactions will cause lock competition on the same row, consuming cpu resources. The CPU may spike high in an instant.

So, don't stubbornly think that the delete statement is simple. As for the above three schemes, which scheme to choose should be combined with the actual scenario, and comprehensively consider various factors, such as the size of the table, the amount of concurrency, and the degree of business dependence on this table.

In addition, the above three delete statements are not common to us. And usually you should not write in this way. To delete data, you must find out the id first, and then delete it according to the id.

Don't believe it, you might not even be able to delete MySQL!
This question was asked during the interview: "Suppose there is a table now, and the first 10,000 pieces of data need to be deleted?" Don't just take out option 1 directly. Then you are dead. We had such interview questions last year, and many people fell into the pit!
In fact, the most important thing in programming is thinking and thinking, not writing code! Do you agree? Please leave a message. Finally, make a small advertisement. If you want to buy geek time learning courses, please add my WeChat account: xttblog, all courses have cash back!

Guess you like

Origin blog.51cto.com/15127565/2666110