请问从Excel导入50W条数据进Mysql,会出现什么问题,如何解决?

首先回答第一个问题,50W条应该算是一个大数据了,POI数据导入过程应该是先把数据读取到内存,在内存中进行编辑后再insert到数据库,这样的话问题就出现了——内存溢出。

那我们怎么解决这个问题呢?官方给出了解决方案

1、分段缓存刷新

官方demo: http://poi.apache.org/spreadsheet/how-to.html#sxssf

1.1、文档说明

SXSSFWorkbook defaults to using inline strings instead of a shared strings table. This is very efficient, since no document content needs to be kept in memory, but is also known to produce documents that are incompatible with some clients. With shared strings enabled all unique strings in the document has to be kept in memory. Depending on your document content this could use a lot more resources than with shared strings disabled.

Please note that there are still things that still may consume a large amount of memory based on which features you are using, e.g. merged regions, hyperlinks, comments, ... are still only stored in memory and thus may require a lot of memory if used extensively.

Carefully review your memory budget and compatibility needs before deciding whether to enable shared strings or not.

The example below writes a sheet with a window of 100 rows. When the row count reaches 101, the row with rownum=0 is flushed to disk and removed from memory, when rownum reaches 102 then the row with rownum=1 is flushed, etc.

1.2、代码示例