从文件中的数据构建SQLite数据库最有效的方法是什么?

时间:2022-09-23 17:47:48

I have a single text file with data in text format and I want to put this data in an automated manner in a database for further use in a Ruby on Rails application and I want to use Ruby for solving this problem.

我有一个文本文件,其中包含文本格式的数据,我想将这些数据自动地放到数据库中,以便在Ruby on Rails应用程序中进一步使用,我想使用Ruby来解决这个问题。

I want to know the most efficient way of solving this problem and the possible solutions, that come in your mind.

我想知道解决这个问题最有效的方法和可能的解决方法,这些都在你的脑海中。

The solutions I have found include using the active record to fetch the data from the file and into the database which might not be the most efficient, so let's see what you have got in your mind???

我找到的解决方案包括使用活动记录从文件中获取数据并将其放入数据库,这可能不是最有效的方法,所以让我们看看您有什么想法?

1 个解决方案

#1


3  

I find Sequel to be faster than ActiveRecord, but both should be fine.

我发现Sequel比ActiveRecord快,但是两者都应该是好的。

  1. Read your text file into memory. If it is too large to fit into memory, you'll want to use File.foreach to operate on it one line at a time.

    将文本文件读入内存。如果它太大,无法装入内存,那么就需要使用File。每条线一次操作。

  2. Parse your text file into an array of logical fields. I'd suggest using regular expressions, unless your text file is CSV.

    将文本文件解析为一个逻辑字段数组。我建议使用正则表达式,除非您的文本文件是CSV。

  3. Perform a multi_insert on your database, which uses a single SQL command to insert n rows at once.

    在数据库上执行multi_insert,它使用一个SQL命令一次插入n行。

    • If you can't perform a batch insertion (I understand that ActiveRecord doesn't support it by default without some extension), then be sure at least to use a database transaction around all your inserts. If you don't, SQLite will be hitting the file system for each insert, which slows you down greatly.
    • 如果您不能执行批插入(我理解ActiveRecord默认情况下没有扩展就不支持它),那么请确保至少在所有插入周围使用数据库事务。如果不这样做,SQLite将会对每个插入文件系统进行攻击,这会大大降低您的速度。

Without more information about your "text file", this is about as helpful as I can be.

如果没有关于“文本文件”的更多信息,这将是我所能提供的最有用的信息。

#1


3  

I find Sequel to be faster than ActiveRecord, but both should be fine.

我发现Sequel比ActiveRecord快,但是两者都应该是好的。

  1. Read your text file into memory. If it is too large to fit into memory, you'll want to use File.foreach to operate on it one line at a time.

    将文本文件读入内存。如果它太大,无法装入内存,那么就需要使用File。每条线一次操作。

  2. Parse your text file into an array of logical fields. I'd suggest using regular expressions, unless your text file is CSV.

    将文本文件解析为一个逻辑字段数组。我建议使用正则表达式,除非您的文本文件是CSV。

  3. Perform a multi_insert on your database, which uses a single SQL command to insert n rows at once.

    在数据库上执行multi_insert,它使用一个SQL命令一次插入n行。

    • If you can't perform a batch insertion (I understand that ActiveRecord doesn't support it by default without some extension), then be sure at least to use a database transaction around all your inserts. If you don't, SQLite will be hitting the file system for each insert, which slows you down greatly.
    • 如果您不能执行批插入(我理解ActiveRecord默认情况下没有扩展就不支持它),那么请确保至少在所有插入周围使用数据库事务。如果不这样做,SQLite将会对每个插入文件系统进行攻击,这会大大降低您的速度。

Without more information about your "text file", this is about as helpful as I can be.

如果没有关于“文本文件”的更多信息,这将是我所能提供的最有用的信息。