循环内的 SQL 查询
Google 代码建议您应该避免在循环内进行 sql 查询。原因是多次往返数据库会显着降低脚本速度。他们给出的示例查询是这样的。
$userData = array();
foreach ($userList as $user) {
$userData[] = '("'.$user['first_name'].'", "'.$user['last_name'].'")';
}
$query = 'INSERT INTO users (first_name,last_name) VALUES'.implode(',',$userData);
mysql_query($query);
我的问题是... 1. 让查询脱离循环有多重要?它总是可以避免的吗? 2. 如何用同样的逻辑实现 SELECT 语句。
即假设我有这个查询。
$index=0;
while ($index < count($id)) {
$result[] = mysql_query("SELECT * FROM tblInfo WHERE site_id = '".$id[$index]."' ");
$index++;
}
如何在循环之外执行此 SELECT 语句?我有大量比这复杂得多的 SELECT 语句。因此,如果认为有必要,我想让这些查询脱离循环。如果有人同意谷歌的观点,请您发布一些示例代码。
任何回应将不胜感激。
Google code suggests that you should AVOID sql queries within a loop. The reason being that the multiple round trips to the database significantly slows your scripts down. The example query they give is this.
$userData = array();
foreach ($userList as $user) {
$userData[] = '("'.$user['first_name'].'", "'.$user['last_name'].'")';
}
$query = 'INSERT INTO users (first_name,last_name) VALUES'.implode(',',$userData);
mysql_query($query);
My questions are...
1. How important is it to keep your query out of a loop, and is it always avoidable?
2. How can you implement a SELECT statement with this same logic.
i.e. Let's say I have this query.
$index=0;
while ($index < count($id)) {
$result[] = mysql_query("SELECT * FROM tblInfo WHERE site_id = '".$id[$index]."' ");
$index++;
}
How can this SELECT statement be executed outside of a loop? I have a large amount of SELECT statements that are far more complex than this. So if deemed necessary, I'd like to get these queries out of loops. If someone out there is concurring with google, could you please post some sample code.
Any response will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 MySQL
IN
运算符与 ID 列表结合使用。它甚至可以处理包含数千个 ID 的非常长的列表(当然比数千个 SELECT 更好)。但在这种情况下,您还应该考虑应用程序的设计。如果您需要在每次页面加载时使用具有数千个 ID 的 IN,那么您的设计就大错特错了。
INSERT
也可以压缩为一个查询,请参见 文档。一般来说,循环中的大多数查询通常可以重写为子查询。但是在这种情况下,您必须在性能和可读性/可维护性之间进行选择。子查询通常很难理解和优化/调试。
You can use MySQL
IN
operator with a list of IDs.It can handle even very lengthy lists of thousands of IDs (certainly better than thousand
SELECT
s). But in such case you also should consider design of your application. If you need to use IN with thousands of IDs on every page load, something is very wrong in you design.INSERT
s can also be condensed into one query, see documentation.Generally, most queries in loops can be usually rewritten as subqueries. however in such case, you have to choose between performance and readability/maintainability. Subqueries generally are hell to understand and optimize/debug.
避免往返费用对您来说有多重要取决于几件事。
首先,您有多少次往返?如果你要做 3 或 4 个,你可能会忽略这个建议,因为听从它会很痛苦。
其次,您的设置中的往返费用是多少?如果到数据库的往返需要 100 毫秒,那么比只需要 2 毫秒更要避免这种情况。
第三,需要执行查询的进程的时间敏感性如何?如果你让用户等待,你真的应该注意这一点——用户讨厌等待!如果您使用的是在幕后运行并执行一些工作的 Ajax 进程,也许它不太重要(尽管您可能仍然需要注意超时)。
基本上,谷歌的建议是好的,因为浪费时间就是浪费时间。但是,根据您的具体情况,浪费时间对您、您的系统和您的用户来说可能或多或少严重。
How important avoiding the round trip cost is to you depends on a few things.
First, how many round trips are you making? If you're making 3 or 4, you can probably ignore the advice if heeding it would be painful.
Second, how costly is the round trip for you in your setup? If a roundtrip to the db takes 100 ms, that's to be avoided much more seriously than if it only takes 2 ms.
Third, how time-sensitive is the process that needs to do the queries? If you're making a user wait, you should really pay attention to this - users hate to wait! If you're using an Ajax process that runs behind-the-scenes and does some work, maybe it's less important (though you'll still have to watch out for timeouts, maybe).
Basically, Google's advice is good, in that wasted time is wasted time. However, depending on your specific case, wasted time may be more or less serious to you, your system, and your users.