简体   繁体   English

将聊天记录存储在关系数据库中

[英]Storing chat logs in relational database

I am writing a chat bot that uses past conversations to generate its responses. 我正在编写一个聊天机器人,该机器人使用过去的对话来生成其响应。 Currently I use text files to store all the data but I want to use a database instead so that multiple instances of the bot can use it at the same time. 目前,我使用文本文件存储所有数据,但我想改用数据库,以便该漫游器的多个实例可以同时使用它。

How should I structure this database? 我应该如何构造该数据库?

My first idea was to keep a main table like create table Sessions (startTime INT,ip INT, botVersion REAL, length INT, tableName TEXT) . 我的第一个想法是保留一个主表,例如create table Sessions (startTime INT,ip INT, botVersion REAL, length INT, tableName TEXT) Then for each conversation I create table <generated name>(timestamp INT, message TEXT) with all the messages that were sent or received during that conversation. 然后,对于每个对话,我create table <generated name>(timestamp INT, message TEXT)其中包含该对话期间发送或接收的所有消息。 When the conversation is over, I insert the name of the new table into Sessions(tableName) . 对话结束后,我将新表的名称插入Sessions(tableName) Is it ok to programmatically create tables in this manner? 以这种方式以编程方式创建表是否可以? I am asking because most SQL tutorials seem to suggest that tables are created when the program is initialized. 我问是因为大多数SQL教程似乎都建议在程序初始化时创建表。

Another way to do this is to have a huge create table Messages(id INT, message TEXT) table that stores every message that was sent or received. 执行此操作的另一种方法是拥有一个巨大的create table Messages(id INT, message TEXT)表,该表存储已发送或接收的每个消息。 When a conversation is over, I can add a new entry to Sessions that includes the id used during that conversation so that I can look up all the messages sent during a certain conversation. 对话结束后,我可以在Sessions中添加一个新条目,其中包括该对话期间使用的id ,以便我可以查找在特定对话期间发送的所有消息。 I guess one advantage of this is that I don't need to have hundreds or thousands of tables. 我猜这的一个优点是我不需要成百上千的表。

I am planning on using SQLite despite its low concurrency since each instance of the bot may make thousands of reads before generating a response (which will result in one write). 我计划使用SQLite,尽管它的并发性较低,因为该机器人的每个实例在生成响应之前都会进行数千次读取(这将导致一次写入)。 Still, if another relational database is better suited for this task, please comment. 但是,如果另一个关系数据库更适合此任务,请发表评论。

Note: There are other questions on SO about storing chat logs in databases but I am specifically looking for how it should be structured and feedback on the above ideas. 注意:关于将聊天记录存储在数据库中,还有其他问题,但是我正在特别寻找它的结构和对上述想法的反馈。

Don't use a different table for each conversation. 请勿在每次对话中使用不同的表格。 Instead add a "conversation" column to your single table. 而是在您的单个表中添加一个“对话”列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM