将用户列表与数据库上的用户进行交叉检查 - 坏主意?
可以说。如果我的系统上注册了 1,000,000 个用户,并且我有一个包含 500 个名称的列表。
我想根据系统上的用户数量交叉检查这些名称,以查看哪些名称/用户已在数据库上注册。
这样的过程会显着减慢应用程序的速度吗?或者说这种事情经常发生?
也许我可以每 30 分钟缓存一次结果,这样我就不必每次都调用它。
编辑>>一点澄清:
我忘了提及我正在尝试更新 500 个名字的列表。因此,如果“foobar”和“joe”在此列表中,并且也在数据库中注册,那么我要做的就是从列表中删除“foobar”和“joe”,从而给我 498 个名称。
我认为不适合做类似的事情:
User.where('name in (?)', Array('foobar', 'joe'))
我会做类似的事情:
User.each do |registered_user|
index = list.index(list.find{ |user| user.screen_name.downcase == registered_user.screen_name.downcase })
list.delete_at(index) if index
end
filtered_list = list
上面的代码是否矫枉过正?
Lets say. If i have 1,000,000 users registered on my system and I have a list of 500 names.
I would like to crosscheck these names against the number of users on my system to see which names / users are already registered on the db.
Will such a process slow down the app significantly? Or does this sort of thing happen all the time?
Perhaps I can cache the results every 30 mins so I don't have to call it every time.
EDIT >> A bit of clarification:
I forgot to mention that I am trying to udate the list of 500 names. So if 'foobar' and 'joe' is on this list and is also registered in the db, then all I want to do is remove 'foobar' and 'joe' from the list, giving me 498 names.
I don't think it would be suitable to do something like:
User.where('name in (?)', Array('foobar', 'joe'))
I would have do something like:
User.each do |registered_user|
index = list.index(list.find{ |user| user.screen_name.downcase == registered_user.screen_name.downcase })
list.delete_at(index) if index
end
filtered_list = list
Is above code, overkill?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
任何值得信赖的现代关系数据库都应该能够轻松处理针对数据库的 500 次检查,而不会影响其他查询,假设您的表结构正确(唯一键、频繁搜索字段的索引等)
Any modern relational DB worth it's salt should easily be able to process 500 checks against the DB without affecting other queries, provided that your tables are structured properly (unique keys, indexes on frequently searched fields, etc)
如果你做得巧妙,速度会非常快。让数据库来完成工作。
返回的列表将是数据库中已存在的 500 个列表。比如调用会很快,慢数据库上顶多几秒。
干杯,
丹尼尔
If you do it cleverly, it can be very fast. Let the database do the work.
The returned list will be the ones of the 500 which already exist in the database. Such as call will be fast, at most a few seconds on a slow database.
Cheers,
Daniel