zoukankan      html  css  js  c++  java
  • 【TPCDS】trino+S3+hive+postgresql性能测试查询与写入数据(五)

    【查询】
     
    TPC-DS的查询SQL存放路径:(为自己方便,将查询、写入脚本放在同一目录下)
    cd /root/trino/tpcds-kit/TpcdsData/script/sample-queries
    记得要把sample-queries文件上传到此处
     
     
    方式一:在数据库中验证查询sql【验证SQL正确性】
    1、进入postgresql数据库
    [root@cluster-data-node-01 ~]# /root/trino/trino-server-363/trino --server 10.201.0.125:8080 --catalog postgresql --schema public
    2、复制query1.sql中的SQL命令运行,执行结果如下示例:
     
    方式二:命令行运行单条SQL,获取SQL查询时间
    1. 执行单条SQL,query1.sql是否成功,并记录query1.sql的查询时间,需要在query1.sql的SQL语句前后加上select now()
    2. query1.sql的SQL内容示例
    select now();
    with customer_total_return as
    (select sr_customer_sk as ctr_customer_sk
    ,sr_store_sk as ctr_store_sk
    ,sum(SR_FEE) as ctr_total_return
    from store_returns
    ,date_dim
    where sr_returned_date_sk = d_date_sk
    and d_year =2000
    group by sr_customer_sk
    ,sr_store_sk)
     select  c_customer_id
    from customer_total_return ctr1
    ,store
    ,customer
    where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2
    from customer_total_return ctr2
    where ctr1.ctr_store_sk = ctr2.ctr_store_sk)
    and s_store_sk = ctr1.ctr_store_sk
    and s_state = 'NM'
    and ctr1.ctr_customer_sk = c_customer_sk
    order by c_customer_id
    limit 100;
    select now();
    3、命令行运行,查看执行结果
    /root/trino/trino-server-363/trino --server 10.201.0.125:8080 --catalog postgresql --schema public -f /root/trino/tpcds-kit/TpcdsData/script/sample-queries/query1.sql
     
    【重要】执行到此处,可以看到每条查询的sql都是单独执行,并且不方便直观的去获取到查询的时间,要自己去计算每执行一条SQL执行的时间,比较耗时耗力,因此要考虑:如何快速全部一次执行所有的SQL?如何将每条SQL的查询时间获取到秒?每个时间可以根据不同的query标记?等等,如下的方式三解决这些问题
     
    方式三:命令行运行所有查询SQL,获取查询时间
    1、新建py文件,如:bath_query_time.py,文件内容如下:
    #!/usr/bin/python
    # --*-- coding: UTF-8 --*--
    import time
    import subprocess
    import datetime
    import os
    
    def run(cmd):
        #sys.stdout.write("Running cmd: %s\n" % cmd)
        p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        p.wait()
        if p.returncode != 0:
            print "Bad rc (%s) for cmd '%s': %s" % (p.returncode, cmd, stdout + stderr)
            return -1
        return 0
    
    def run_one_presqlsql(sql):
        start_time=time.time()
        #run_presql_cmd="kubectl exec -it pod/trino-cli -- trino --server trino:8080 --catalog hive --schema default --execute \"%s\"" %(sql)
        run_presql_cmd="/root/trino/trino-server-363/trino --server 10.201.0.125:8080 --catalog postgresql --schema public -f /root/trino/tpcds-kit/TpcdsData/script/sample-queries/%s" %(sql)
        #run_presql_cmd="cat /opt/tpcds/new_tpcds_queries/tpcds-presto/%s | mysql -h 10.201.0.204 -u root -P 9030 -p123456 sf500_02 " %(sql)
        if run(run_presql_cmd)!=0:
            return -1
        else:
            return time.time()-start_time
    
    
    def get_all_queries_time_use_file(queries_dirs):
        logfilename = "/tmp/tpcds_time.log"
        logfile=open(logfilename,'w+')
        for fstr in os.listdir(queries_dirs):
            #f=open(os.path.join(os.path.abspath(queries_dirs),fstr),'r')
            run_time= run_one_presqlsql(fstr)
            if run_time!=-1:
                print >> logfile,"query:%s,time:%.2f" %(fstr,float(run_time))
    
        logfile.flush()
        logfile.close()
    
    def get_all_queries_time(queries_dirs):
        logfilename = "/tmp/tpcds_time.log"
        usedb="tpcds"
        logfile=open(logfilename,'w+')
        for fstr in os.listdir(queries_dirs):
            f=open(os.path.join(os.path.abspath(queries_dirs),fstr),'r')
            strbuf="use %s; \n" %(usedb)
            for s in f.readlines():
                strbuf+=s
            run_time= run_one_presqlsql(strbuf)
            if run_time!=-1:
                print >> logfile,"schema:%s,query:%s,time:%.2f" %(usedb,fstr,float(run_time))
    
        logfile.flush()
        logfile.close()
    
    if __name__ == "__main__":
        get_all_queries_time_use_file("/root/trino/tpcds-kit/TpcdsData/script/sample-queries/")

    【重要】注意文件修改内容:

    • run_presql_cmd:sql查询语句存放的目录
    • logfilename = "/tmp/tpcds_time.log" #执行log的存放目录,里面会记录查询的时间
    • get_all_queries_time_use_file:query的存放路径
     
    2、运行py文件
    python bath_query_time.py
    nohup python bath_query_time.py #后台运行脚本
     
    3、查看执行结果

    【写入】与查询的操作方式一致,参考如上的方式三
    常见文件,命名为bath_insert_time.py复制“查询的方式三中的脚本”,修改run_presql_cmd这行参数

    示例:查看call_center.sql的内容
     

    1.作者:Syw
    2.出处:http://www.cnblogs.com/syw20170419/
    3.本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    4.如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    hdu 6702 ^&^ 位运算
    hdu 6709 Fishing Master 贪心
    hdu 6704 K-th occurrence 二分 ST表 后缀数组 主席树
    hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列 LCIS
    hdu 5909 Tree Cutting FWT
    luogu P1588 丢失的牛 宽搜
    luogu P1003 铺地毯
    luogu P1104 生日
    luogu P1094 纪念品分组
    luogu P1093 奖学金
  • 原文地址:https://www.cnblogs.com/syw20170419/p/15593592.html
Copyright © 2011-2022 走看看