以有效的方式计算行级别

时间:2015-03-12 17:06:09

标签: php mysql sql ranking rank

我的下表包含大约10M的数据:

CREATE TABLE IF NOT EXISTS `tbl_site_visit` (
  `id` int(11) NOT NULL,
  `siteURL` varchar(50) NOT NULL,
  `userHash` varchar(32) NOT NULL,
  `timestamp` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

siteURL, userUID, timestamp也有索引。

现在,我想基于上述数据以快速有效的方式计算网站的排名。我写了以下查询,但排名不正确,计算排名也需要40~60sec

SET @prevRank := -1, @rankCounter := 0;

SELECT siteURL, siteRank FROM ( 
    SELECT *, @rankCounter := IF(@prevRank = visitCount, @rankCounter, @rankCounter + 1) AS siteRank, @prevRank := visitCount AS dummyRank FROM (
        SELECT siteURL, COUNT(1) AS visitCount
        FROM tbl_site_visit
        GROUP BY siteURL
        ORDER BY visitCount DESC
    ) coreTable
) rankedTable
WHERE rankedTable.siteURL = "www.google.com"
LIMIT 1;

有关如何解决此问题的任何想法,还是有任何统计公式来计算排名?

2 个答案:

答案 0 :(得分:0)

您遇到的一个错误是您正在计算所有网站的排名,然后选择该网站的结果。

您应该只计算该网站的排名。

SET @prevRank := -1, @rankCounter := 0;

SELECT siteURL, siteRank FROM ( 
    SELECT *, @rankCounter := IF(@prevRank = visitCount, @rankCounter, @rankCounter + 1) AS appRank, @prevRank := visitCount AS dummyRank FROM (
        SELECT siteURL, COUNT(1) AS visitCount
        FROM tbl_site_visit WHERE siteURL = "www.google.com"
        GROUP BY siteURL
        ORDER BY visitCount DESC
    ) coreTable
) rankedTable

这应仅计算www.google.com'并返回它的数据。可以删除一些外部SELECT,但这取决于您要对数据执行的操作。

答案 1 :(得分:0)

SELECT  @rank := @rank+1 AS rank, siteURL, visitCount
    FROM  
      ( SELECT  @rank := 0 ) AS z
    JOIN  
      ( SELECT  siteURL, COUNT(*) AS visitCount
            FROM  tbl_site_visit
            GROUP BY  siteURL 
      ) AS x
    ORDER BY  visitCount DESC;