What is pros and cons between write application with stored procedures in php  OR with normal php queries.

Posted on 2016-09-05
Medium Priority
Last Modified: 2016-09-28
Hello, All
   I want to know what is pros and cons between write application with stored procedures in php  or with normal php queries.

Is necessary to write each query with stored procedures.
Which is the best way to create large application like any portal (ecom, travel, property etc.).
in which scenario use stored procedures or use normal php queries.
Question by:Pravin Bnakar
LVL 62

Expert Comment

by:Julian Hansen
ID: 41785627
Stored procedure have a number of advantages over direct queries from PHP

1. It separates your logic layer from your data layer. You can change the mechanics of the SP without changing the interface - so your code remains unchanged

2. It is less prone to injection attacks and other security issues

3. You can control access to the stored procedure with DB security independently of the application using it.

5. A stored procedure is the domain of a DB admin who is skilled at ensuring queries are optimal. Programmers might not have the same skill

6. There can be a performance benefit - stored procedures are cached

7. Network load can be reduced. If your have a complex operation that needs to be done with multiple calls to the DB based on finite set of parameters - you can do that by calling the SP with the parameters in one call rather than multiple round trips

8. Stored procedures can provide the same functionality across multiple applications.

1. Programmability is limited compared to a language like PHP

2. It might require employing an additional (DB) resource so has an impact on cost

3. Only means of grouping functionality is by naming convention
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41785642
You may think stored procedures will perform better, you may think defining stored procedures in the database layer is a better architecturial design than defining the queries in the application business logic layer.

Here's a "rant" on the first assumption: http://www.joinfu.com/2010/05/mysql-stored-procedures-aint-all-that/

Think about other metrics than performance, about maintainability, releaseability, tastabililty, flexibility and portability.

Then look at some CMS or other PHP systems and see what part of the data access logic is put into their MySQL database.

If you look at it from the perspecitive of using a Framework like laravel, which uses Eloquent ORM which is based on ActiveRecord, you are really far off using SQL in the implementation of an application. For a simple create, reade, update, delete example, look here:

You are not concerned about how data access is done, this is managed by frameworks. Notice especially in the create sample code, first an object of a user is created in memory, not in the database. This new user is stored to the database with the save call. It's naturally also with SQL you first need to know what to save before you can do an insert. The intersting part is whether the ActiveRecord class uses stored procs to find,save,update, or delete data. What's your guess?

Bye, Olaf.
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 41785904
Which is the best way to create large application like any portal (ecom, travel, property etc.).
in which scenario use stored procedures or use normal php queries.
This sort of question makes me think of the fellow at the automobile dealership.  He's trying to buy a vehicle, and he's basing his decision on the differences between a Hitachi back-up camera and a Sony back-up camera.  If he goes with the Hitachi, he will get a truck.  If he goes with the Sony, he will go home with a sports car.  The backup camera is a misplaced point of emphasis that overlooks the larger picture.

Any implementation of a large application would use object-oriented design.  The first principle of OO design is the Single Responsibility Principle (separation of concerns).  This means that the data model would be abstracted into a series of interfaces that accomplish all the data storage and retrieval.  These interfaces would not be concerned with the details of the database or its queries.  You might try a few different concretions that implement the interfaces, but my general expectation would be that the first successful concretion would be adequate to the success of your project.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

by:Pravin Bnakar
ID: 41785942
@Ray Paseur
 In object-oriented design should I use stored procedures or use normal php queries?
LVL 111

Expert Comment

by:Ray Paseur
ID: 41785955
Either one is OK.  Use whatever you are more comfortable with!
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41786090
While all Ray says is true there still are implementation decisions having been made by todays frameworks and/or CRMs, some may be old fashioned ways some modern API/CRUD or ORM type of approaches. To be very concrete about stored procs: I haven't seen anything using them, really. Laravel/Eloquent ORM/ActiveRecord in the end use PDO with prepared statements. That's close to stored procs, as it also makes use of caching, and - as the article about the misconception of stored procedure caching says - that's the same amount of caching per session/connection you can only have with stored procs, too, anyway. Besides MySQL is only one possible database to use as backend. ActiveRecord also supports MSSQL and Oracle. And thats thanks to PDO drivers for these databases, but also thanks to declaring that OOP abstraction Ray talks about.

Setting up stored procs you just have another level of things to maintain, need a developer capable to maintain it, and that is not a php developer capable to do MySQL queries alone, you have to have a bit more knowlegde of MySQL specifics in terms of procedures, so this at least has the need for some more experienced PHP/MySQL dev.

You are very new here and I don't know about your experience in PHP or development in general, databases or desktop application/website development, etc. If you begin, I'd not suggest learning MySQL up to stored procs, you'll find more info, tutorials and tools for the approach to handle querying with the "normal queries" approach, of which there are still plenty different ones, obviously starting on mysqli vs PDO and very directly using SQL queries or ORM (object relational mapping) frameworks,

Bye, Olaf.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question