if i wasIf I were you, i willI would try to add somekindsome sort of index to your MySQL database for this fields:
when this is done i willI would make changes your SQL, something similar to a kind of this SQL:
SELECT DISTINCT
cb.customers_id,
cb.customers_basket_date_added,
c.customers_firstname,
c.customers_lastname,
c.customers_email_address,
c.customers_telephone,
sc.datemodified AS last_contacted
FROM
customers_basket cb
customers_basket cb INNER JOIN customers c ON c.customers_id = cb.customers_id
LEFT JOIN scart sc ON cb.customers_id = sc.customers_id
WHERE
( cb.customers_basket_date_added BETWEEN '{start-date}' AND '{end-date}' ) AND
NOT EXISTS (
SELECT
sc.customers_id
FROM
scart sc
WHERE
sc.datemodified > '{date-modified}' AND
sc.{customerId-field} = cb.customers_id )
ORDER BY
ORDER BY
cb.customers_basket_date_added DESC
i have changeI changed your NOT INNOT IN function and changes it to NOT EXISTSNOT EXISTS, and changechanged a lot more.
whyWhy are you using distinct and not group by function?
when this SQL perfomeperforms fast, we can go to next step, the SQL inside your loop, is.
Is it posible you canto make youa SQL dump so ithat I can download it toand help you out more? i
I think you have a big mistake in your 33rd block of SQL, maby itsmaybe it can be done widthwith only 1 SQL select and not 1 select * 2 select each loop run, this mean if you got 90 rows out you make 181 selects, its take a big perfome from you MySQL and i prettyI am sure that you can do it on only onewith less select, :) statements being run against the SQL Server.