我有一个大的用户 ID 表和另一个用户记录表,其中包含带有用户 ID 的用户帖子。该过程是每当检索到新的提要帖子时,我都会向用户 id 表请求一个标记为非活动的 id(我有该字段 ACTIVE,因为我有另一个进程创建这些 id 并将其连续插入到表 1 中)并且当请求一个 id 时,它被标记为非活动状态。然后我检查用户表(表 2)中是否存在用户,如果存在则返回与该用户关联的用户 ID。
有人告诉我,我可以加快这个过程,但创建一个哈希表来查找表 2。我什至不知道如何开始这个,任何链接或示例将不胜感激。我还需要运行一个单独的进程来清理表 1 并删除所有非活动用户 ID。
当我调用插入表 2 的过程时,我传递了从表 1 中检索到的用户 ID。
CREATE TABLE [dbo].[userforums]
(
[userid] [VARCHAR](16) NOT NULL CONSTRAINT [PK_forumssiteid] PRIMARY KEY CLUSTERED ,
[forumname] [VARCHAR](500) NOT NULL,
[exported] [INT] NULL,
[lastcrawled] [DATETIME] NULL,
[priority] [INT] NULL,
[origin] [VARCHAR](50) NULL,
[queryid] [VARCHAR](25) NULL,
[dateinserted] [DATETIME] NULL DEFAULT (getdate())
)
第二张桌子
CREATE TABLE [dbo].[userids]
(
[userid] [NVARCHAR](20) NOT NULL CONSTRAINT [PK_userids] PRIMARY KEY CLUSTERED,
[active] [NVARCHAR](20) NULL CONSTRAINT [IX_userids] UNIQUE NONCLUSTERED
)
获取用户id存储过程
BEGIN TRANSACTION
SELECT TOP 1 @id = userid
FROM userids WITH (UPDLOCK, HOLDLOCK)
WHERE active = 'Y'
OR active IS NULL
UPDATE userids
SET active = 'N'
WHERE userid = @id
COMMIT TRANSACTION
检查用户标识是否存在
CREATE PROC Foo @forumname VARCHAR(500),
@userid VARCHAR(16),
@origin VARCHAR(50),
@queryid VARCHAR(25)
AS
SET NOCOUNT ON;
DECLARE @cnt INT
DECLARE @serverip VARCHAR(16)
DECLARE @mincnt INT
DECLARE @siteservercnt INT
SELECT @cnt = COUNT(*)
FROM userforums
WHERE forumname = @forumname
IF @cnt = 0
BEGIN
INSERT INTO userforums
(forumname,
userid,
exported,
origin,
queryid)
VALUES (@forumname,
@userid,
1,
@origin,
@queryid)
SELECT @siteservercnt = COUNT(*)
FROM siteserverip
WHERE userid = @userid
IF @siteservercnt = 0
BEGIN
SELECT TOP 1 @mincnt = COUNT(*),
@serverip = serverip
FROM siteserverip
GROUP BY serverip
ORDER BY COUNT(*)
SELECT TOP 1 @mincnt = sitecount,
@serverip = serverip
FROM serveripcounts
ORDER BY sitecount
INSERT INTO siteserverip
VALUES (@siteid,
@serverip)
UPDATE serveripcounts
SET sitecount = sitecount + 1
WHERE serverip = @serverip
END
END
SELECT userid
FROM userforums
WHERE forumname = @forumname
RETURN