何时以及如何在PHP(PDO)中使用多个MySQL查询 [英] When and How to use Multiple MySQL Queries with PHP (PDO)

查看:52
本文介绍了何时以及如何在PHP(PDO)中使用多个MySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在语句中使用多个MySQL查询有什么好处,除了可以最大程度地减少代码之外.

What are the benefits of using multiple MySQL queries in a statement, other than to minimize code.

如何执行,检索和显示使用PHP(最好是PDO)在一条语句中发送的多个MySQL查询的结果.

How do you execute, retrieve, and display the results of multiple MySQL queries sent in one statement using PHP (and preferably PDO).

推荐答案

无论使用哪个数据库,将多个查询放入一个语句中都可以更加高效.如果分别执行查询,则必须通过网络(或至少在同一计算机上的进程之间)对数据库进行调用,获得与该数据库的连接(包括身份验证),传递查询,然后返回结果集,并释放每个查询的连接.

Regardless of which database you are using, it can be more efficient to put multiple queries into one statement. If you perform the queries separately, you have to make a call to the database across the network (or at least, between processes if on the same machine), get a connection to it (including autheticating), pass in the query, return the resultset, and release the connection for each query.

即使您使用连接池,来回传递的请求仍然比必要的多.

Even if you use connection pooling, you are still passing more requests back and forth than is necessary.

例如,如果将两个查询合并为一个,则已保存了第二个查询的所有来回调用.您合并的查询越多,应用程序的效率就越高.

So, for example, if you combine two queries into one, you have saved all of that extra calling back and forth for the second query. The more queries you combine, then, the more efficient your app can become.

再举一个例子,许多应用在启动时会填充列表(例如下拉列表).那可以是许多查询.一次调用即可完成所有操作,从而缩短启动时间.

For another example, many apps populate lists (such as for dropdownlists) when they start up. That can be a number of queries. Performing them all in one call can accelerate startup time.

这篇关于何时以及如何在PHP(PDO)中使用多个MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆