This does speed it up a little, thank you. I'm still thinking it shouldn't take this long. Still 100 rows/45 sec.
Using the function you provided above the whole sql query is:
select m.master_id,
customers_x(m.master_id,m.product_id,1),
customers_x(m.master_id,m.product_id,2)...