No need to do this yourself, the Magento system has a built-in for cleaning up log information. If you go to
System > Configuration > Advanced > System > Log Cleaning
You can configure your store to automatically clean up these logs.
----------------
Magento maintains several tables for logging. These tables log things
such as customer accesses and which products have been compared. Magento
has a mechanism for cleaning these logs regularly, but unfortunately
this feature is disabled by default and most customers do not turn it
on.you can use below SQL query to clean your DB table.
SET foreign_key_checks = 0;
TRUNCATE dataflow_batch_export;
TRUNCATE dataflow_batch_import;
TRUNCATE log_customer;
TRUNCATE log_quote;
TRUNCATE log_summary;
TRUNCATE log_summary_type;
TRUNCATE log_url;
TRUNCATE log_url_info;
TRUNCATE log_visitor;
TRUNCATE log_visitor_info;
TRUNCATE log_visitor_online;
TRUNCATE report_viewed_product_index;
TRUNCATE report_compared_product_index;
TRUNCATE report_event;
TRUNCATE index_event;
TRUNCATE catalog_compare_item;
SET foreign_key_checks = 1;