0

需要组织一个关系数据库来存储和搜索下。有关键字的消息:

message1 (key word1, kw2, kw3)
message2 (kw2, kw3, kw4)
...

接下来,您需要使用一组关键字来选择那些与关键字最匹配的消息。

例如:kw2、kw4:

message2, count: 2
message1, count: 1

你认为如何最好地组织这样一个结构?最好和最快的搜索方式是什么?

4

1 回答 1

0

交集表可以处理这些关系。
下面是一个例子。(省略了序列、附加键等)message_text 仅用于示例——如果消息数据可能发生变化,包括全文可能是一种责任。

CREATE TABLE MESSAGES
(MESSAGE_ID INTEGER NOT NULL PRIMARY KEY
,MESSAGE_TEXT VARCHAR2(4000));

CREATE TABLE KEYWORDS
(KEYWORD_ID INTEGER NOT NULL PRIMARY KEY
,KEYWORD VARCHAR2(100));

CREATE TABLE MESSAGES_KEYWORDS
(MESSAGE_ID NOT NULL REFERENCES MESSAGES
,KEYWORD_ID NOT NULL REFERENCES KEYWORDS);

INSERT INTO MESSAGES
VALUES
(1,'IN A HOLE IN THE GROUND THERE LIVED A HOBBIT.');

INSERT INTO MESSAGES
VALUES
(2,'IT WAS A HOBBIT-HOLE, AND THAT MEANS COMFORT');

INSERT INTO KEYWORDS VALUES (1,'HOBBIT');
INSERT INTO KEYWORDS VALUES (2,'GROUND');
INSERT INTO KEYWORDS VALUES (3,'COMFORT');

INSERT INTO MESSAGES_KEYWORDS VALUES (1,1);
INSERT INTO MESSAGES_KEYWORDS VALUES (1,2);
INSERT INTO MESSAGES_KEYWORDS VALUES (2,1);
INSERT INTO MESSAGES_KEYWORDS VALUES (2,3);

查询关系会连接三个表:

SELECT 
MESSAGES.MESSAGE_TEXT
,KEYWORDS.KEYWORD
FROM MESSAGES
INNER JOIN MESSAGES_KEYWORDS
USING (MESSAGE_ID)
INNER JOIN KEYWORDS
USING (KEYWORD_ID)
WHERE KEYWORDS.KEYWORD = 'HOBBIT';

SELECT 
MESSAGES.MESSAGE_ID
,COUNT('X') COUNT_KEYWORDS
FROM MESSAGES
INNER JOIN MESSAGES_KEYWORDS
ON (MESSAGES.MESSAGE_ID = MESSAGES_KEYWORDS.MESSAGE_ID)
AND MESSAGES.MESSAGE_ID = 1
INNER JOIN KEYWORDS
USING (KEYWORD_ID)
GROUP BY MESSAGES.MESSAGE_ID;
于 2013-07-04T19:00:52.593 回答