Best DB (MySQL) structure: Articles which contain favored tags

前端 未结 2 1723
再見小時候
再見小時候 2021-01-01 05:59

I\'ve built a news site: - The articles are shown on the front page ordered by date. The newest one first. - The news are in the table \"news\" with the fields \"id\", \"t

相关标签:
2条回答
  • 2021-01-01 06:01

    The following is by no means exhaustive/definitive, but it should get you going in the right direction.

    Tables:

    news
    =====
    id
    title
    text
    
    tag
    ===
    id
    tag
    
    tag_map
    =======
    tag_id
    news_id
    
    favorite_tags
    =============
    user_id
    tag_id
    

    Query

    SELECT * 
    FROM favorite_tags
    JOIN tag_map ON favorite_tags.tag_id = tag_map.tag_id
    JOIN news ON tag_map.news_id = news.id
    WHERE favorite_tags.user_id = $userid
    
    0 讨论(0)
  • 2021-01-01 06:02

    The query's performance (whether in your sub-select approach, or Frank Farmer's more elegant join-based one) will chiefly depend on indices. Just remember that MySQL uses just one index per table, and the proper set of indices (depending on the query you want to optimize) invariably becomes pretty obvious...

    0 讨论(0)
提交回复
热议问题