gpt4 book ai didi

mysql - 按允许或阻止的值过滤 50k+ 行

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:56 28 4
gpt4 key购买 nike

我的数据库包含大约 50k(还在增加)的产品。此外,我网站上的用户已经声明了他们的原籍国。

现在我需要向产品字段添加 allowed_countriesblocked_countries

  • 产品可以将这些字段留空 - 它将向所有用户显示,
  • 产品可以填写allowed_countries - 它只会显示给那些国家的用户,
  • 产品可以填写blocked_countries - 它会显示给所有用户,blocked_countries 列表中的用户除外。

此类过滤应适用于产品目录、产品页面和搜索器(当前为全文搜索)。

如何高效地设计满足这种需求的数据库?我关心的是在相当大的负载下的性能。我在考虑 MariaDB 10.0.20 中的动态列,但我不确定按一个国家过滤的速度。

最佳答案

使用链接表

产品表(id,其他数据)

国家表(身份证、姓名等)

链接表(product_id, country_id, allowed) 均不为空,1表示允许,0表示允许

select * from product 
left join linking linking_table on linking_table.product_id = product.id
left join counties_table on (countries_table.id = linking_table.country_id)

现在where变成这样了

允许的县

where countries_table.id = (USERS COUNTRY) and linking_table.allowed = 1

不允许

where countries_table.id = (USERS COUNTRY) and (linking_table.allowed = 1 or linking_table.allowed is null)

第二个增加了产品没有任何黑名单的情况。

这可能会为每个产品返回多行,因此您需要分组依据或返回不同

关于mysql - 按允许或阻止的值过滤 50k+ 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34122470/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com