在本次开发中,涉及到好友信息,因为这个好友处理,让我浪费了不短的时间。
在本次开发中,建立的好友表,数据库数据结构如下:
[SQL] 纯文本查看 复制代码 CREATE TABLE [dbo].[Con_ConnectionFriend](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NOT NULL,
[FriendUserID] [int] NOT NULL,
[AddDate] [datetime] NOT NULL,
CONSTRAINT [PK_Con_ConnectionFriend] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Con_ConnectionFriend] ADD CONSTRAINT [DF_Con_ConnectionFriend_AddDate] DEFAULT (getdate()) FOR [AddDate]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Con_ConnectionFriend', @level2type=N'COLUMN',@level2name=N'UserID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'好友ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Con_ConnectionFriend', @level2type=N'COLUMN',@level2name=N'FriendUserID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'添加时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Con_ConnectionFriend', @level2type=N'COLUMN',@level2name=N'AddDate'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'好友表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Con_ConnectionFriend'
GO
如下如所示:
这样的话,就会存在如下问题:
1. 如果为了查询方便,一个朋友就需要同时添加两条记录,如A与B是朋友, 就需要添加两条记录,UserID=A, FriendUserID= B 与 userID=B, FriendUserID=A, 这样就会增加数据库的压力
2. 如果为了减少数据库的压力,就需要处理数据库的逻辑。
因为这个原因,我试了很多分方法,到最后的还是选择的减少数据库的压力,处理数据库查询逻辑。
本次的处理是建立了数据库视图,如下所示:
[SQL] 纯文本查看 复制代码 SELECT UserID, FriendUserID, FriendUserName, AddDate
FROM (SELECT cf.FriendUserID AS UserID, r.UserID AS FriendUserID, r.UserName AS FriendUserName, cf.AddDate
FROM dbo.Con_ConnectionFriend AS cf INNER JOIN
dbo.Res_Resume AS r ON cf.UserID = r.UserID
UNION
SELECT cf.UserID, r.UserID AS Expr1, r.UserName AS FriendUserName, cf.AddDate
FROM dbo.Con_ConnectionFriend AS cf INNER JOIN
dbo.Res_Resume AS r ON cf.FriendUserID = r.UserID) AS t
这样处理的好处是,查询方便,将视图作为一个表进行处理,直接使用UerID=A 就可以查询出所有的A的朋友,不论A是在UserID上,还是在FriendUserID上。
|