How to design a relational database for associating multiple tags with id?

社会主义新天地 提交于 2019-12-11 05:05:30

问题


I'm developing a project of Q/A website, and a questions in this website can be linked with multiple tags.

For example: How can I implement quicksort in C++ ?

Tags for this question can be C, C++, Algorithm

My question is how can I store these tags in MySQL table ?

My approach:

The question above has id = 123

-------------------------
|question_id | tags     |
-------------------------
| 123        |  C       |
-------------------------
| 123        |  C++     |
-------------------------
| 123        |Algorithm |
-------------------------
| 124        | Java     |
-------------------------

But if I create a table in this sense, then for lot of questions, the table will become very large.

Is there any better and efficient way to store this kind of data ?


回答1:


Have an extra table with exactly 3 columns: id of item being tagged, tag, ordering info.

When you add or delete an item, add or delete all the relevant rows from this table.

The details are here: http://mysql.rjweb.org/doc.php/lists
Efficient, fast, easy, and highly scalable.




回答2:


To keep your tags compact, make the tags themselves a first-class entity, not just some random string column:

CREATE TABLE tags (
  id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
  label VARCHAR(255) NOT NULL,
  UNIQUE KEY `index_on_label` (label)
);

Then have a small, simple join table between the questions and tags:

CREATE TABLE question_tags (
  question_id INT NOT NULL,
  tag_id INT NOT NULL,
  UNIQUE KEY `index_on_question_tag` (question_id, tag_id),
  KEY `index_on_tag` (tag_id)
);

Which should cover querying both what tags a question has and what questions a tag has.




回答3:


Managing Tags in Information Systems can be done by two methods. And we have trade-off between simplicity and low performance and complexity and difficulties and high performance.

First Solution: Using TAG Table and many-to-many relationship between Our_Table and TAG table. (as @tadman describe)

Second Solution: If we want a very quick and high performance way to retrieve data related to specific tags, we can use Bit-Mask Solution.

Bit-Mask Solution for TAG Management System (previously described for similar problem here)

In this method we have same tables (as @tadman said). just add 1 fields, a long or bigint (related to our DBMS) into the table that wants to have TAGs (like Question)

This field shows TAGs of Question in binary format. For example suppose that we have 8 records in TAG table.
1- some TAG 1
2- some TAG 2
...
8- some TAG 8

then if we want to set TAGs 1,3,6,7 to one Question, just use this number 01100101.
(I offer to use reversed version of binary (0,1) placement to support additional TAGs in future.)

we can use 10 base number to add in database (101 instead of 01100101)

Then, to find appropriate Questions for any given TAGs, JUST select from Question and use bitwise AND like below.


A: (shows the TAGs sequence that we are looking in Question, like 5 as 00000101)
B: (shows TAGs of any Question like 101 as 01100101)

select * from Question q
where A & q.B  = q.B

This query returns all Questions that have subsets of the specific TAGs.

We can use other functionalities of Bit-Wise operations like A & q.B > 0 to return at least 1 equal TAG between A and B. And So on.

Notice:
1: we have some extra difficulties when a new TAG is Added or to be Delete. But this is a trade-off. Adding or Deleting new TAGs less happens.
2: we should use Question, Tags and Question_Tags Table too. But in search, we just use new field.



来源:https://stackoverflow.com/questions/47881198/how-to-design-a-relational-database-for-associating-multiple-tags-with-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!