简体   繁体   English

从关系表插入到面向对象表的快速方法

[英]Fast way to insert from relational table to object-oriented table

I have a relational table with lots of columns. 我有一个带有很多列的关系表。 (import_table) I'm trying to insert all of this data into an object-oriented database. (import_table)我正在尝试将所有这些数据插入到面向对象的数据库中。

The object oriented database has tables: 面向对象的数据库具有表:

#table (tableId, name)
#row (rowId, table_fk)
#column(colId, table_fk, col_name)
#value(valueId, col_fk, row_fk)

So far I have created a procedure that will read the import_table information_schema and insert the table and the columns correctly into the object-orientated structure. 到目前为止,我已经创建了一个过程,该过程将读取import_table information_schema并将表和列正确插入到面向对象的结构中。 I then copy the import_data into a temp table with an extra identity-column just to get row-ids. 然后,我将import_data复制到带有额外标识列的临时表中,以获取行ID。 Then iterate through all rows, with an inner loop to iterate through each column and do an insert pr. 然后遍历所有行,并使用一个内部循环遍历每一列并执行插入pr。 column. 柱。 Like this: 像这样:

SELECT ROWID=IDENTITY(INT, 1, 1), * INTO #TEST
FROM import_table

DECLARE @COUNTER INT = 1
WHILE @COUNTER <= (SELECT COUNT(*) FROM #TEST)
BEGIN
    INSERT INTO #ROW (ROWID, TABLE_FK) VALUES(@COUNTER, 1)
    DECLARE @COLUMNCOUNTER INT = 1
    WHILE @COLUMNCOUNTER <= (SELECT COUNT(*) FROM #COLUMN WHERE TABLE_FK = 1)
    BEGIN
        DECLARE @COLNAME NVARCHAR(254) = select col_name from #column where table_fk = 1 and rowid = @columnCounter
        DECLARE @INSERTSQL NVARCHAR(1000) = 'insert into #value (column_fk, row_fk, value) select '+cast(@columnCounter as nvarchar(20))', '+cast(@counter as nvarchar(20))+', ' + @colName+' from #test where rowId = '+cast(@counter as nvarchar20))
        exec (@insertSQL)
        set @columncounter = @columncounter +1
    end
    set @counter = @counter +1
end

This works, but it is extremely slow. 这可以,但是非常慢。 Any suggestions on how to speed things up? 关于如何加快速度的任何建议?

Redesign the database. 重新设计数据库。

What you have there is a property bad type of table. 您所拥有的是表的属性错误类型。 These are known trade off setups, and guess what - speed is the item they are bad in. Ouch. 这些是已知的折衷设置,然后猜猜-速度是他们不擅长的项目。

You likely could do things faster in C# outside and then stream the inserts back in. 您可能可以在外部C#中更快地执行操作,然后再将插入流送回。

One way to speed up your code is to wrap your inserts in transactions (maybe one transaction per iteration of the outer loop?). 加快代码速度的一种方法是将插入内容包装在事务中(可能是外循环的每个迭代有一个事务?)。 Having each one in a separate transaction, as the code is now, will be very slow if there are lots of inserts. 如果有很多插入,将每个代码(如现在的代码)放在一个单独的事务中将非常慢。

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

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