简体   繁体   English

通过rails的mysql查询的最大大小是多少?

[英]What's the maximum size of a mysql query, via rails?

I don't seem to be able to phrase this question well enough to google the answer. 我似乎对这个问题的措辞不够好,无法用谷歌找到答案。

What's the maximum length of a MySQL 5.1 query? MySQL 5.1查询的最大长度是多少? Also, is there a relevant Rails 3 limit? 另外,Rails 3是否有相关限制?

I have an array of 5,000+ email addresses, and need to grab the associated user ids from the db. 我有5,000多个电子邮件地址,需要从数据库中获取关联的用户ID。 My preference would be to do that all in one trip, like: 我的首选是一次完成所有操作,例如:

emails = get_emails # an array of strings
User.select(:id).where("email in (#{emails * ','})")

Even if this works for 5,000, I want to make sure that it will work someday for 50,000. 即使这适用于5,000,我想确保它有一天会工作50,000。 So how big can queries be? 那么查询有多大? And if they can't be that big, what's the next best way to get these ids? 如果它们不能那么大,那么获得这些ID的下一个最佳方法是什么? Just do the same thing in batches? 分批做同样的事情?

For this I care more about the officially supported limits than whatever happens to work on the current versions. 为此,我更关心官方支持的限制,而不是当前版本的任何工作。

Depends on your max_allowed_packet setting in your MySQL configuration. 取决于MySQL配置中的max_allowed_packet设置。 That's the number of bytes that your MySQL server will accept. 这是MySQL服务器将接受的字节数。

I would agree with Pekka that the best way to see how large you can go is to try with fake values. 我同意Pekka的看法,看看你能走多大的最好的方法就是试试假值。

Also, you can write that query like this: 此外,您可以像这样编写该查询:

User.select(:id).where(:email => emails)

And Rails will automatically do the IN query. Rails会自动执行IN查询。

You could do this in a 'sharded' manner -- and you would just be limited by the amount of memory you have... 您可以采用“分片”方式进行此操作-并且您将受到所拥有的内存量的限制...

For example store records 1..10,000 in Object1 and 10,001..20,000 in Object2 ... etc 例如在Object1中存储记录1..10,000,在Object2中存储记录10,001..20,000 ...等

In this sense there would be no limit on a rails query length 从这个意义上说,rails查询的长度没有限制。

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

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