douzi6060 2013-10-04 08:45
浏览 31
已采纳

SQL数据库中的长字符串替代?

We are creating a website where users can create a certain profile. At the moment we already have about 662000 profiles (records in our database). The user can link certain keywords (divided into 5 categories) to their profile. They can link up to about 1250 keywords per category (no, this isn't nonsense, for certain profiles this would actually make sense). At the moment we save these keywords into an array and insert the serialized array in the profile's record in the database.

When a different user uses the search function and searches for one of the keywords, an SQL query is executed with 'WHERE keyword LIKE %keyword%'. This means that is has to go to a pretty big number of records and go through the entire serialized array for each record. Adding an index to the keyword columns is pretty tricky, since they don't have a defined max lenght (this could be 22000+ chars!).

Is there any other more sensible and practical way to go about this?

Thanks!

  • 写回答

2条回答 默认 最新

  • douxing6434 2013-10-04 08:48
    关注

    Never, never, never store multiple values in one column!

    Use a mapping table

    user_keywords TABLE
    --------------------
    user_id       INT
    keyword_id    INT
    
    
    users         TABLE
    ---------------------
    id            INT
    name          VARCHAR
    ...
    
    
    keywords      TABLE
    ---------------------
    id            INT
    name          VARCHAR
    ...
    

    You could then return all users having a specific keyword in their profile like this

    select u.* 
    from users u
    inner join user_keywords uk on uk.user_id = u.id
    inner join keywords k on uk.keyword_id = k.id 
    where k.name = 'keyword_name'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等