基本上,我在数据库中有一个 Items 表,其中需要经常更新项目的库存。我在XML Feed中有更新的库存值。
这并不一定意味着表格和XML Feed中的项目数必须相同。我目前在数据库中有 9K 项,而XML中有 72K 项。
这是我到目前为止所尝试的内容:
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
# added by Anaconda3 2.0.1 installer
export PATH="/Users/myname/anaconda/bin:$PATH"
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# added by Anaconda 2.0.1 installer
export PATH="/Users/myname/anaconda/bin:$PATH"
# added by Anaconda 2.1.0 installer
export PATH="/Users/myname/anaconda/bin:$PATH"
# added by Anaconda2 4.1.1 installer
export PATH="/Users/myname/anaconda/bin:$PATH"
# added by Anaconda3 5.0.0 installer
export PATH="/Users/myname/anaconda3/bin:$PATH"
..而另一种方式:
Using context As New QuotationDbContext
For Each item In context.Items.Where(Function(i) i.Active)
Dim matchedNode =
xDoc.Descendants("item"). 'xDoc is an XDocument object.
FirstOrDefault(Function(n) n.Attribute("itemcode").Value = item.ItemNumber)
If matchedNode IsNot Nothing Then item.Stock = matchedNode.Attribute("stock").Value
Next
context.SaveChanges()
End Using
问题在于两种方法都需要花费2分多钟来进行“匹配和更新”,而更新所有项目需要花费几百毫秒,因此显然匹配就是花费所有时间。
那么,是否有更好(更快)的方法来批量匹配/查找数据库中的记录以进行更新?
我认为我的问题的解决方案更可能是纯粹的LINQ解决方案不一定连接到EF 。我想提供完整的上下文,以防万一我错过了什么。
P.S。虽然我的代码是在VB中,但是使用C#代码的任何答案都非常受欢迎。
答案 0 :(得分:3)
我认为这主要是算法问题。 您花了很多时间在xml中查找项目。 这里:
import React, { Component } from 'react';
class App extends Component {
constructor(props){
super(props);
this.state = {qualification:''};
this.submitData = this.submitData.bind(this);
this.inputData = this.inputData.bind(this);
}
submitData(event)
{
}
inputData(event)
{
this.setState({[event.target.name]: event.target.value});
}
render() {
return (
<div>
<form>
<div class="btn-group">
<button type="button" class="btn btn-primary btn-ch dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Qualification
</button>
<div class="dropdown-menu dropdown-ch dropdown-menu-right">
<button class="dropdown-item" type="button" value="Gynaec" onChange={this.inputData} name="qualification">Gynaec</button>
<button class="dropdown-item" type="button" value="Medicine" onChange={this.inputData} name="qualification">Medicine</button>
<button class="dropdown-item" type="button" value="Surgery" onChange={this.inputData} name="qualification">Surgery</button>
</div>
</div>
<button>Submit</button>
</form>
</div>
);
}
}
export default App;
尝试将xml中的所有项目都添加到字典中。因为字典花费O(1)时间来寻找元素。它会是这样的(我没有IDE尝试这个代码):
Dim matchedNode =
xDoc.Descendants("item").
FirstOrDefault(Function(n) n.Attribute("itemcode").Value = item.ItemNumber)
答案 1 :(得分:0)