简体   繁体   English

PHP PDO-使用提取时,是否需要LIMIT 1作为查询的一部分?

[英]PHP PDO- Is LIMIT 1 needed as part of the query when using fetch?

So fetchAll will return an array, but fetch will return only one result: 因此, fetchAll将返回一个数组,但fetch将仅返回一个结果:

fetch(PDO::FETCH_ASSOC);

My question is, if I have many rows on the table, would it be faster to also include LIMIT 1 in my query when using fetch , or is it totally unnecessary? 我的问题是,如果表上有很多行,使用fetch时在查询中也包含LIMIT 1会更快吗?还是完全没有必要?

If you're only going to fetch one row, it's a good idea to use LIMIT 1 . 如果您仅要获取一行,则最好使用LIMIT 1 This will reduce the load on the database, because it can stop processing the query as soon as it finds the first result. 这将减少数据库的负载,因为它可以在找到第一个结果后立即停止处理查询。 It also means less traffic over the network, because the server only needs to send back the first result; 这也意味着更少的网络流量,因为服务器只需要发回第一个结果。 without this, the server will send lots of results that get buffered in the PDO library but never get used by the application. 否则,服务器将发送大量结果,这些结果将缓冲在PDO库中,但永远不会被应用程序使用。

It's not required, but it will generally result in a faster response. 这不是必需的,但通常会导致更快的响应。 The database will not have to compute/collect the whole result set and can potentially do many optimisations if the limit is known. 如果知道限制,数据库将不必计算/收集整个结果集,并且可以进行许多优化。

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

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