Kiran
Kiran

Reputation: 8538

Using Multiple Database query compared to single Database query in MySQL

I might be a trivial questions, but I dare ask, please help me answer this questions.

I have been developing a PHP Mysql Application. Earlier, I used to use the following principle :

  1. Validate the HTML form through Javascript
  2. Error Validation in PHP by checking each of the input
  3. If successful, update the Database through a single MySQL query

Now, I am following another approach as follows :

  1. No Javascript Validation, but use required attribute in the form where input is necessary
  2. Error validation of each of the input element in PHP and simultaneously update the database

So, in the second approach, I am using, I am making multiple MySQL query( around 30 queries) for each row update.

I know, it might not result in an optimal performance, but,

  1. it has reduced the number of lines of code by a factor of atleast 3( if not more).
  2. It is much easier to Develop, Debug, and collaborate
  3. Development time is much less (by my own estimate, by a factor of 5)

I would like your thoughts on the second approach, I am currently using in my application.

Upvotes: 0

Views: 68

Answers (2)

Nachiket Kate
Nachiket Kate

Reputation: 8571

I think here confusion is for 2 points,

  1. Code development efforts and readability
  2. Performance

Generally, Code development and readability takes precedence over performance. This approach generally works well for small scale applications. As small scale applications will not face drastic performance issues.

But when Application grows in size then 1 query vs 30 queries will show the difference. Imagine if 100-1000 users use your application and with your 1st approach you will fire 100-1000 queries but with 2nd approach you will fire 3000-30000 queries on DB.

This can slow down your entire application. As no. of queries are directly proportional to,

  1. Connection pool limit
  2. DB locking time (1 query will lock row for small time x but 30 queries will lock row for 30x time)

In worst case, If your application has other forms,modules which are reading from DB as well then those queries will suffer from locking issue.

For greater performance and scalability, less no. of update queries should be preferred if they come with little coding overhead. And other jquery modules can used for validation which will also reduce coding effort.

Upvotes: 1

Frankey
Frankey

Reputation: 3757

You almost answered your own question I think, you are basically saying, I am on a crossroad where I can choose for script which executes 30 queries, or with a little more work, it could be 1 query.

I think it is a no go. It is much more efficient building a query statement with the values that are send. Also a best practice because mostly, you cannot predict how the application will grow.

I like html validation also, though browsers still handle this differently. Since most of my projects involves using jQuery, I tend to use jquery.validate plugin, which requires just a few lines extra to get a instant responsive form which is great for User Interaction. Ofcourse, serverside validation should always be in place.

Upvotes: 0

Related Questions