php mysql 中的查询长度有限制吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3026134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a limitation to the length of the query in mysql?
提问by Bakhtiyor
I am asking this question because I need to know this limitation as I am generating SELECTquery in my PHP script and the part of WHEREin this query is generated inside the loop.
Precisely it looks like this
我问这个问题是因为我需要知道这个限制,因为我在我的 PHP 脚本中生成SELECT查询,并且这个查询中的WHERE部分是在循环内生成的。
正是它看起来像这样
$query="SELECT field_names FROM table_name WHERE ";
$condition="metadata like \"%$uol_metadata_arr[0]%\" ";
for($i=1; $i<count($uol_metadata_arr); $i++){
$condition.=" OR metadata like \"%$uol_metadata_arr[$i]%\" ";
}
$query.=$condition;
$result=mysql_query($query);
So, that's why I need to know how long my $querystring can be, because the array $uol_metadata_arrcould contain many items.
所以,这就是为什么我需要知道我的$query字符串可以有多长,因为数组$uol_metadata_arr可以包含许多项目。
回答by Frank Farmer
- (if possible) Use
WHERE metadata IN ('value1', 'value2') - You may need to increase max_allowed_packet. It defaults to 16MB (client-side, and as low as 1MB server-side in older versions), and it's not that hard to construct a query that runs up against that limit (e.g., importing data from elsewhere with a giant
INSERTquery)
- (如果可能)使用
WHERE metadata IN ('value1', 'value2') - 您可能需要增加max_allowed_packet。它默认为 16MB(客户端,在旧版本中低至 1MB 服务器端),并且构建一个超过该限制的查询并不难(例如,使用巨大的
INSERT查询从其他地方导入数据)
LIKE '%string%'is a performance killer. Such a query can't use an index on that column. LIKE 'string%'on the other hand, is indexable
LIKE '%string%'是性能杀手。这样的查询不能在该列上使用索引。 LIKE 'string%'另一方面,是可索引的
回答by ircmaxell
See the max_allowed_packetglobal variable. You'll need access to the my.cnf file to adjust it (and need to adjust it on your client as well). The typical defaults are either 1mb or 16mb...
请参阅max_allowed_packet全局变量。您需要访问 my.cnf 文件以对其进行调整(并且还需要在您的客户端上进行调整)。典型的默认值是 1mb 或 16mb...

