JDBC code performs an extended insert.
(We donβt see the actual SQL statement here, and we donβt see the binding of the first positional parameter in the SQL text, only the 2nd and 3rd parameters. I am going to make the assumption (possibly incorrect) that JDBC is null for an unbound parameter.)
Thus, the actual SQL statement sent by MySQL from the JDBC driver is:
INSERT INTO test.table_test (id,name, pid) VALUES (NULL,'name','pid') ,(NULL,'name','pid') ,(NULL,'name','pid') ,(NULL,'name','pid') ,...
In your MySQL procedure, you execute separate instructions:
INSERT INTO test.table_test (id,name,pid) VALUES (NULL,'name','pid'); INSERT INTO test.table_test (id,name,pid) VALUES (NULL,'name','pid'); INSERT INTO test.table_test (id,name,pid) VALUES (NULL,'name','pid'); INSERT INTO test.table_test (id,name,pid) VALUES (NULL,'name','pid');
An expanded form of insertion will be much more efficient than separate insertion instructions. (There is a lot of hard work that MySQL does to execute each statement.)
To get the best performance from a MySQL procedure, you need to do something like:
BEGIN SET @sqltext= 'insert into test.table_test (name,pid) values (''1'',''1'')'; SET @i=1; WHILE @i<200 DO SET @sqltext = CONCAT(@sqltext,',(''1'', ''1'')'); SET @ i=@i +1; END WHILE; PREPARE stmt FROM @sqltext; EXECUTE stmt; DEALLOCATE PREPARE stmt; END
And to get the same degraded performance from JDBC, you will need to execute separate INSERT statements.
This is the biggest contribution to the big difference in performance that you see.
This does not mean that the only difference is, but it explains most of this 17 second elapsed time that you see when performing your procedure. A procedure with a single extended insert is likely to be 100 times faster than running 200 individual inserts.
You still have a performance difference, but at least you'll be closer to comparing apples to apples.