compare python use py-postgresql & direct pgbench's performance

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:
使用py-postgresql驱动链接PostgreSQL, 使用unix socket链接.
测试插入性能, 单线程插入100万数据需要172秒.
$ vi test.py
import postgresql
import time
conn = { "user": "postgres", 
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
}

db = postgresql.open(**conn)
db.execute("create table if not exists tt(id int)")
ins = db.prepare("insert into tt values($1)")
print(time.time())
for i in range(0,1000000):
  ins(i)
print(time.time())

postgres@localhost-> python test.py
1422871147.8219907
1422871319.8280523
耗费172秒

TOP
 593328 postgres  20   0  189128  16960   7632 R  81.3  0.0   0:09.31 /usr/local/bin/python3 test.py                                
 593329 postgres  20   0 8628412  26260  24088 S  32.2  0.0   0:04.41 postgres: postgres postgres [local] idle  


使用while循环亦如此 : 
postgres@localhost-> cat test.py
import postgresql
import time
conn = { "user": "postgres", 
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
}

db = postgresql.open(**conn)
db.execute("create table if not exists tt(id int)")
ins = db.prepare("insert into tt values($1)")
print(time.time())
i = 0
while i<1000000:
  ins(i)
  i=i+1
print(time.time())

postgres@localhost-> python test.py
1422872074.1050985
1422872255.3471527


postgres@localhost-> cat test.py
import postgresql
import time
conn = { "user": "postgres", 
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
}

db = postgresql.open(**conn)
db.execute("create table if not exists tt(id int)")
ins = db.prepare("insert into tt values($1)")
print(time.time())
i = 0
while i<1000000:
  ins(i)
  i=i+1
print(time.time())


使用pgbench单线程插入100万, 只需要55秒. 
postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -c 1 -j 1 -t 1000000
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 1
number of threads: 1
number of transactions per client: 1000000
number of transactions actually processed: 1000000/1000000
tps = 18156.624380 (including connections establishing)
tps = 18157.507920 (excluding connections establishing)
statement latencies in milliseconds:
        0.001353        \setrandom id 0 1000000
        0.052901        insert into tt values (:id)

postgres=# select 1000000/18157.507920;
      ?column?       
---------------------
 55.0736369994104345
(1 row)


之前使用sysbench测试和pgbench测试相差也巨大.
sysbench, py-postgresql和C+libpq比性能损耗差距巨大.
python循环消耗极低, 看样子还是在驱动上, 未使用异步接口:
print(time.time())
for i in range(0,1000000):
  pass
print(time.time())

postgres@localhost-> python test.py
1422872588.5488484
1422872588.610911


其他
1. py-postgresql的例子
事务使用db.xact():
>>> import postgresql
>>> db = postgresql.open('pq://user:password@host:port/database')
>>> db.execute("CREATE TABLE emp (emp_first_name text, emp_last_name text, emp_salary numeric)")
>>> make_emp = db.prepare("INSERT INTO emp VALUES ($1, $2, $3)")
>>> make_emp("John", "Doe", "75,322")
>>> with db.xact():
...  make_emp("Jane", "Doe", "75,322")
...  make_emp("Edward", "Johnson", "82,744")



[参考]
1.  https://pypi.python.org/pypi/py-postgresql/
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
30天前
|
存储 缓存 算法
python性能问题(Performance Issues)
【7月更文挑战第19天】
34 5
python性能问题(Performance Issues)
|
Python Windows
【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )(一)
【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )(一)
1863 0
【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )(一)
|
1月前
|
自然语言处理 Python
【Python】已解决:Resource punkt not found. Please use the NLTK Downloader to obtain the resource:
【Python】已解决:Resource punkt not found. Please use the NLTK Downloader to obtain the resource:
104 1
|
1月前
|
JSON 关系型数据库 API
Python 使用 FastAPI 和 PostgreSQL 构建简单 API
最近一年公司也在卷 LLM 的应用项目,所以我们也从 goper => Pythoner。 这一年使用最多的就是 Python 的 FastAPI 框架。下面一个简易项目让你快速玩转 Python API Web。 API代表应用程序编程接口,是软件开发中最重要的概念之一。它允许程序通过发送和接收数据与其他服务进行交互。API Web 通信最广泛使用的标准之一是 REST,它依赖于JSON 格式或键值对,类似于 Python 的字典。 如果想用 Python 构建一个,那么可以从几个框架中选择。Flask -RESTful、Django Rest Framework 和 FastAPI 是最受
|
2月前
|
SQL 关系型数据库 数据库
Python执行PostgreSQL数据库查询语句,并打印查询结果
本文介绍了如何使用Python连接和查询PostgreSQL数据库。首先,确保安装了`psycopg2`库,然后创建数据库连接函数。接着,展示如何编写SQL查询并执行,例如从`employees`表中选取所有记录。此外,还讨论了处理查询结果、格式化输出和异常处理的方法。最后,提到了参数化查询和事务处理以增强安全性及确保数据一致性。
Python执行PostgreSQL数据库查询语句,并打印查询结果
|
1月前
|
自然语言处理 Java 开发工具
【Python】已解决Resource averaged_perceptron_tagger not found. Please use the NLTK Downloader to obtain t
【Python】已解决Resource averaged_perceptron_tagger not found. Please use the NLTK Downloader to obtain t
50 0
|
1月前
|
自然语言处理 Python
【Python】已解决:Resource stopwords not found. Please use the NLTK Downloader to obtain the resource:
【Python】已解决:Resource stopwords not found. Please use the NLTK Downloader to obtain the resource:
41 0
|
1月前
|
监控 开发者 Python
【Python】已解决:WARNING: This is a development server. Do not use it in a production deployment. Use a p
【Python】已解决:WARNING: This is a development server. Do not use it in a production deployment. Use a p
75 0
|
2月前
|
SQL 关系型数据库 数据库
Python查询PostgreSQL数据库
木头左教你如何用Python连接PostgreSQL数据库:安装`psycopg2`库,建立连接,执行SQL脚本如创建表、插入数据,同时掌握错误处理和事务管理。别忘了性能优化,利用索引、批量操作提升效率。下期更精彩!💡 csvfile
Python查询PostgreSQL数据库
|
3月前
|
计算机视觉 Python
ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly
ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly
70 2