2

I am using a combination of php and MySql. My case is I am using two queries (one 'select' and 'insert') for an activity, sometimes 3 queries. This will be done very frequently by different users.

I am using mysql_query function separately to execute the queries. Is this good or is it better to use a SQL function which executes all the queries. I want to know which is better regarding performance.

3 Answers 3

1

In my opinion it is better practice to create a function which performs a single operation well. This makes the functions easier to test and allows them to be utilized in other operations in the future. In your case I would create a stored procedure to execute the routine.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're using the mysqli extension you can utilize multi_query:

http://php.net/manual/en/mysqli.multi-query.php

However please note that if one of the queries relies on the success of another of the queries, they should be separated out for error checking.

3 Comments

this is my case . second query will be executed only based on the result of first query. so which one is better executing queries seperately or writing a sql function
Then execute them separately. You need to error check that the first function actually went through before you make calls that depend on it.
If one depends on the other, you must execute them separately, because you have to check feedback in the program layer. edit Although there might be a rollback on error option for multi query functions.
0

It would help if you could show concrete examples - right now, it's all very hypothetical. However:

If you can combine the operations into a single query, it's the fastest option. For instance:

insert into tableA
select x, y, z
from tableB
where Foo = Bar

If you have to do some processing on the results of the select statement, you can create a stored procedure to do the processing. This avoids the round trip of sending the data back to your PHP server, which should yield a performance benefit - however, it may well be that PHP is a better/faster language to execute the processing in than the stored procedure. For instance, if you have to manipulate text, I'd do it in PHP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.