Data structure of Level three rebate system? Modify and query problems

Source: Internet
Author: User
A three-level rebate project needs to be able to view someone's first, two, and three users and change the person's superiors, and the data structure is initially designed:
UID 1 2 3
User ID belongs to upper Superior
1 0 0 0
2 1 0 0
3 2 1 0
4 3 2 1
5 4 3 2
6 3 2 1

User 1 of the superior platform, on the three level are 0, the user 6 of the superior is the user 3, the superior is 2, the upper level is 1, the structure is convenient to query, but modify the default superior, if the person's subordinates have 100,000 people, then this 100,000 people on the superior also need to modify, the amount of modification is too big.
If you change to:
UID 1
User ID belongs to superior
1 0
2 1
3 2
4 3
5 4
6 3
That query the user down the third level of all users, you need to query all the user's second-level users, and then query all secondary users all subordinate, such a large number of queries.
Would you like to consult us on a compromise approach?

Reply content:

A three-level rebate project needs to be able to view someone's first, two, and three users and change the person's superiors, and the data structure is initially designed:
UID 1 2 3
User ID belongs to upper Superior
1 0 0 0
2 1 0 0
3 2 1 0
4 3 2 1
5 4 3 2
6 3 2 1

User 1 of the superior platform, on the three level are 0, the user 6 of the superior is the user 3, the superior is 2, the upper level is 1, the structure is convenient to query, but modify the default superior, if the person's subordinates have 100,000 people, then this 100,000 people on the superior also need to modify, the amount of modification is too big.
If you change to:
UID 1
User ID belongs to superior
1 0
2 1
3 2
4 3
5 4
6 3
That query the user down the third level of all users, you need to query all the user's second-level users, and then query all secondary users all subordinate, such a large number of queries.
Would you like to consult us on a compromise approach?

MySQL foreign key + recursion

The second option is to retain only the UID and PID two fields, so that any level of relationship is supported from the design, and if there is an update, the number of records modified will be relatively small.
Multilevel queries, which can be simplified by recursive query syntax for each database, such as the Connect by statement in Oracle, or the Sqlsqler CTE.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.