我在数据库中有大约450K记录,我必须进行模糊匹配。我已经实现了Parallel.foreach
循环来查看传入请求是否与任何450K记录匹配。如果在搜索过程中我找到了任何匹配项,我会停止foreach
循环并将回复给回调用者返回true(表示已找到该匹配项)。
我正在调用数据库并将所有450K记录存储在静态对象(内存中)中。 Parallel.foreach
的逻辑运作良好。问题是确定它是否匹配需要大约40-45秒。
我计划在每个循环上为100k数据运行多个parallel.foreach
,以便减少总时间。我的挑战是,一旦parallel.foreach
匹配,我将如何停止执行其他parrallel.foreach
循环?
以下是我的单parallel.foreach
代码:
public bool Checkmatch(CheckRequest request)
{
bool isPCCMatch = false;
//This will load all the data in Static object to avoid db call for each request
PolicyInformation.Initialize(request.IsCacheRefresher);
try
{
logger.Debug(MethodBase.GetCurrentMethod().Name + ": Matching alogorithm is started");
Parallel.ForEach(PolicyInformation.Policies, (policy, loopState) =>
{
double state=0, stateF=0;
if (!request.stateF)
{
state= GetPrecentageMatch(request.state, policy.state);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (state== OneFieldSSNPercentage)
{
isPCCMatch = true;
loopState.Stop();
}
}
else
{
stateF= GetPrecentageMatch(request.stateF, policy.stateF);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (stateF== OneFieldFEINPercentage)
{
isPCCMatch = true;
loopState.Stop();
}
}
double SEIN = GetPrecentageMatch(request.SEIN, policy.FEIN);
//this if condtion is for checkibnf first field condtion earily so that it will not get all the perecentage
if (SEIN == OneFieldSEINPercentage)
{
isPCCMatch = true;
loopState.Stop();
}
double WCIRB = GetPrecentageMatch(request.WCIRB, policy.WCIRB);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (WCIRB == OneFieldWCIRBPercentage)
{
isPCCMatch = true;
loopState.Stop();
}
double DUN = GetPrecentageMatch(request.DUNS, policy.DUNS);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (DUN == OneFieldDUNSPercentage)
{
isPCCMatch = true;
loopState.Stop();
}
double legalNames = GetPrecentageMatch(request.LegalName, policy.LegalName);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (legalNames == OneFieldLegalNamePercentage)
{
isPCCMatch = true;
loopState.Stop();
}
double tradeNames = GetPrecentageMatch(request.TradeName, policy.TradeName);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (tradeNames == OneFieldTradeNamePercentage)
{
isPCCMatch = true;
loopState.Stop();
}
double mailingname = GetPrecentageMatch(request.MailingName, policy.MailingName);
//this if condition is for checkibnf first field condition early so that it will not get all the percentage
if (mailingname == OneFieldMailingNamePercentage)
{
isPCCMatch = true;
loopState.Stop();
}
double ownerInfo = GetPrecentageMatch(request.OwnerName, policy.Ownership);
int partitalmatchcount = 0;
int addressmatchcount = 0;
// condtion 2
// get the partial count. if it is not more than 2 then compare the address an
if (GetPartialMatchFieldCount(SSN, FEIN, SEIN, DUN, legalNames, tradeNames, mailingname, ownerInfo, out partitalmatchcount) < 2)
{
// it will be hit if IsAddressmatch is true or count of partitalmatchcount & addressmatchcount is equal or more then 2
if (IsAddressmatch(request, policy, out addressmatchcount, partitalmatchcount) || (addressmatchcount + partitalmatchcount >= 2))
{
logger.Debug(MethodBase.GetCurrentMethod().Name + ": policy matched 2nd condition " + policy.ID);
isPCCMatch = true;
loopState.Stop();
}
}
else
{
isPCCMatch = true;
loopState.Stop();
}
// check for the 3 fields
if (GroupAmatchcount(SSN, FEIN, SEIN) + GroupBmatchcount(legalNames, tradeNames, mailingname) + GroupCMatchCount(request,
policy) + GroupDMatchCount(ownerInfo) >= 3)
{
logger.Debug(MethodBase.GetCurrentMethod().Name + ": policy matched 3rd condition " + policy.ID);
isPCCMatch = true;
loopState.Stop();
}
//else if ()
});
logger.Debug(MethodBase.GetCurrentMethod().Name + ": Matching algorithm is ended");
}
catch (Exception ex)
{
logger.ErrorException(MethodBase.GetCurrentMethod().Name, ex);
throw;
}
// }
return isPCCMatch;
}
我想为100k记录运行上面的循环,因此它将有4个parallel.foreach
循环。一旦我在一个循环中找到匹配项,我想停止执行其他parallel.foreach
循环。