简体   繁体   English

为什么快速 API 需要 10 分钟以上才能将 100,000 行插入 SQL 数据库

[英]Why does Fast API take upwards of 10 minutes to insert 100,000 rows into a SQL database

I've tried using SqlAlchemy, as well as raw mysql.connector here, but commiting an insert into a SQL database from FastAPI takes forever.我已经尝试在这里使用 SqlAlchemy 以及原始 mysql.connector,但是从 FastAPI 向 SQL 数据库提交插入需要永远。

I wanted to make sure it wasn't just my DB, so I tried it on a local script and it ran in a couple seconds.我想确保它不仅仅是我的数据库,所以我在本地脚本上尝试了它,它在几秒钟内运行。

How can I work with FastAPI to make this query possible?我怎样才能使用 FastAPI 使这个查询成为可能?

Thanks!谢谢!

''' '''

@router.post('/')
def postStockData(data:List[pydanticModels.StockPrices], raw_db = Depends(get_raw_db)):
  
    cursor = raw_db[0]
    cnxn = raw_db[1]

    # i = 0
    # for row in data:
    #   if i % 10 == 0:
    #     print(i)
    #     db.flush()
    #   i += 1
    #   db_pricing = models.StockPricing(**row.dict())
    #   db.add(db_pricing)
    # db.commit()
    SQL = "INSERT INTO " + models.StockPricing.__tablename__ + " VALUES (%s, %s, %s)"
    print(SQL)

    valsToInsert = []
    for row in data:
      rowD = row.dict()
      valsToInsert.append((rowD['date'], rowD['symbol'], rowD['value']))
    cursor.executemany(SQL, valsToInsert)
    cnxn.commit()

    return {'message':'Pricing Updated'}

''' '''

You are killing performances because you try a " RBAR " approach which is not suitable in RDBMS... You use a loop and execute an SQL INSERT of only one row... When the RDBMS is facing a query, the sequence of execution is the following:您正在扼杀表演,因为您尝试了一种不适合 RDBMS 的“ RBAR ”方法...您使用循环并执行 SQL INSERT 只有一行...当 RDBMS 面临查询时,执行顺序是以下:

  • does the user that throw the query be authenticate?抛出查询的用户是否经过身份验证?
  • parsing the string to verify the syntax解析字符串以验证语法
  • looking for metadata (tables, columns, datatypes...)寻找元数据(表、列、数据类型...)
  • analyzing which operations on tables and columns this user is granted分析授予该用户对表和列的哪些操作
  • creating an execution plan to sequences all the operations needed for the query创建执行计划以对查询所需的所有操作进行排序
  • setting up lock for concurrency设置并发锁
  • executing the query (inserting only 1 row)执行查询(仅插入 1 行)
  • throw back an error or a OK message抛出错误或 OK 消息

Every steps consumes time... and your are all theses steps 100 000 times because of your loop.每个步骤都消耗时间……由于您的循环,您的所有这些步骤都执行了 100 000 次。

Usually when inserting in a table many rows, there just one query to do even if the INSERT concerns 10000000000 rows from a file !通常当在表中插入多行时,即使 INSERT 涉及文件中的 10000000000 行,也只需执行一个查询!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Cassandra数据库读取数据的速度太慢?想要在不到10秒的时间内读取100,000行 - Why is my Cassandra Database too slow in reading data? Want to read 100,000 rows in less than 10s 如何在mySQL中的单个查询中插入100,000条记录 - How to insert 100,000 records in a single query in mySQL 提取100,000行而不在MySQL中停滞 - Extracting 100,000 rows without stalling in MySQL 使用大量表备份mysql数据库(> 100,000) - Backup a mysql database with a large number of tables (>100,000) 如何有效地更新100,000条记录MySQL数据库 - How to update 100,000 record MySQL database efficiently 哪个数据类型用于在 varchar 以外的列中插入从 10,000 到 100,000 的数据 - which datatype is used for inserting the data from 10,000 to 100,000 in a column other than varchar 100,000条条目的mysql性能估计 - mysql performance estimate for 100,000 entries MySQL:在表InnoDB中设置大约100,000行的列索引所需的时间? - MySQL: time needed to set up index for column in the table InnoDB of about 100,000 rows? 当结果包含~100,000行时,mysqli_query停止执行php - mysqli_query stops php execution when result contains ~100,000 rows 在数据库中超过1亿行的10,000个设备上查找最新同步时出现问题 - Trouble finding most recent sync on 10,000 devices in database with over 100 million rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM