简体   繁体   English

桌子设计-需要建议

[英]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? 我认为这可能需要大量资源,例如100000个用户和1000个要约,以查看其中有很多查询...。或者还有另一种方法可以做到这一点? 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. GROUP BY子句可能比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. 如果您要查询一个用户(这种情况似乎经常发生),则可以将用户ID添加到WHERE子句中。 Even on a huge table, I'd expect that WHERE clause to be pretty selective. 即使在很大的桌子上,我也希望WHERE子句具有很高的选择性。

我将创建一个新的单独的表,称为“ 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. 您将查询过去订单中所有不同的item_id,以及不同的Supplier_id。 This should return a manageable number, then you join those ids to your offer table(s) to show offers related. 这应该返回一个可管理的数字,然后将这些ID合并到要约表中以显示相关要约。 You can create one table containing all offers (item specific and supplier specific) or separate them into 2 tables. 您可以创建一个包含所有商品(特定于商品和特定于供应商)的表,或者将它们分成2个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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