I would like to know if it is possible to simplify the following process: I want to mark a game for shipping to a customer based on the following criteria: (If you are familiar with Netflix, it's the equivalent of their movie queue.)
- How many games they have at home.
- How many games have they received in total for their billing month.
I have a table that stores customer choices (their games queue) called wishlists. I'm using the following query to automatically rank their choices for newly inserted records (rank determines the priority of shipping):
schema (id, memberId, gameId, rank, markedForShipment, shippedOn, returnedOn)
INSERT INTO wishlists (memberId, gameId, rank, markedForShipment)
SELECT @memberId, @gameId, COALESCE(MAX(rank), 0) + 1, 0, null, null
FROM wishlists
WHERE wishlists.memberId = @memberId
I would like to automatically populate "markedForShipment" with either a 0 or a 1 based on two criteria: 1. How many games a member has already received this month
, and 2. how many games a member currently has at home
At the moment I am doing the following AFTER each insert:
Grab the plan limits from a plans table (shipmentsPerMonthLimit and gamesAtHomeLimit).
Find out how many shipments (
shipmentCount
) a member has had this monthSELECT COUNT(shippedOn) AS shipmentCount FROM wishlists INNER JOIN members ON wishlists.memberId = members.id WHERE wishlists.memberId = @memberId AND wishlists.shippedOn >= members.billingCycleStart AND wishlists.shippedOn < DATE_ADD(members.billingCycleStart, INTERVAL 1 MONTH)
Find out how many
gamesAtHome
a member hasSELECT COUNT(shippedOn) AS gamesAtHome FROM wishlists WHERE memberId = @memberId AND shippedOn IS NOT NULL AND returnedOn IS NULL
Find out how many queued shipments (
markedForShippment
) a member hasSELECT COUNT(markedForShippment) AS queuedShipments FROM wishlists WHERE memberId = @memberId AND markedForShipping = 1
Check to see if the game is inventory.
If all the following is true I update the newly inserted record by setting markedForShipping to 1
.
gamesAtHome is < than gamesAtHomeLimit
shipmentCount is < than shipmentsPerMonthLimit
queuedShipments is < gamesAtHomeLimit
How can I improve this process? Can I simplify it? I ask these questions bearing in mind that performance is probably the most important factor. 4.