snowflake python connector - Time to make database connection

半城伤御伤魂 提交于 2021-02-20 04:21:27

问题


Python code is taking around 2-3 secs to make the snowflake database connection. Is it expected behaviour ? OR are there any parameters which will speed up connection time.

Here is the sample code:

import snowflake.connector
import time

t1=time.time()
print("Start time :"+str(t1))
try:
    conn = snowflake.connector.connect(
        user=user,
        password=password,
        account=account,
        warehouse=warehouse,
    #   database=DATABASE,
    #   schema=SCHEMA
    )
    cur = conn.cursor()
except Exception as e:
    logging.error("Connection Error while initialing connection to snowflake")
    logging.error(str(e))

t2=time.time()
print("End time: "+str(t2))
t3=t2-t1
print("Difference(secs) : "+str(t3))
print("DB Connection : END")
tart time :1575009530.075109
End time: 1575009533.320529
Difference(secs) : 3.245419979095459
DB Connection : END

回答1:


You should probably turn off the snowflake logging. By default you would see a lot of console info and debug messages and writing to the console is not a cheap operation.

def add_module_handler(logger, level=logging.DEBUG):
    """Module handler for log file.

    :param logger: Logger object
    :type logger: :class:`logging.Logger`
    :param level: Log level to set., defaults to logging.DEBUG
    :type level: int, optional
    """
    logformat = '[%(asctime)s] %(levelname)s:%(name)s:%(message)s'
    logging.basicConfig(level=level,stream=sys.stdout,
                    format=logformat, datefmt="%Y-%m-%d %H:%M:%S")
    if not os.path.exists(path):
        os.makedirs('logs')
    handler = logging.FileHandler(
        f"{path}/module-{logger.name.replace('.', '-')}.log"
    )
    formatter = logging.Formatter(logformat)
    handler.setFormatter(formatter)
    handler.setLevel(level)
    for name in logging.Logger.manager.loggerDict.keys():
        if 'snowflake' or 'urllib3' in name:
            logging.getLogger(name).setLevel(logging.ERROR)
            logging.getLogger(name).propagate = False
    logger.addHandler(handler)
    return


来源:https://stackoverflow.com/questions/59100327/snowflake-python-connector-time-to-make-database-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!