【发布时间】:2012-07-09 16:31:33
【问题描述】:
我正在阅读一个很长的文本文件,其中每一行都由一个 ID、groupID 和其他数据组成。每个 ID 可以与多个 groupID 相关联(第 1、2、3 行),并且每个 ID-groupID 组合可以与多个数据相关联(第 2,3 行)。
JWOFJ903JCKDF8O | groupID-22 | some data
JWOFJ903JCKDF8O | groupID-33 | same ID as above, but different groupID and data
JWOFJ903JCKDF8O | groupID-33 | same ID and groupID as above, but different data
...
DF8#CKJ90JJ3WOF | groupID-22 | some data
...
我正在将此数据移动到数据库中,因此我有一个 ID 表(没有 ID 重复)、一个 ID 和 groupID 表(没有 ID-groupID 重复)和一个数据表,其中引用 ID-groupID 表。
所以要向数据库中插入 1 行,我首先检查 ID 表中不存在此 ID,然后将其插入。然后我检查 ID-groupID 表中是否不存在此 ID-groupID 组合,然后将其插入。最后,在这个 ID-groupID id 下插入数据。
does this $id exist in the IDs table
if($id doesn't exist in the IDs table){
insert a new ID()
save()
}
does this ID-groupID combo exist in the ID-groupID table
if(doesn't exist){
create new id-groupid combo
}
does this data exist under the third table in association with this id-groupid combo
if(doesn't exist){
insert it
}
问题在于,由于文件非常大(100,000 行),该过程需要数小时才能完成。我可以做些什么来优化我的推进查询吗?还是改进数据库的设计?
【问题讨论】: