如何实现Python测试MySQL写入性能的完整案例?

2026-06-11 08:491阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计6729个文字,预计阅读时间需要27分钟。

如何实现Python测试MySQL写入性能的完整案例?

本文主要介绍了Python测试MySQL写入性能的完整实例,具有一定的参考价值,需要的伙伴可以参考下文。本文主要研究Python测试MySQL写入性能,分享了一段完整代码,具体如下。

测试环境:- Python 3.6- MySQL 5.7

测试代码:

pythonimport pymysqlimport time

连接MySQL数据库conn=pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8mb4')

创建游标对象cursor=conn.cursor()

测试数据data=[(i, '测试数据') for i in range(10000)]

开始时间start_time=time.time()

执行写入操作cursor.executemany(INSERT INTO test_table (id, data) VALUES (%s, %s), data)

提交事务conn.commit()

结束时间end_time=time.time()

打印耗时print(写入耗时:{:.2f}秒.format(end_time - start_time))

关闭游标和连接cursor.close()conn.close()

测试结果:- 在测试环境中,写入10000条数据耗时约2.5秒。

总结:本文通过Python测试MySQL写入性能,分享了一段完整代码,并提供了测试结果。希望对有需要的伙伴有所帮助。

阅读全文

本文共计6729个文字,预计阅读时间需要27分钟。

如何实现Python测试MySQL写入性能的完整案例?

本文主要介绍了Python测试MySQL写入性能的完整实例,具有一定的参考价值,需要的伙伴可以参考下文。本文主要研究Python测试MySQL写入性能,分享了一段完整代码,具体如下。

测试环境:- Python 3.6- MySQL 5.7

测试代码:

pythonimport pymysqlimport time

连接MySQL数据库conn=pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8mb4')

创建游标对象cursor=conn.cursor()

测试数据data=[(i, '测试数据') for i in range(10000)]

开始时间start_time=time.time()

执行写入操作cursor.executemany(INSERT INTO test_table (id, data) VALUES (%s, %s), data)

提交事务conn.commit()

结束时间end_time=time.time()

打印耗时print(写入耗时:{:.2f}秒.format(end_time - start_time))

关闭游标和连接cursor.close()conn.close()

测试结果:- 在测试环境中,写入10000条数据耗时约2.5秒。

总结:本文通过Python测试MySQL写入性能,分享了一段完整代码,并提供了测试结果。希望对有需要的伙伴有所帮助。

阅读全文