2

那里有一个好的 SQL PageRank 实现吗?我看过http://www.databasedevelop.com/197517/,但它缺乏易读性和正确的(T-SQL)语法。

当我们这样做时,有人知道上面的链接使用的是哪种 SQL 吗?什么 SQL 在随机位置使用 'is',之后什么都没有使用的 'where',那个奇怪的AT关键字等等?

4

2 回答 2

4

我刚刚在 SQL 中实现了 PageRank 算法。该算法工作在以下进展中。

1.计算PageRank初始排名值;

2.将PageRank表与Edge表联合起来,将排名值发送到相邻节点,并使用聚合函数sum来“收集”接收到的值。然后,将结果保存到寺庙表TmpRank

  1. 交换PageRankTmpRank的内容,直到满足收敛条件或达到最大迭代时间,继续执行步骤 2。

这是代码:

-- The graph data and algorithm source from the book "Mining of Massive Datasets", P175, http://infolab.stanford.edu/~ullman/mmds/book.pdf
-- This script has been verified the correctness in SQL Server 2017 Linux Version.
DROP TABLE Node;
DROP TABLE Edge;
DROP TABLE OutDegree;
DROP TABLE PageRank;
CREATE TABLE Node(id int PRIMARY KEY);
CREATE TABLE Edge(src int,dst int, PRIMARY KEY (src, dst));
CREATE TABLE OutDegree(id int PRIMARY KEY, degree int);
CREATE TABLE PageRank(id int PRIMARY KEY, rank float);
CREATE TABLE TmpRank(id int PRIMARY KEY, rank float);

--delete all records
DELETE FROM Node;
DELETE FROM Edge;
DELETE FROM OutDegree;
DELETE FROM PageRank;
DELETE FROM TmpRank;

--init basic tables
INSERT INTO Node VALUES (0);
INSERT INTO Node VALUES (1);
INSERT INTO Node VALUES (2);
INSERT INTO Node VALUES (3);

INSERT INTO Edge VALUES (0, 1);
INSERT INTO Edge VALUES (0, 2);
INSERT INTO Edge VALUES (0, 3);
INSERT INTO Edge VALUES (1, 0);
INSERT INTO Edge VALUES (1, 3);
INSERT INTO Edge VALUES (2, 2);
INSERT INTO Edge VALUES (3, 1);
INSERT INTO Edge VALUES (3, 2);

--compute out-degree
INSERT INTO OutDegree
SELECT Node.id, COUNT(Edge.src) --Count(Edge.src) instead of Count(*) for count no out-degree edge
FROM Node LEFT OUTER JOIN Edge
ON Node.id = Edge.src
GROUP BY Node.id;

--WARN: There's no special process for node with out-degree, This may cause wrong result
--      Please to make sure every node in graph has out-degree

DECLARE @ALPHA float = 0.8;
DECLARE @Node_Num int;
SELECT @Node_Num = COUNT(*) FROM Node;

--PageRank Init Value
INSERT INTO PageRank
SELECT Node.id, rank = (1 - @ALPHA) / @Node_Num
FROM Node INNER JOIN OutDegree
ON Node.id = OutDegree.id

/*
--For Debugging
SELECT * FROM Node;
SELECT * FROM Edge;
SELECT * FROM OutDegree;
SELECT * FROM PageRank;
SELECT * FROM TmpRank;
*/

DECLARE @Iteration int = 0;

WHILE @Iteration < 50
BEGIN
--Iteration Style
    SET @Iteration = @Iteration + 1

    INSERT INTO TmpRank
    SELECT Edge.dst, rank = SUM(@ALPHA * PageRank.rank / OutDegree.degree) + (1 - @ALPHA) / @Node_Num
    FROM PageRank
    INNER JOIN Edge ON PageRank.id = Edge.src
    INNER JOIN OutDegree ON PageRank.id = OutDegree.id
    GROUP BY Edge.dst

    DELETE FROM PageRank;
    INSERT INTO PageRank
    SELECT * FROM TmpRank;
    DELETE FROM TmpRank;
END

SELECT * FROM PageRank;
于 2018-02-12T22:39:39.407 回答
0

根据您的 SQL Server 版本,请查看 OFFSET_FETCH 窗口函数。为此有很多页面排名应用程序。当然,这需要 2012 年。

在无法使用 OFFSET_FETCH 的情况下,我还使用 SSIS 和 NTILE() 划分的临时表来完成分页。通常使用记录数除以我想在页面中看到的最大数作为 NTILE 调用的种子。

无论出于何种原因,我什至无法打开您的链接,所以希望这是您的要求。

MSDN-OFFSET_FETCH

MSDN-NTILE

于 2013-11-13T22:25:39.300 回答