Find all woocommerce orders that have a particular product_id in it. Returns an array of order ids.
global $wpdb;
$sql = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = $product_id ) AND order_item_type = 'line_item'";
$order_ids = $wpdb->get_col( $sql );