Database design problems with multiple values in one field

Source: Internet
Author: User
The database design problem of one field with multiple values now involves field. The value is single. for example, the value is 3.
There is one field with multiple values. For example, field B may be 1, 2, 3, or 4.
Now we need to find all records that contain the value of field. For example:

Records:, 5, and 7 (compliant)
Records, 8, and 9 (non-conforming)

How can I design a database reasonably? Fully considering performance optimization.


Reply to discussion (solution)

I seem to have thought of methods.
In this way, the data of field B is saved in the database.
| 2 | 4 | 6 | 3 |
If you want to query whether 3. like % | 3 | %. Is this feasible?

Yes, or use commas to separate them.

If the value range cannot exceed 64, you can use the set type directly.
If you do not want to use the set type, the value range is more than 64.
The varchar or text type can be used.
Data is composed of strings separated by commas (,), such as 2, 4, 6, and 3.
Mysql provides the find_in_set function for searching.

If the value range cannot exceed 64, you can use the set type directly.
If you do not want to use the set type, the value range is more than 64.
The varchar or text type can be used.
Data is composed of strings separated by commas (,), such as 2, 4, 6, and 3.
Mysql provides the find_in_set function for searching.

Can you talk about the optimal solution?
Few value ranges. Under 20. The value is selected from a fixed array.
Performance is a concern. If tens of thousands or even tens of thousands of lines are fake, it is too slow to find matching data.

If the value range cannot exceed 64, you can use the set type directly.
If you do not want to use the set type, the value range is more than 64.
The varchar or text type can be used.
Data is composed of strings separated by commas (,), such as 2, 4, 6, and 3.
Mysql provides the find_in_set function for searching.

Taught. For the first time, I learned about the mysql field and the set type. Mysql also has the find_in_set function.
Both methods can solve the problem. If you have time, I will test which method is the fastest!

Like, find_in_set, and in
They are all looking for "whether it is in it", and the performance will certainly not be high.
Package the data together, and change the space by time.

To achieve high-speed purposes, of course, you cannot package together, but store and add indexes independently. of course, you must add the host id.
This is the so-called space for time

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.