High-performance batch insert and batch Delete SQL statement writing

Source: Internet
Author: User
The technical level can always be improved in context and nitpicking. If you never "find fault", you may not know if (str! = "") Not as good as if (str! = String. empty) efficient, batch insert, and delete SQL statements are the fastest to execute in that way. The difference between interfaces and abstract classes is not only the language level, but also the original permission management is designed in this way. responsibilities are simpler and easier to expand ......

Originally, the first two articles used the cnblogs editing control. When I saw a friend asking about batch insertion and batch deletion, I decided to sort out the documents and share them with you.

We will only discuss how to write common SQL statements more efficiently without considering the special import methods such as file transfer. After all, it is more convenient to call SQL statements or stored procedures in code.

Batch deletion is simple, and you may have used it:

Delete from TestTable where id in (1, 3, 54, 68) -- run through

When you choose to delete multiple objects in a non-consecutive manner on the interface, this statement is more efficient than multiple methods such as multiple deletions in a loop call or one call of the intermediate bonus points in multiple delete statements.

This article focuses on writing batch inserts:

SQL statement:

Insert into TestTable SELECT 1, 'abc' union select 2, 'bcd' union select 3, 'cde' -- TestTable table has no primary key, and ID is not the primary key

Oracle statement:

Insert into TestTable SELECT 1, 'abc' From daul union select 2, 'bcd' From daul -- TestTable table has no primary key and ID is not the primary key

Once tested, inserting 1000 pieces of data in this way is much more efficient than simply adding one call to 1000 insert or 1000 insert statements in a loop call, which is about 20 times faster (the debugging status is not accurate ). In fact, it was very easy to use a union (union all can also be used), but it was still very pleasant to get the test results.

To get this result, two conditions are required:

1. the table cannot have a primary key or the primary key is the default database (SQL uses auto-incrementing columns and oracle uses sequences)

2. When you combine SQL statements, you can only connect them with strings. You cannot use parameterized SQL statements (that is, @ parm is used as a placeholder in the combined SQL statement and Parameter is added to the Command object)

If either of the above two statements is not met, the efficiency improvement is not obvious.

In addition, the maximum length of an SQL statement is limited. If thousands of pieces of data are combined into an insert statement, the maximum length may be exceeded. Therefore, if there are 5000 pieces of data, you can insert 1000 pieces at a time, write it five times (not necessarily 1000 times at a time. You can study it if you are interested ).

 

Statement: After Reading comments from several friends, I found that I did not clearly describe them.

The above two conditions do not mean this is good, but they are suitable for the union statement when the Application Scenario meets these two conditions.

At that time, I used this method to solve the problem of batch import of mobile phone numbers to a WinForm program. The program was used by myself, and the excel file was also reliable, so I do not need to consider SQL attacks. There are also some scenarios where a large amount of data comes from dedicated terminals connected through the Gprs network. In short, the data source is reliable, and the data volume is large but not huge.

 

 

PS: I often encounter some tips or small details that are worth noting. I don't remember them until the next meeting. Now I am determined to develop good work habits. Leave this article to prove it.

What can be written into documents must be written in the project team development specifications. Others must be recorded at least.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.