1

Magento comes with a log-cleaning process as a Magento shell script located at shell/log.php, which is also used in the log maintenance configurable in the admin.

I traced the code to Mage_Log_Model_Resource_Log for the piece responsible for cleaning when php -f log.php clean --days xxx is executed. I'm running it manually, and I'm seeing report_event and report_viewed_product_index being cleaned as well, even though I don't see any reference to them in the code. Any ideas?

Update

I was able to SHOW CREATE TABLE, which shows me everything I need to know.

report_viewed_product_index:

CREATE TABLE `report_viewed_product_index` (
  `index_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Index Id',
  `visitor_id` int(10) unsigned DEFAULT NULL COMMENT 'Visitor Id',
  `customer_id` int(10) unsigned DEFAULT NULL COMMENT 'Customer Id',
  `product_id` int(10) unsigned NOT NULL COMMENT 'Product Id',
  `store_id` smallint(5) unsigned DEFAULT NULL COMMENT 'Store Id',
  `added_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Added At',
  PRIMARY KEY (`index_id`),
  UNIQUE KEY `UNQ_REPORT_VIEWED_PRODUCT_INDEX_VISITOR_ID_PRODUCT_ID` (`visitor_id`,`product_id`),
  UNIQUE KEY `UNQ_REPORT_VIEWED_PRODUCT_INDEX_CUSTOMER_ID_PRODUCT_ID` (`customer_id`,`product_id`),
  KEY `IDX_REPORT_VIEWED_PRODUCT_INDEX_STORE_ID` (`store_id`),
  KEY `IDX_REPORT_VIEWED_PRODUCT_INDEX_ADDED_AT` (`added_at`),
  KEY `IDX_REPORT_VIEWED_PRODUCT_INDEX_PRODUCT_ID` (`product_id`),
  CONSTRAINT `FK_REPORT_VIEWED_PRD_IDX_CSTR_ID_CSTR_ENTT_ENTT_ID` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_REPORT_VIEWED_PRD_IDX_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_REPORT_VIEWED_PRODUCT_INDEX_STORE_ID_CORE_STORE_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `core_store` (`store_id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=11096 DEFAULT CHARSET=utf8 COMMENT='Reports Viewed Product Index Table'
2

If you have a look at the report_viewed_product_index table you'll notice that it has a couple of relevant indexes, most notably:

  • UNQ_REPORT_VIEWED_PRODUCT_INDEX_VISITOR_ID_PRODUCT_ID, and
  • UNQ_REPORT_VIEWED_PRODUCT_INDEX_CUSTOMER_ID_PRODUCT_ID

As the visitor & customer IDs are being truncated by the

  • _cleanVisitors(), and
  • _cleanCustomers()

methods (of app/code/core/Mage/Log/Model/Resource/Log.php) respectively, it means that any record indexing it as a primary key will be dropped as well, and due to the nature of this particular BTREE index it means that by truncating one table, (i.e log/visitor) it would essentially do the same to this table.

phpMyAdmin preview of BTREE index structure

P.S: This explanation holds true for the other table (report_event) as well.

  • Those UNQ_... are just unique index names, aren't they? How do you know by reading just the keynames that log/visitor and log/customer record deletions cascade? I did post the CONSTRAINTs which does show such condition. – musicliftsme Mar 25 '14 at 20:28
  • (Separately) The CONSTRAINTs I posted tells us that deleting records in customer_entity and catalog_product_entity with designated keys also deletes corresponding records in report_viewed_product_index. Correct? – musicliftsme Mar 25 '14 at 20:30
1

See the observer plugged on log_log_clean_after : eventClean

The code is pretty self explanatory whereas why your reports are being cleaned :)

$event = Mage::getModel('reports/event');
$event->clean();
Mage::getModel('reports/product_index_compared')->clean();
Mage::getModel('reports/product_index_viewed')->clean();

I know it is too years late but I hope it'll help future viewers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.