在已检索的 mysql 结果中搜索
我试图限制执行 mysql 查询的次数,因为这可能会导致 2k+ 查询,只是为了完成相当小的结果。
我正在浏览一个 CSV 文件,我需要检查 csv 中内容的格式是否与数据库期望的格式匹配,有时我尝试完成一些基本的清理工作(例如,我有一个字段是一个字符串,但有时在 csv 中为 jb2003-343,我需要去掉 -343)。
我做的第一件事是从数据库中按名称获取需要从 csv 中检索的字段列表,然后获取 csv 中这些列的索引,然后遍历 csv 中的每一行并获取每个索引列
get_fields = BaseField.find_by_group(:all, :conditions=>['group IN (?)',params[:group_ids]]) csv = CSV.read(csv.path) first_line=csv.first first_line.split(',') csv.each_with_index do |row| if row==0 col_indexes=[] csv_data=[] get_fields.each do |col| col_indexes << row.index(col.name) end else csv_row=[] col_indexes.each do |col| #possibly check the value here against another mysql query but that's ugly csv_row << row[col] end csv_data << csv_row end end
问题是,当我添加 csv_data 的内容进行输出时,我不再与原始 get_fields 查询有任何连接。因此,我似乎不能说“这是否与数据库期望的数据类型匹配”。
我可以回到让我达到该级别的相同过程,并进行另一个这样的查询,
get_cleanup = BaseField.find_by_csv_col_name(first_line[col]) if get_cleanup.format==row[col].is_a csv_row << row[col] else # do some data clean-up end
但正如我所提到的,这可能意味着 get_cleanup 运行了 2000 多次。
有没有办法在原始 get_fields 结果中搜索名称,然后获取关联的字段,而不是这样做?
我尝试搜索“搜索轨道对象”,但不断返回有关构建搜索的结果,而不是在已存在的对象中进行搜索。
我知道我可以执行 array.search,但在对象 api 中没有看到任何有关搜索的内容。
注意:上面的代码可能并不完美,因为我还没有运行它,只是随手写下,但希望它能让您了解我的目的。
I'm trying to limit the number of times I do a mysql query, as this could end up being 2k+ queries just to accomplish a fairly small result.
I'm going through a CSV file, and I need to check that the format of the content in the csv matches the format the db expects, and sometimes I try to accomplish some basic clean-up (for example, I have one field that is a string, but is sometimes in the csv as jb2003-343, and I need to strip out the -343).
The first thing I do is get from the database the list of fields by name that I need to retrieve from the csv, then I get the index of those columns in the csv, then I go through each line in the csv and get each of the indexed columns
get_fields = BaseField.find_by_group(:all, :conditions=>['group IN (?)',params[:group_ids]]) csv = CSV.read(csv.path) first_line=csv.first first_line.split(',') csv.each_with_index do |row| if row==0 col_indexes=[] csv_data=[] get_fields.each do |col| col_indexes << row.index(col.name) end else csv_row=[] col_indexes.each do |col| #possibly check the value here against another mysql query but that's ugly csv_row << row[col] end csv_data << csv_row end end
The problem is that when I'm adding the content of the csv_data for output, I no longer have any connection to the original get_fields query. Therefore, I can't seem to say 'does this match the type of data expected from the db'.
I could work my way back through the same process that got me down to that level, and make another query like this
get_cleanup = BaseField.find_by_csv_col_name(first_line[col]) if get_cleanup.format==row[col].is_a csv_row << row[col] else # do some data clean-up end
but as I mentioned, that could mean the get_cleanup is run 2000+ times.
instead of doing this, is there a way to search within the original get_fields result for the name, and then get the associated field?
I tried searching for 'search rails object', but kept getting back results about building search, not searching within an already existing object.
I know I can do array.search, but don't see anything in the object api about search.
Note: The code above may not be perfect, because I'm not running it yet, just wrote that off the top of my head, but hopefully it gives you the idea of what I'm going for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您填充 col_indexes 数组时,您可以存储包含索引和数据类型的哈希,而不是存储单个值。
然后,您可以在
for 循环
中访问所有数据When you populate your col_indexes array, rather than storing a single value, you can store a hash which includes index and the datatype.
You can then access all your data in the
for loop