简体   繁体   English

如何将超过100000条记录导入到mysql数据库中?

[英]How to import more than 100000 records into a mysql database?

I have a sql dump file. 我有一个SQL转储文件。 which has 185458 records in the table. 该表中有185458条记录。 for example 例如

INSERT INTO `cities` (`city_id`, `city_name`, `countryId`, `stateid`, `countryCode`, `latitude`, `longitude`, `zip_code`) VALUES
(2, 'Djelfa', 67, 1, 'DZA', 34.67, 3.25, ''),
(3, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048'),
...
...
...
(185452, 'Bosendurnbach', 17, 771, 'AUT', 48.5, 15.77, ''),
(185453, 'Môlay', 79, 937, 'FRA', 47.73, 3.94, ''),
(185454, 'Miloszyce', 183, 422, 'POL', 51.05, 17.31, ''),
(185455, 'Lovce', 212, 698, 'SVK', 48.45, 18.37, ''),
(185456, 'Winchester', 174, 74, 'NZL', -44.2, 171.28, ''),
(185457, 'Wohlde', 62, 402, 'DEU', 54.4, 9.3, ''),
(185458, 'Chiavazza', 115, 2, 'ITA', 45.58, 8.07, '');

After long time i'm getting this error 长时间后,我收到此错误

Fatal error: Maximum execution time of 300 seconds exceeded in C:\\xampp\\phpMyAdmin\\libraries\\dbi\\DBIMysqli.class.php on line 290 致命错误:第290行的C:\\ xampp \\ phpMyAdmin \\ libraries \\ dbi \\ DBIMysqli.class.php中超过了300秒的最大执行时间

How can i import ? 如何导入?

The error message is quite clear and points you into what the issue is: the limitation of execution time in your php environment. 错误消息很清楚,并指出了问题所在:php环境中执行时间的限制。 So some approaches to solve this are obvious: 因此,解决此问题的一些方法很明显:

  1. increase the time limit of your environment 增加环境的时间限制

You can do that by either raising the limit in your php configuration or, if permitted, by dynamically increasing it inside your import script. 您可以通过提高php配置中的限制或在导入脚本中动态增加限制(如果允许)来实现。 So something like ini_set('max_execution_time', 3000) . 所以像ini_set('max_execution_time', 3000) Both options are documented. 这两个选项均已记录。 The php documentation should always be the first location where you should start to look for an answer to such a question. php文档应该始终是您应该开始寻找此类问题答案的第一个位置。

  1. use a different php environment 使用不同的php环境

Typically such limitation is chosen for a web environment to reduce the risks of serving requests from anyone out there. 通常,为Web环境选择这种限制,以减少服务来自任何人的请求的风险。 However nothing speaks against using a different configuration for another environment. 但是,对于在另一个环境中使用不同的配置,没有什么可说的。 Import jobs are typically not processed by using web requests, but by using php in the command line (CLI). 导入作业通常通过使用Web请求来处理,而是通过在命令行(CLI)中使用php来处理。 For such typically a separate configuration is used. 为此,通常使用单独的配置。 Again, this is documented. 再次说明。 That is what you can use here to configure both environments different from each other according to your needs. 这就是您可以在此处根据需要配置互不相同的两种环境的方法。 However for this you need access to php on CLI. 但是为此,您需要在CLI上访问php。 That is no issue on your own system, but usually not available on a cheap web hosting service. 在您自己的系统上,这不是问题,但通常在廉价的Web托管服务上不可用。

  1. split your import into smaller chunks 将导入分成较小的块

Since the data you import is stored in a sql file, so a simple text file, you can use any ordinary text editor to modify that file. 由于您导入的数据存储在一个sql文件(即简单的文本文件)中,因此您可以使用任何普通的文本编辑器来修改该文件。 Note: a text editor, not a word processor. 注意:文本编辑器, 而不是文字处理器。 You can split the big INSERT statement contained in there into several chunks. 您可以将其中包含的大INSERT语句分成几个块。 The syntax is quite obvious. 语法非常明显。

  1. use many separate insert statements instead of one big one 使用许多单独的插入语句,而不是一个大的插入语句

Depending on the tool you use to create that dump file you are trying to import now you have an option to create the dump such that it uses many separate INSERT statements (one for each row) instead of one big, combined one. 现在,根据要尝试导入的用于创建该转储文件的工具的不同,您可以选择创建转储,以使它使用许多单独的INSERT语句(每行一个)而不是一个大的合并的语句。 mysqldump for example offers the --skip-extended-insert flag for this. 例如, mysqldump为此提供了--skip-extended-insert标志。 With such a dump file it is trivial to split the import into several smaller chunks by simply splitting the file. 使用这样的转储文件,只需简单地将文件拆分即可将导入拆分为几个较小的块是很简单的。

  1. bypass php for the import altogether 完全绕过php导入

If you have a direct access to your database server (MySQL in this case), then you can simply interact directly with it instead of using the phpMyAdmin tool inbetween. 如果您可以直接访问数据库服务器(在这种情况下为MySQL),则可以直接与其直接交互,而不必在两者之间使用phpMyAdmin工具。 You can simply load your dumpfile directly by means of MySQLs source command. 您可以直接通过MySQLs source命令直接加载转储文件。 That way you are completely independent from the php limitations. 这样,您就完全不受php限制。

Try combining insert values in group of 10,000

For e.g.

INSERT INTO `cities` (`city_id`, `city_name`, `countryId`, `stateid`, `countryCode`, `latitude`, `longitude`, `zip_code`) VALUES
(2, 'Djelfa', 67, 1, 'DZA', 34.67, 3.25, ''),
(3, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048'),


...
...
(10000, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048');
INSERT INTO `cities` (`city_id`, `city_name`, `countryId`, `stateid`, `countryCode`, `latitude`, `longitude`, `zip_code`) VALUES

(10001, 'Valenza', 115, 2, 'ITA', 45.02, 8.63, '15048'),
...
(185452, 'Bosendurnbach', 17, 771, 'AUT', 48.5, 15.77, ''),
(185453, 'Môlay', 79, 937, 'FRA', 47.73, 3.94, ''),
(185454, 'Miloszyce', 183, 422, 'POL', 51.05, 17.31, ''),
(185455, 'Lovce', 212, 698, 'SVK', 48.45, 18.37, ''),
(185456, 'Winchester', 174, 74, 'NZL', -44.2, 171.28, ''),
(185457, 'Wohlde', 62, 402, 'DEU', 54.4, 9.3, ''),
(185458, 'Chiavazza', 115, 2, 'ITA', 45.58, 8.07, '');


This will definitely improve the performance

You have problem with maximum execution time. 您有最长执行时间的问题。
You should follow this link: Increase execution time 您应该点击以下链接: 增加执行时间

in case if link is not working, This is the same from Link povided. 如果链接不起作用,则与链接显示的内容相同。
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Place this at the top of your PHP script. 将其放在您的PHP脚本的顶部。


Hope this helps you. 希望这对您有所帮助。

I don't think this is an issue with MySQL, its an issue with PHP configuration. 我不认为这是MySQL的问题,不是PHP配置的问题。 You can resolve this from command prompt by importing SQL file directly to Mysql thus eliminating PHP from the picture. 您可以从命令提示符处解决此问题,方法是将SQL文件直接导入Mysql,从而从图中删除PHP。

Since you are using Windows, 由于您使用的是Windows,

C:\xampp\mysql\bin\mysql -u {username} -p {databasename} < file_name.sql

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

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