简体   繁体   English

使用C#在mysql中插入大量流数据

[英]Insert mass streaming data in mysql using C#

I use Mysql and I need to insert mass data. 我使用Mysql,我需要插入海量数据。 The data is streamed to my server in the form of a list of 5k rows. 数据以5k行列表的形式流式传输到我的服务器。 I need to insert more than 3k requests, that means 3k request * 5k rows = 15 000 000 rows. 我需要插入超过3k个请求,这意味着3k个请求* 5k行= 15000000行。

What I did was used create threads and insert using those threads, as the data come in packets of 5k in an async event. 我所做的工作是创建线程并使用这些线程进行插入,因为数据在异步事件中以5k的数据包形式传入。 The data response is generated on my request. 数据响应是根据我的请求生成的。

What is the best possible way to do it, keeping this scenario in mind? 牢记这种情况的最佳方法是什么?

ThreadPooling for thread managment or simple multithreaded applition and will threads benifit in insertion as I need to insert in a single table (Innodb engine) ThreadPooling用于线程管理或简单的多线程应用,并且当我需要在单个表中插入时将使线程受益于插入(Innodb引擎)

You can cache incoming requests on a server. 您可以在服务器上缓存传入的请求。 Keep some buffered data in-memory until you get N requests (which you can fine-tune later). 将一些缓冲的数据保留在内存中,直到获得N个请求(以后可以对其进行微调)。 Once you get those you just flush data into MySql using some bulk insert routine. 一旦获得这些,就可以使用一些大容量插入例程将数据刷新到MySql中。 It is generally much faster to do one big insert than many small ones. 通常,一个大插入比许多小插入要快得多。

You can use ConcurrentBag class to keep data on the server. 您可以使用ConcurrentBag类将数据保留在服务器上。 This is a thread-safe collection. 这是线程安全的集合。

Additionally, you may need to expire cache based on time. 此外,您可能需要根据时间使缓存过期。 This will cover the case where you get some requests n < N and then a client just stops sending data. 这将涵盖以下情况:您收到一些请求n <N ,然后客户端只是停止发送数据。 You would want to flush it anyways and not wait forever until next upcoming requests fully fill the cache. 您可能想一直刷新它,而不是永远等待直到下一个即将到来的请求完全填满缓存。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM