简体   繁体   中英

Table design - advice needed

I have a table of products which users have bought in the past. eg

item_id - user_id - title - supplier_id - - supplier_name - date added

table of users

user_id - name

I also have a table of suppliers eg

supplier_id - supplier_name

From time to time suppliers submit special offers which i then want to pass on to my users... this could be on one item or across the board.

My question is how best to manage this

Is it a case that when a user signs into the system i look through all the offers and then match them again either items bought in the past or against the supplier as a whole (if offer across the board)

I'm thinking this might take hefty resources with say 100000 users and 1000 offers to look through which is a lot of querying.... or is there another way one would do this? maybe an interim table somewhere which would be easier to cross-reference.

I have to write this immediately and want to make sure i do it as efficiently as possible but this is a new one on me hence the question.

Is this a standard problem which has a simple solution?

Thanks for any help.

I think it makes sense to manage offers with two different tables.

  • One table deals with users who have bought particular products from a supplier.
  • One table deals with users who have bought anything from a supplier.

In the first case, you can identify those users with a query something like this one. (Code isn't tested.)

select distinct user_id
from products  -- Seems like "purchases" might be a better name.
where supplier_id = ?
  and item_id     = ?;

In the second case, use a query kind of like this one.

select distinct user_id
from products
where supplier_id = ?;

A GROUP BY clause might give you better performance than SELECT DISTINCT.

In the first case, a table of product (item) offers might look like this.

supplier_id  item_id  offer_start  offer_end
--
1            10156    2012-08-01   2012-08-15

And you'd get the users who should receive those offers with a query along these lines.

select distinct user_id
from products  -- Seems like "purchases" might be a better name.
inner join item_offers on item_offers.supplier_id = products.supplier_id
                      and item_offers.item_id = products.item_id
                      and current_date between item_offers.offer_start 
                                           and item_offers.offer_end
where supplier_id = ?
  and item_id     = ?;

If you're querying for a single user, which seems to be the case more often than not, you can add the user id to the WHERE clause. Even on a huge table, I'd expect that WHERE clause to be pretty selective.

我将创建一个新的单独的表,称为“ special_offers”(或任何您想要的表),并使其与供应商和用户之间的多对多关系。

You will only query for records for specific user, so it shouldn't be a big deal. You would query for all distinct item_ids from past orders, as well as distinct supplier_ids. This should return a manageable number, then you join those ids to your offer table(s) to show offers related. You can create one table containing all offers (item specific and supplier specific) or separate them into 2 tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM