开发者

I need advice on how to simplify/improve an ordering model in mysql

开发者 https://www.devze.com 2023-01-18 00:43 出处:网络
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

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.)

  1. How many games they have at home.
  2. 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:

  1. Grab the plan limits from a plans table (shipmentsPerMonthLimit and gamesAtHomeLimit).

  2. Find out how many shipments (shipmen开发者_运维知识库tCount) a member has had this month

        SELECT 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)
    
  3. Find out how many gamesAtHome a member has

        SELECT COUNT(shippedOn) AS gamesAtHome
        FROM wishlists
        WHERE memberId = @memberId AND shippedOn IS NOT NULL AND returnedOn IS NULL
    
  4. Find out how many queued shipments (markedForShippment) a member has

        SELECT COUNT(markedForShippment) AS queuedShipments
        FROM wishlists
        WHERE memberId = @memberId AND markedForShipping = 1
    
  5. 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.


Looks pretty efficient to me (I suppose you use indices and foreign keys). My advise would be not to optimize unless you really have performance issues.

The only way I see to optimize this would be to introduce redundancy. For instance, you could add "shippingLimitReached" and "gamesAtHomeLimitReached" to the members table. If many users reach their limits (and probably users with a long wishlist will), you can save some SELECTs.

0

精彩评论

暂无评论...
验证码 换一张
取 消