更好的多列搜索设计 - Django PostgreSQL

时间:2022-03-07 12:33:01

I have a table of products(productsmaster).
For each product in the table, I have four columns to show the months in which quarterly check dates are conducted (product1: Q1-Jan, Q2-Apr; product2: Q1-Dec,Q2Mar...)
I am developing an app in Django-PostgreSQL that for a specified month, it picks all products that are reporting in the specified month ie that have the month in column Q1 or Q2 or Q3 or Q4.
The products are set up once but are accessed a lot more when being read for reports and processing. I am sorry I dont have any code yet as I am looking to clarify design before I start coding. Can anyone advise how I can improve this? Database design if possible but willing to use django solutions too.

我有一张产品表(productsmaster)。对于表中的每个产品,我有四列显示进行季度检查日期的月份(产品1:Q1-Jan,Q2-Apr;产品2:Q1-Dec,Q2Mar ...)我正在开发一个应用程序Django-PostgreSQL在指定月份,它选择在指定月份报告的所有产品,即在Q1或Q2或Q3或Q4列中有月份的产品。产品设置一次,但在阅读报告和处理时可以访问更多。对不起,我还没有任何代码,因为我希望在开始编码之前澄清设计。任何人都可以建议我如何改进这个?数据库设计如果可能,但也愿意使用django解决方案。

1 个解决方案

#1


0  

You should have a dedicated model for this, call it ProductCheck

你应该有一个专门的模型,称之为ProductCheck

product | quater | month
------------------------
 prod1  |   Q1   |  Jan
 prod1  |   Q2   |  Apr
 prod2  |   Q1   |  Dec
 prod2  |   Q2   |  Mar

Then your query looks like this:

然后您的查询如下所示:

ProductCheck.objects.filter(month="Jan")

Remember to put indexes on the fields that are most commonly searched upon, to speed up the query. Also you can add unique constraint to avoid duplicated data.

请记住将索引放在最常搜索的字段上,以加快查询速度。您还可以添加唯一约束以避免重复数据。

#1


0  

You should have a dedicated model for this, call it ProductCheck

你应该有一个专门的模型,称之为ProductCheck

product | quater | month
------------------------
 prod1  |   Q1   |  Jan
 prod1  |   Q2   |  Apr
 prod2  |   Q1   |  Dec
 prod2  |   Q2   |  Mar

Then your query looks like this:

然后您的查询如下所示:

ProductCheck.objects.filter(month="Jan")

Remember to put indexes on the fields that are most commonly searched upon, to speed up the query. Also you can add unique constraint to avoid duplicated data.

请记住将索引放在最常搜索的字段上,以加快查询速度。您还可以添加唯一约束以避免重复数据。