我有一个包含三列的表 correlation 。
相关
user1 user2 corr
此表包含所有用户对的相关值。
我需要更新另一个表中所有对的 corr 值。
我使用的查询是:
UPDATE correlation
SET corr = (SELECT ROUND((COUNT(*) * SUM(x.rating * y.rating) - SUM(x.rating) * SUM(y.rating)) /
(SQRT(COUNT(*) * SUM(SQUARE(x.rating)) - SQUARE(SUM(x.rating))) * SQRT(COUNT(*)
* SUM(SQUARE(y.rating)) - SQUARE(SUM(y.rating)))), 2)
FROM users AS x INNER JOIN
users AS y ON x.itemID = y.itemID
WHERE (x.userID = @user1) AND (y.userID = @user2)))
WHERE user1 = @user1 and user2 = @user2
如何使用过程执行此查询?
我首先从关联表中检索所有用户对。
SELECT user1, user2 from correlation
如何使用此查询的结果,对于返回的每一行,执行更新查询?
相关表中有大约200万行。
我尝试使用SqlDataReader(没有任何存储过程)在C#代码中执行此操作,但这花费的时间太长了。 SqlDataReader将读取所有行,并且对于每行读取它将执行更新查询。
答案 0 :(得分:2)
是否要更新关联表中的所有记录?像这样:
UPDATE correlation
SET corr = (SELECT ROUND((COUNT(*) * SUM(x.rating * y.rating) - SUM(x.rating) * SUM(y.rating)) /
(SQRT(COUNT(*) * SUM(SQUARE(x.rating)) - SQUARE(SUM(x.rating))) * SQRT(COUNT(*)
* SUM(SQUARE(y.rating)) - SQUARE(SUM(y.rating)))), 2)
FROM users AS x
INNER JOIN users AS y
ON x.itemID = y.itemID
WHERE (x.userID = user1) AND (y.userID = user2)))
或者
UPDATE c
SET corr = z.corr
FROM correlation c
INNER JOIN (SELECT ROUND((COUNT(*) * SUM(x.rating * y.rating) - SUM(x.rating) * SUM(y.rating)) /
(SQRT(COUNT(*) * SUM(SQUARE(x.rating)) - SQUARE(SUM(x.rating))) * SQRT(COUNT(*)
* SUM(SQUARE(y.rating)) - SQUARE(SUM(y.rating)))), 2) AS corr,
x.userID AS user1, y.userID AS user2
FROM users AS x
INNER JOIN users AS y
ON x.itemID = y.itemID
GROUP BY x.userID, y.userID) AS z
ON z.user1 = c.user1 AND z.user2 = c.user2
答案 1 :(得分:0)
你可以做三件事:
答案 2 :(得分:0)
如果要从数据库表中检索数据并将其发送回数据库,那么我建议创建SP,SP将为您执行相同的操作。
CREATE PROCEDURE [Procedure_Name]
AS
BEGIN
declare @user1 int
declare @user2 int
declare cur CURSOR LOCAL for
SELECT user1, user2 from correlation
open cur
fetch next from cur into @user1, @user2
while @@FETCH_STATUS = 0 BEGIN
UPDATE correlation
SET corr = (SELECT ROUND((COUNT(*) * SUM(x.rating * y.rating) - SUM(x.rating) * SUM(y.rating)) /
(SQRT(COUNT(*) * SUM(SQUARE(x.rating)) - SQUARE(SUM(x.rating))) * SQRT(COUNT(*)
* SUM(SQUARE(y.rating)) - SQUARE(SUM(y.rating)))), 2)
FROM users AS x INNER JOIN
users AS y ON x.itemID = y.itemID
WHERE (x.userID = @user1) AND (y.userID = @user2)))
WHERE user1 = @user1 and user2 = @user2
--If you have Sp to perform above put your SP
--execute your SP with user1, user2 on each row
--For example exec uspYourSP @user1, @user2
fetch next from cur into @user1, @user2
END
close cur
deallocate cur
END
现在一旦你的SP到位,你只需要从C#调用SP而不需要任何参数。这将改善您的表现,因为我们正在从CURSOR获取记录。