shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

How to improve efficiency of batch insert ?

Open piggytough opened this issue 6 years ago • 3 comments

My version : Sharding-JDBC 4.1.1

sql like this: insert into pressure(userid,orderid) values(?, ?),(?,?),... insert data size is 5000, and i've used prepare statement.

when NOT using sharding, time consumes about 40ms, but sharding takes about 400ms.

Sharding tables configuration: image

Analysis by arthas, I can see that time mostly cost on function org.apache.shardingsphere.sharding.rewrite.token.generator.impl.keygen.GeneratedKeyInsertValuesTokenGenerator:isToAddDerivedLiteralExpression. See below: batchinsert

Is there anything I can do to improve efficiency?

piggytough avatar Oct 16 '20 08:10 piggytough

It seems isToAddDerivedLiteralExpression method will call getGroupedParameters for an empty check every time. So we can make two improvement choices:

  • cache the GroupedParameters for one sql and will called 5000 times in multiple values
  • add a new check empty method and avoid new/merge parameters
    private boolean isToAddDerivedLiteralExpression(final InsertStatementContext insertStatementContext, final int insertValueCount) {
        return insertStatementContext.getGroupedParameters().get(insertValueCount).isEmpty();
    }

    public List<List<Object>> getGroupedParameters() {
        List<List<Object>> result = new LinkedList<>();
        for (InsertValueContext each : insertValueContexts) {
            result.add(each.getParameters());
        }
        if (null != insertSelectContext) {
            result.add(insertSelectContext.getParameters());
        }
        return result;
    }

kimmking avatar Oct 16 '20 10:10 kimmking

there has been fixed in master branch, can you try it with master branch 5.0.0. https://github.com/apache/shardingsphere/pull/7816

kimmking avatar Oct 16 '20 13:10 kimmking

there has been fixed in master branch, can you try it with master branch 5.0.0. #7816

sure!

piggytough avatar Oct 17 '20 01:10 piggytough