我正在信息系统中开发消息中心,今天用户 Eric 建议使用 hierarchyid 数据类型来跟踪消息回复,因为目标是显示为 Outlook 或 Gmail 对话。
为了简化,我在我的数据库表 Messages 中有:
MessageId int PK
ReplyToId int FK null
Subject varchar
Body varchar
Hierarchy hierarchyid
当插入新消息时,我有一个触发器来进行更新。
我插入了一条新消息,层次结构为空,因为是第一条消息,而不是回复。
如果尝试插入对该消息的回复,则 hierarchyid 仍然为空... :(
我的触发器:
ALTER TRIGGER [dbo].[trg_UpdateHierarchy]
ON [dbo].[Messages]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
DECLARE @replyId int
SELECT @replyId = inserted.ReplyId
FROM inserted
IF(@replyId IS NULL)
BEGIN
RETURN
END
DECLARE @parent hierarchyid
SELECT @parent = Hierarchy
FROM [Messages]
WHERE [Messages].MessageId = @replyId
DECLARE @currentHierarchy hierarchyid = @parent.GetDescendant(null, null).ToString()
DECLARE @messageId int
SELECT @messageId = inserted.MessageId
FROM inserted
UPDATE [Messages]
SET Hierarchy = @currentHierarchy
WHERE [Messages].MessageId = @messageId
END
GO
我做错了什么?
另一点,我读过索引,但深度优先不适合,因为有很多空值,因为来自对话的第一条消息具有空值,而面包优先是最好的索引类型,具有更好的表现?或者我可以丢弃这个索引?
提前致谢!
编辑:
我已经更新了触发器,但没有hierarchyid
以正确的方式进行。
现在触发器是:
ALTER TRIGGER [dbo].[trg_UpdateHierarchy]
ON [dbo].[Messages]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
DECLARE @messageId int
DECLARE @ParentId int
SELECT @messageId = inserted.MessageId,
@ParentId = inserted.ParentId
FROM inserted
IF(@ParentId IS NULL)
BEGIN
UPDATE [Messages]
SET Hierarchy = hierarchyid::GetRoot()
WHERE [Messages].MessageId = @messageId
RETURN
END
ELSE
BEGIN
DECLARE @parent hierarchyid
SELECT @parent = Hierarchy
FROM [Messages]
WHERE [Messages].MessageId = @ParentId
DECLARE @lastHierarchy hierarchyid
SELECT @lastHierarchy = MAX(Hierarchy)
FROM [Messages]
WHERE Hierarchy.GetAncestor(1) = @parent
UPDATE [Messages]
SET Hierarchy = @parent.GetDescendant(@lastHierarchy, NULL)
WHERE [Messages].MessageId = @messageId
END
END
如果我插入诸如id = 2
hasparentId = 1
和id = 3
has之类的消息,则parentId = 2
具有此层次结构:
id = 1, hierarchy = \
id = 2, hierarchy = \1\
id = 3, hierarchy = \1\1\
第一个和第二个记录具有正确的层次结构,但下一个没有...... :(
有什么线索吗?