Database table structure design. Solution method

by kpkmd54461 on 2012-02-13 15:59:39

Here is the translation of your content into English:

---

**Data as follows:**

- A project has multiple indicators, and each indicator has a tracking value either monthly or quarterly.

- The project requires tracking for a complete natural year.

- Example: A project starting in January 2011 should be tracked until December 2011.

- Example: A project starting in August 2011 should be tracked until December 2012.

**Question:** How many tables are needed to store such data in a database? What is the most efficient way to query this data?

**Note:**

- The project's associated year is fixed. When the tracking values span across years, they should be displayed annually with corresponding monthly or quarterly tracking values.

---

### **Supplementary Question Explanation:**

User "xinannansha" wrote:

My understanding is:

- Project 1 → Multiple Indicators

- Indicator 1 → Multiple Tracking Values

Thus, three corresponding tables would be created. Regarding query efficiency, it depends on the data volume. For general data volumes, proper indexing can ensure that joining three tables does not pose significant issues. If the data volume is extremely large, redundant fields could be considered.

The key lies in the design of the tracking values. There are two possible designs:

1. **Indicator ID, Tracking Period, Tracking Value**

- This design is more flexible and scalable but makes generating the required table structure more complex.

2. **Indicator ID, Tracking Year, Monthly Tracking Values (January...December)**

- This design is less scalable but simplifies the query process and improves efficiency.

Perhaps I haven't explained clearly enough: **"The project requires tracking for a complete natural year."** If the project does not start in January, it must track until December of the following year. For example, a project starting in August 2011 needs to track from August 2011 to December 2011 and from January 2012 to December 2012. In reality, the tracking duration ranges from a minimum of 12 months to a maximum of 23 months.

---

### **Supplementary Question Explanation:**

User "grandboy" wrote:

If there are too many columns associated with rows, designing the database using typical column-to-row conversion might lead to performance issues. Adding a row would result in a significant increase in associated rows in related tables. I once encountered a similar requirement where one row was associated with over 200 columns, and I had no choice but to use dynamic columns to handle it. However, dynamic columns also have limitations, such as field limits per table. In extreme cases, you may need to split the data into multiple tables based on specific requirements.

This is my proposed table structure, consisting of four tables:

1. **Info Master Table**: Corresponds to project name, associated year, department, etc.

2. **Index Indicator Table**: Corresponds to project indicators, in a 1-to-N relationship with the master table.

3. **Monthly Table**: Corresponds to the months being tracked for the project, in a 1-to-N relationship (12 ≤ n ≤ 32, since it tracks a complete natural year, as explained above).

4. **Monthly_Index Tracking Value Table**: Corresponds to the tracking values for each month, in a 1-to-N relationship with the monthly table.

I am seeking help to improve this design. I've considered using caching techniques, but I prefer starting with the basic design. Thank you.

No satisfactory answers were provided.

---

### **Proposed Solutions:**

1. **Database Design Should Be Simple:**

Database design should aim for simplicity to minimize complex data operations. Specific adjustments depend on the actual requirements.

2. **At Least Two Tables Are Needed:**

The business logic isn't entirely clear, but at least two tables are necessary.

3. **Three Tables Would Work:**

My understanding:

- Project 1 → Multiple Indicators

- Indicator 1 → Multiple Tracking Values

Therefore, three corresponding tables would be created. Query efficiency depends on the data volume. For general data volumes, appropriate indexing ensures that joining three tables doesn't cause major issues. For very large data volumes, adding redundant fields could be considered.

Key considerations for tracking value design:

- Option 1: **Indicator ID, Tracking Period, Tracking Value**

- More flexible and scalable but complicates generating the required table structure.

- Option 2: **Indicator ID, Tracking Year, Monthly Tracking Values (January...December)**

- Less scalable but simplifies queries and improves efficiency.

4. **Alternative Three-Table Design:**

- Table 1: **Project ID, Project Name, Year, Department**

- Table 2: **Project ID, Indicator Information, Control Indicators**

- Table 3: **Project ID, Year, Month Fields (1-12)**

- This avoids the need for row-to-column conversion.

5. **Performance Considerations for Large Data Volumes:**

If there are too many columns associated with rows, using typical column-to-row conversion might lead to performance issues. Adding a row would significantly increase the number of associated rows in related tables. In extreme cases, dynamic columns could be used, but they also have limitations (e.g., field limits per table). Splitting data into multiple tables might be necessary depending on the situation.

6. **Reference Materials:**

- How to write data generated by a data window into the backend database table: [Link](http://www.myexception.cn/pb/230223.html)

- Related articles:

- Beginner questions for guidance

- Conceptual questions about functionality

- Compiling Tomcat source code

---

Let me know if further clarification or adjustments are needed!