更快地执行列的非空值

时间:2018-04-25 06:28:36

标签: sql-server

我需要获取表中给定列的空值百分比。该表包含截至目前表中的368081344个记录。记录数量每天将增加2000万。以下是我正在使用的查询。

SELECT (COUNT_BIG(column)/ count_big(*)) * 100 
from <table>

然后,我执行100 - above output来获取所需的输出

请让我知道可以产生更快结果的最佳解决方案

2 个答案:

答案 0 :(得分:0)

您是否尝试过以下方法:

DECLARE @T TABLE
(
    Id INT
)

;WITH CTE
AS
(
    SELECT
       SeqNo = 1,
       NULL "Val"

    UNION ALL

    SELECT
       SeqNo = SeqNo+1,
       Val
       FROM CTE
          WHERE SeqNo<100
)
INSERT INTO @T(Id)
SELECT Val FROM CTE
UNION ALL
SELECT SeqNo FROM CTE

SELECT
    TotCount = COUNT(1),
    ValCount = SUM(CASE WHEN Id IS NULL THEN 0 ELSE 1 END),
    NullCount = SUM(CASE WHEN Id IS NOT NULL THEN 0 ELSE 1 END),
    NullPercent = (CAST(SUM(CASE WHEN Id IS NOT NULL THEN 0 ELSE 1 END) AS FLOAT)/CAST(COUNT(1) AS FLOAT))*100
    FROM @T

答案 1 :(得分:0)

仅部分回答。不确定如何获取特定列的计数

您可以使用此查询加快总行数。

SELECT      P.ROWS
FROM        SYS.OBJECTS AS O INNER JOIN SYS.PARTITIONS AS P 
            ON O.OBJECT_ID = P.OBJECT_ID
WHERE       O.NAME = 'PARENT' AND
            P.INDEX_ID < 2
ORDER BY    O.NAME