Skip to content

Instantly share code, notes, and snippets.

@steverobbins
Last active August 29, 2015 14:17
Show Gist options
  • Save steverobbins/9d1105b32f5f52e1917c to your computer and use it in GitHub Desktop.
Save steverobbins/9d1105b32f5f52e1917c to your computer and use it in GitHub Desktop.
-- This takes about 30 seconds, but will make the next query a billion times faster
alter table sales_flat_order_item add index (product_id);
-- This takes about 5 seconds with the index added
select
cpe.entity_id product_id,
cpe.sku,
at_style_number.value style_number,
cpe.created_at product_created_date,
sum(sfoi.qty_ordered) total_ordered,
max(sfoi.created_at) last_order
from catalog_product_entity cpe
left join catalog_product_entity_varchar AS at_style_number
on at_style_number.entity_id = cpe.entity_id
and at_style_number.attribute_id = 177
and at_style_number.store_id = 0
left join sales_flat_order_item sfoi
on sfoi.product_id = cpe.entity_id
where
cpe.type_id = 'simple'
group by cpe.entity_id
order by total_ordered desc, last_order desc;
-- These are products that we probably don't need anymore
select
cpe.entity_id product_id,
cpe.sku,
at_style_number.value style_number,
cpe.created_at product_created_date,
sum(sfoi.qty_ordered) total_ordered,
max(sfoi.created_at) last_order
from catalog_product_entity cpe
left join catalog_product_entity_varchar AS at_style_number
on at_style_number.entity_id = cpe.entity_id
and at_style_number.attribute_id = 177
and at_style_number.store_id = 0
left join sales_flat_order_item sfoi
on sfoi.product_id = cpe.entity_id
where
cpe.type_id = 'simple'
-- only unpurchased
and sfoi.product_id is null
-- older than 6 months
and cpe.created_at < now() - interval 6 month
group by cpe.entity_id
order by product_created_date desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment