简体   繁体   English

与MySQL中的单数据库查询相比,使用多数据库查询

[英]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. 我一直在开发PHP Mysql应用程序。 Earlier, I used to use the following principle : 之前,我曾经使用以下原则:

  1. Validate the HTML form through Javascript 通过Java脚本验证HTML表单
  2. Error Validation in PHP by checking each of the input 通过检查每个输入来在PHP中进行错误验证
  3. If successful, update the Database through a single MySQL query 如果成功,则通过单个MySQL查询更新数据库

Now, I am following another approach as follows : 现在,我正在遵循以下另一种方法:

  1. No Javascript Validation, but use required attribute in the form where input is necessary 没有Java脚本验证,但是在required输入的形式中使用required属性
  2. Error validation of each of the input element in PHP and simultaneously update the database 在PHP中对每个输入元素进行错误验证并同时更新数据库

So, in the second approach, I am using, I am making multiple MySQL query( around 30 queries) for each row update. 因此,在第二种方法中,我使用的是为每行更新进行多个MySQL查询(约30个查询)。

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). 它使代码行数减少了至少3(如果不更多)。
  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) 开发时间要少得多(据我自己估计,是5倍)

I would like your thoughts on the second approach, I am currently using in my application. 我希望您对第二种方法有什么想法,我目前正在应用程序中使用它。

I think here confusion is for 2 points, 我认为这里的混乱是2分,

  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. 但是,当应用程序大小增加时,1个查询与30个查询将显示出差异。 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. 想象一下,如果有100-1000个用户使用您的应用程序,并且使用第一种方法,您将触发100-1000个查询,但是使用第二种方法,您将在数据库上触发3000-30000个查询。

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) 数据库锁定时间(1个查询将锁定行一小段时间x,但30个查询将锁定行一小段时间30x)

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. 其他jquery模块可用于验证,这也将减少编码工作。

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. 您几乎回答了您自己的问题,我想,您基本上是在说,我处在十字路口上,我可以选择执行30个查询的脚本,或者再做一点工作,可能是1个查询。

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. 我也喜欢html验证,尽管浏览器仍然处理不同的问题。 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. 由于我的大多数项目都涉及使用jQuery,因此我倾向于使用jquery.validate插件,该插件仅需要几行即可获得即时响应的表单,这对于用户交互非常有用。 Ofcourse, serverside validation should always be in place. 当然,服务器端验证应始终存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM