我有一个项目,其中包括将所有用户(包括其所有属性)从Active Directory域导入到SQL Server表。此表将由Reporting Services应用程序使用。SSIS只添加已更改的行
表模型具有以下的列: -ID:(即自动生成的唯一标识符)。 -distinguishedName:包含用户的LDAP专有名称属性。 -attribute_name:包含用户属性的名称。 -attribute_value:包含属性值。 -timestamp:包含自动生成的日期时间值。
我已经创建了一个脚本任务的SSIS包,其中包含一个C#代码,可将所有数据导出到稍后由数据流任务导入到表中的.CSV。该项目没有任何问题,但生成了超过200万行(AD域有大约30,000个用户,每个用户有100-200个属性)。
SSIS包应该每天运行,并且只有当新的用户属性或属性值更改时才导入数据。
为了做到这一点,我创建了一个数据流,将整个表复制到一个记录集中。
此记录被转换成一个数据表,并在脚本组件步骤,如果在所述数据表中存在的当前行,其verfies使用。如果该行存在,则比较属性值,并仅当值不同时或在数据表中找不到该行时才将行返回给输出。这是代码:
块引用
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
bool processRow = compareValues(Row);
if (processRow)
{
//Direct to output 0
Row.OutdistinguishedName = Row.distinguishedName.ToString();
Row.Outattributename = Row.AttributeName.ToString();
Row.Outattributevalue.AddBlobData(System.Text.Encoding.UTF8.GetBytes(Row.AttributeValue.ToString()));
}
}
public bool compareValues(Input0Buffer Row)
{
//Variable declaration
DataTable dtHostsTbl = (DataTable)Variables.dataTableTbl;
string expression = "", distinguishedName = Row.distinguishedName.ToString(), attribute_name = Row.AttributeName.ToString(), attribute_value = Row.AttributeValue.ToString();
DataRow[] foundRowsHost = null;
//Query datatable
expression = "distinguishedName LIKE '" + distinguishedName + "' AND attribute_name LIKE '" + attribute_name + "'";
foundRowsHost = dtHostsTbl.Select(expression);
//Process found row
if (foundRowsHost.Length > 0)
{
//Get the host id
if (!foundRowsHost[0][2].ToString().Equals(attribute_value))
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
的代码工作,但它是极其缓慢。有没有更好的方法来做到这一点?
感谢您的建议,螺旋。我发现了一个更简单的方法来做到这一点,我刚刚导入新的AD出口到另一个表和使用EXCEPT命令: SELECT的distinguishedName,属性名称,ATTRIBUTE_VALUE FROM dbo.ad_User EXCEPT SELECT的distinguishedName,属性名称,ATTRIBUTE_VALUE FROM dbo.ad_User_Old 该命令只需要10秒。 – Sergio