Database Design for Tagging with Efficient AND-Search
To design a database for tagging that supports an efficient AND-search, we need to consider the following requirements:
Traditionally, JOINs could be used to implement the tagging functionality, but that approach becomes impractical when dealing with a large number of tags. To overcome this limitation, consider the following solutions:
Entity-Attribute-Value (EAV) Model
The EAV model separates data into three tables:
This model allows for efficient search queries by creating a separate column for each tag in the Value table. Each item can have multiple rows in the Value table, one for each tag it has. To perform an AND-search, simply specify conditions on all the desired tag columns.
Bitset Approach
This approach uses a bitset to represent the presence or absence of tags for each item. A bit is allocated for each tag, and each item has a corresponding bitset. To perform an AND-search, binary operations (e.g., bitwise AND) can be used on the bitsets of the specified tags.
Reference to Performance Tests
For further insights, refer to the following resources:
Note that the cited sources provide specific performance evaluations for MySQL, which may have improved its full-text indexing capabilities since their publication.
The above is the detailed content of How to Design a Database for Efficient AND-Search on Tags?. For more information, please follow other related articles on the PHP Chinese website!