douwen5546 2014-12-11 08:15
浏览 19

聊天应用数据库中的行太多

I have a laravel application and i want to build an instant chat Module for my application.

I have made a database structure by using the logic of Eloquent relationships (hasMany, BelongsTo).

Here is the structure:

http://s17.postimg.org/3knkv55lr/IMG_1648.jpg

The problem about it is that there is 1 row for every chat between every user.

Which means: (so bad) :(

for 50 users, 50•((50-1)/2) = 1225 rows

for 100 -> 4950

and for 250 users which is possible -> 31125 rows on channels table which will be queried in every message.

I need suggestions to make this smaler somehow

Thanks!

  • 写回答

1条回答 默认 最新

  • doulue7522 2014-12-12 09:41
    关注

    I would create my database like this

    **Users**
    user_id
    username
    etc
    
    
    
    **Chats**
    chat_id
    chat_name
    etc
    
    **Messages**
    message_id
    message_user_id     ->foreighn key to users table field user_id
    message_text
    chat_id             ->foreighn key to chats field chat_id
    

    The amount of rows will be

    1(unique chat) + amount of messages

    You dont have to save the users in the chat table, you can find them by looking in the messages table field chat_id

    If you want to save the users which joined a chat (without posting a message) you could add a table

     **Chat_Users**
     chat_id            ->foreighn key to chats table field chat_id
     user_id            ->foreighn key to users table field user_id
    

    The amount of rows will be

    1 (unique chat) + amount of messages + amount of users in chat

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计