zl程序教程

您现在的位置是:首页 >  工具

当前栏目

PySpark 教程之 Count(1) vs Count(*) vs Count(col_name),那个是正确的方式从表中获取计数

vs教程 获取 方式 正确 name count 计数
2023-09-11 14:18:32 时间

更多时候,我们很困惑选择正确的方式从表中获取计数,其中没有一个是错误的。但是哪一个是最熟练的呢?是count(1)还是count(*)还是count(col_name)?

在这里插入图片描述
count(1)和count(*)基本上都给出了记录的总数,而count(col_name)基本上给出了该列上 NOT NULL 记录的计数。

对于上述情况,Spark 有自己的处理方式。像往常一样,让我们​​用例子来看看。

我们将创建一个 Python 装饰器并使用“ noop ”格式进行性能基准测试。

# Lets create a simple Python decorator - {get_time} to get the execution timings
# If you dont know about Python decorators - check out : https://www.geeksforgeeks.org/decorators-in-python/
import time
def get_time(func):
    def inner_get_time() -> str:
        start_time = time.time()
        func()
        end_time = time.time()
        return (f"Execution time: {(end_time - start_time)*1000} ms")
    print(inner_get_time())

在这里插入图片描述