Error in batch warehousing: prepared statement contains too many placeholders

When I was doing batch storage of Mysql recently, when I executed the sentence PreparedStatment.executeBatch(), I got an error: prepared statement contains too many placeholders, indicating that there are too many placeholders~, but I found that the tables I put into the database are only 40+ A field, it shouldn't be, but...

 

When entering the database in batches, Mysql will assemble the insert statement into the following format:

insert into tablename(field1, field2, field3, ..., field45) values(:field1, :field2, :field3, ..., :field45), (:field1, :field2, :field3, ……, :field45), (:field1, :field2, :field3, ……, :field45), ……, (:field1, :field2, :field3, … …, :field 45)

 For example, the objects I batch-stored are 3000+ records. Although I encapsulate it into a List collection, each element in the collection has 45 keys (one key corresponds to one field). After sql is assembled into the above format, the number of placeholders reaches 3000*45=135000, which is greater than the maximum limit of 65535 for placeholders by MySQL.

Therefore, it should be noted that when batch storage, the number of placeholders is no longer 45, but the product of 45 and the number of items.

 

Lesson: Try to divide the List into batches, and then batch each batch separately to ensure that the number of placeholders generated in each batch is less than the maximum limit of 65535 in MySQL.

Under normal circumstances, this kind of error is easy to appear when entering the warehouse in batches, but it often occurs in the case of large batches of data, so pay more attention.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326929395&siteId=291194637