Insert batch data into database

1. The normal operation is to directly insert in batches at once. In this case, if the amount of data is extremely large, it will exceed the maximum data packet limit of the database.

2. Inserting one item at a time in a loop will keep the disk IO at a high level, which is not advisable.

3. Split and loop insertion for large amounts of data is most preferable. Generally, 1,000 pieces of data are divided into one segment.

 public static void main(String[] args) {
        ArrayList<Object> objects = new ArrayList<>();
        for(int i=0;i<1000000;i++){
            objects.add(i);
            if(objects.size()%1000==0){
                System.out.println(objects.toString());
                objects.clear();
            }
        }
        if(objects.size()>0){
            System.out.println(objects.toString());
        }

    }

Guess you like

Origin blog.csdn.net/lxctxx/article/details/131783215