Developer forum

Forum » Development » How do i optimize saving multiple products

How do i optimize saving multiple products

Magnus Holmberg
Reply
Hi.

I asked this question before but did not not get an answer. Now we are developing in Dynamicweb 8 and are wondering if there exists some function that saves a whole productcollection?
Saving mutiple products takes a big effort for the database and therefor takes long time.
It there anyway of saving them in one transaction or saving a whole productcollection at once?

We have a routine that updates hundreds of products through an webservice, then creates and updates the products but it takes very long time saving products one by one. Or is it preferable to use the Data integration module, but as far I know it is not possible with this module to import from a webservice?

All this is done codewise.....

//Magnus

Replies

 
Morten Bengtson
Reply
When you save a product you can make it faster if you skip the extended save, which saves changes to product variants and languages:
// Last parameter enables you to skip extended save...
product.SaveAndConfirm(product.ID, product.VariantID, product.LanguageID, true);
You could also implement your own web service data source for Data Integration.

Or you could code it all yourself and use SqlBulkCopy...
DataTable data = GetAllProductDataFromWebService();

using (SqlBulkCopy bulk = new SqlBulkCopy(connectionstring)) {
    bulk.DestinationTableName = "EcomProductsTemporaryTable"; 
    bulk.WriteToServer(data);
}

// TODO: Update existing rows in EcomProducts from EcomProductsTemporaryTable
// TODO: Insert new rows in EcomProducts from EcomProductsTemporaryTable
// TODO: Delete old rows in EcomProducts that are not in EcomProductsTemporaryTable

If you have a lot of data to import then you probably need to implement a custom data reader that inherits from IDataReader which can read "rows" of data one by one from the web service and then use that instead of the DataTable in the example above.

Be aware that it can be very difficult to figure out the cause of any failed import unless you add some sort of validation to your custom data reader.

If it is the other way around (you have a web service connected to DW that is called from somewhere else) then you can just add products to the data table and then bulk copy every time you reach 100 products or so.

 

You must be logged in to post in the forum