I'm working on improving the data import performance of a web application, and one thing that has drastically improved performance, was porting database related logic from the server side code into stored procedures and functions in the database.
Also, when dealing with large amounts of similar queries, queuing them in batches to later be executed in a single SQL transaction, provided a noticeable decrease in the bottleneck of the import process.
Unfortunately I've come to a problem along the way, for some reason I cannot call a stored procedure more than once in a single SQL command.
Take this SQL where I make two consecutive calls to LegacyUpdateProduct
to showcase my problem.
SQL
The stored procedure accepts a few parameters with data, does not return a result set, and out gives the @ID
variable with the row ID, which in this case we have no need for anyway.
CALL LegacyUpdateProduct(92,'00010','ADAISD 345',18.81,'91020200','FSB92','GS8219','FSB92',TRUE,2,3,0,0,@ID);CALL LegacyUpdateProduct(277,'0002335600','TPASD 987',6.04,'0 002 335 ','UX79','','',TRUE,2,3,0,0,@ID);
PHP
In PHP anyone would expect to simply run the query with the two calls to LegacyUpdateProduct
and that everything would be alright.
$sql = "CALL LegacyUpdateProduct(92,'00010','ADAISD 345',18.81,'91020200','FSB92','GS8219','FSB92',TRUE,2,3,0,0,@ID);CALL LegacyUpdateProduct(277,'0002335600','TPASD 987',6.04,'0 002 335 ','UX79','','',TRUE,2,3,0,0,@ID);"
# With Code Igniter
$this->db->query($sql);
# Or with raw PHP
mysqli_query($this->db->conn_id, $sql);
Sadly that isn't the case. Running this query with Code Igniter or with plain PHP throws a SQL error.
Note: I went through the data with a fine-tooth comb, and made use of Code Igniter DB escape mechanism to produce safer queries.
Error Number: 1064 You have an error in your SQL syntax
Most interesting is that when I copy and paste the allegedly erroneous SQL in PHPMyAdmin or MySQL Workbench, it runs smoothly without errors or warnings.
Actually, I was able to run up to fifty consecutive calls to LegacyUpdateProduct
in a single query without a problem in PHPMyAdmin and MySQL Workbench.
I've looked through Google and SO for questions that might be related or helpful in someway, but haven't found much to go on, except that this might be a Code Igniter bug of some sorts, but even with plain PHP I found the same problem.
In an answer to another SO question that is somewhat related, it mentions this might be a Code Igniter bug, however it doesn't explain why with plain PHP mysqli_query()
this query fails.
In the comments of another question, for a different error, hints that there might be a concurrency problem, were sequential calls to the procedure cannot be made without waiting for completion of the previous. I do not have enough understanding of MySQL concurrency to make a claim but, shouldn't each command in a query be executed after the previous.
Help?