Does Mysqli_multi_query improve performance - php

I am updating mysql data through php script. I am looking to use mysqli_multi_query() instead of mysql_query. I have N number of update queries to execute. Please suggest if multi query will help in better execution time.
A. Updating data using mysql_query(), Firing single single queries N times.
B. Concatinating All Update queries with ";" and firing once using multi query.
Please Suggest if Technique "B" will help in performance.
Thanks.

C. Use prepared statements and if possible (InnoDB) within a transaction.
A multiquery will save you some client-server roundups, but the queries are still executed one by one.
If you use prepared statement together with transactions, the query is checked once by the parser after that just values are pasted to server. Transaction prevents indexes being rebuild after each update.
Multiple insert statements can be rewritten as a single bulk insert statement:
INSERT INTO t1 (c1,c2,c3) VALUES (1,2,3), (3,4,5) --etc

Related

pg_prepare: cannot insert multiple commands into a prepared statement

I have 2 tables, TableA and TableB. TableB has a fk field pointing to TableA.
I need to use a DELETE statement for TableA, and when a record in it is deleted I need to delete all records in TableB related to that record in TableA. Pretty basic.
begin;
DELETE FROM TableB
WHERE nu_fornecedor = $1;
DELETE FROM TableA
WHERE nu_fornecedor = $1;
commit;
This string is passed to pg_prepare(), but then I get error
ERROR: cannot insert multiple commands into a prepared statement
Ok, but I need to run both commands in the same transaction, I cant execute 2 separated statements. I tried to use with without begin-commit and got same error.
Any idea how to do it?
To understand what is going on and your options, let me explain what a prepared statement is and what it is not. You can use pg_prepare, but only for the statements individually, not for the transaction as a whole.
A prepared statement is a statement handed to PostgreSQL which is then parsed for and stored as a parse tree for future use. On first execution, the parse tree is planned with the inputs provided, and executed, and the plan cached for future use. Usually it makes little sense to use prepared statements unless you want to reuse the query plan (i.e. executing a bunch of otherwise identical update statements hitting roughly the same number of rows), all in the same transaction.
If you want something that gives you the benefits of separating parameters from parse trees but does not cache plans, see pg_query_param() in the PHP documentation. That is probably what you want.

sql insert- 1 big query, or many single queries

I am using PHP with an odbc connection to MSSQL database.
Currently, I having around 1900 insert statements, in the one string, separated by a semicolon, and running that in 1 odbc_execute statement.
Firstly, is this a bad method? Should I be processing every insert statement separately in a for loop?
Also, the way I am currently doing it, with 1 big statement, for some reason, only a maximum of 483 rows are being inserted with no errors. If I copy the statement that is run and run this through SQL studio, all rows insert, yet every single time, only a maximum of 483 rows insert.
Any ideas why this could be?
One network round trip per INSERT will mean a lot of latency. It'll be very slow.
There's probably a limit on the buffer size of all those concatenated SQL statements.
I think you want to use a prepared statement and bound variables:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms712553(v=vs.85).aspx
You can still process it in a loop - two, actually. You'll want an inner loop to add INSERTs to a batch that's executed as a single transaction, and an outer loop to process all the necessary batches.
As long as you aren't running a separate transaction for each insert, there's nothing wrong with inserting one at a time.
For this sort of 'batch insert' I would typically run a transaction for every 100 rows or so.
I would avoid trying to cram them all in at once. nothing really to be gained by doing that.
You may put the data in a file (xml?) on the sql-server and request a stored procedure from php that process it.
regards,
/t

2 Mysql queries in 1 variable, help?

$qry1 = "DELETE FROM
si_topics
WHERE
topic_id = '". $_GET['topic'] ."'
DELETE FROM
si_posts
WHERE
topic_id = '". $_GET['topic'] ."'";
$mysqlqry1 = mysql_query($qry1);
if($mysqlqry1){
echo 'Topic deleted from database!';
} else {
echo 'Mysql query failed!';
}
This gives the message Mysql query failed!, why?
Greetings
You are missing a semicolon between your two delete statements.
Also, multiple queries are not supported in mysql_query.
From the documentation:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.`
You can not run multiple queries in mysql_query() function in once.
Try mysqli::multi_query instead to run multiple queries. queries can be separated by ;
Quoting from the manual (my emphasis)
mysql_query() sends a unique query (multiple queries are not supported)
If you want to execute several SQL statements in a single query, you need to use mysqli and multi-query, and each statement needs to be separated by a semi-colon (;)
Because:
you're sending one query with invalid syntax, and
even if you'd added the semicolon between queries (how else is the MySQL server supposed to know where your second query starts?!?), PHP's old library doesn't support that here.
Use mysqli.multi_query() instead.
You should also either use prepared statements or at the very least sanitise your inputs, as your code has a huge SQL Injection vulnerability (how are people still writing PHP like this?!? what are they teaching in these "schools"?! aghh!)
PHP does not allow running 2 queries in one go.
You need to split your queries in 2 variables and run them one by one.
http://php.net/manual/en/function.mysql-query.php
The documentation explains:
mysql_query() sends a unique query
(multiple queries are not supported)
to the currently active database on
the server that's associated with the
specified link_identifier.
The standard MySQL separator semicolon ( ; ) will not work here.
If you wish to specify multiple delete conditions across several tables, then you can use JOIN syntax for the DELETE statement, however this might be inefficient - but that way, the query will fit within mysql_query();
Someone already mentioned mysqli and multi_query function, thumbs up for that :)
On the other hand, your code is extremely prone to SQL injection attacks, always clean your $_POST and $_GET input before using it in the query directly!
Add mysql_error ($link) after 'echo 'Mysql query failed!';' line
That will explain the problem.

Is there any way to send more than one postgresql sql prepared statement in one call in php?

Is there any way to execute more sql prepared statements at once? Or at least use something to achieve this result, can it be emulated with transactions?
pg_send_query can execute more statements (from php docs "The SQL statement or statements to be executed.")
but
pg_send_execute and pg_send_prepare can work only with one statement.
The query parameter has the following description
"The parameterized SQL statement. Must contain only a single statement. (multiple statements separated by semi-colons are not allowed.) If any parameters are used, they are referred to as $1, $2, etc."
from http://www.php.net/manual/en/function.pg-send-prepare.php
Is there any way to send more statements at once to make less roundtrips between php and postgresql like the pg_send_query does?
I don't want to use pg_send_query because without parameter binding I can have sql injection vulnerabilities in my code.
The round trips to the DB server shouldn't be your bottleneck as long as you are (a) using persistent connections (either directly or via a pool) and (b) aren't suffering from the "n+1 selects" problem.
New connections have an order of magnitude overhead which slows things down if done on every query. The n+1 problem results in generating far more trips than is really needed if the application retrieved (or acted upon) sets of related rows rather than doing all operations one at a time.
See: What is the n+1 selects problem?
Separate your queries by semicolon:
UPDATE customers SET last_name = 'foo' WHERE id = 1;UPDATE customers SET last_name = 'bar' WHERE id = 2;
Edit:
Okay you cannot do this on the call side:
The parameterized SQL statement. Must contain only a single statement. (multiple statements separated by semi-colons are not allowed.)
Another way would be to call a stored procedure with this method and this SP issues multiple statements.

Insert into MySQL tables with JOIN?

I have these tables: My final mysql db, could someone check if the tables are correctly made?
How can I make an Insertion of an ad here?
Is join used on insert also?
Thanks
Clarification: I need to insert values into multiple tables here, and don't know how to do it else than using multiple INSERT INTO statements.
So I wonder if there is anyway to make just ONE statement (one line) and use JOIN to INSERT?
As far as I'm aware of, you can't INSERT data into multiple tables Within one plain SQL statement.
There are many database abstraction frameworks out there which can do something like that (DOCTRINE TO THE RESCUE!!) but thats a whole other story.
SQL for it self it not capable of such things.
No it's not possible with an INSERT statement to insert into multiple tables. But you could use a stored procedure that would nicely batch the various inserts, and the application would have only one SQL command to emit.
I don't understand your first question about the ads. As for the second, JOIN will not be used on a standard table unless you are using it in an INSERT...SELECT statement, which you very likely aren't.

Categories