雪花Python连接器-建立数据库连接的时间
问题描述
Python代码大约需要2-3秒来建立雪花数据库连接。这是意料之中的行为吗?或者是否有任何参数可以加快连接时间。
示例代码如下:
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
解决方案
您可能应该关闭雪花记录。默认情况下,您将看到大量控制台信息和调试消息,写入控制台并不是一项简单的操作。
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
相关文章