-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
Steps to reproduce
- Install Magento from
2.0.2tag (the one we tested on, but likely affects all versions). - Create/import enough products so that when running an export, the main product query will have to use offsets to batch the results
- Look at the CSV and you will find duplicated product entries and some products that are missing altogether. From my testing, the total count is always correct and matches a SELECT COUN(*) FROM catalog_product_entity.
I spent some time looking into what may be causing this, and I believe it has to do with the ORDER BY has_options clause in the MySQL query. What I'm not sure about, is why the Magento export orders by that field in the first place.
When combining that ORDER BY with an OFFSET, the MySQL result order is inconsistent.
This can be observed by running the following queries against the Magento2 sample database.
-
Query the first 10 products
SELECT entity_id, sku FROM catalog_product_entity AS e ORDER BY e.has_options ASC LIMIT 10; -
Query the first 20 products
SELECT entity_id, sku FROM catalog_product_entity AS e ORDER BY e.has_options ASC LIMIT 20;
If you compare the result set... you'll see what I'm talking about. For me, the first row in query 1 has entity_id 9347. In query 2, the first row has entity_id 90. Once you introduce offsetting the results, you get the same problem.
My proposed solution would be to order by entity_id in the export query, as that will always produce consistent result ordering, even with limits and offsets. This would require a change in Magento\CatalogImportExport\Model\Export\Product:: export
However, without knowing why it's sorted by has_options in the first place, I can't be sure.