search
HomeDatabaseMysql TutorialHow to Bulk Upload CSV file data into MySql Table? A very fast way using LOAD DATA.

? Introduction

Are you still using "for" or "while" loops to iterate over your rows and insert them in your database? Do you still writing separate code for reading .csv file and the uploading it to your MySQL database?

Say "NO" to linear logic with the "LOAD DATA" statement provided by the MySQL. Prepare yourself for changing your code to both minimize the lines, and increase the performance drastically.

MySql provides the LOAD DATA statement, which is a very high speed way to read the data from text files and insert it into table with very minimal code.

? What is the LOAD DATA INFILE Statement?

As per the official docs of MySQL states, the LOAD DATA statement is used to Read Data from the file in a very fast manner and you can insert this data in your table with one single query, instead of hitting your db multiple times with "INSERT INTO" query.

MySQL also provides a statement -> "SELECT…INTO OUTFILE", which does exactly opposite of what "LOAD DATA" does, which is to read the data from the table to file.

mysqlimport utility provided by MySql internally calls the LOAD DATA statement on the server to import the data.

? How to Use it?

Above gist snippet shows the generic load data statements. All the available options are mentioned in the code above.

Lets now focus on a simple example of simply reading a .csv file and inserting it's rows into a compatible table.


A simple example :

LOAD DATA INFILE ‘data.csv’ INTO TABLE db.my_table

A birds-eye example with all essential options :

Needless to say that your CSV file should be properly formatted for this statement to work?.

And voila, using the above statement, your .csv with more than 100 thousands rows will be imported into your table in seconds.

Now, some important things to note:


The above statement runs at the MySql level, hence the source files are copied from client to server in order to import them.

This raises some security issues, we need to make sure the client-server connection is secured while using this code.

In MySql 8.0, the capability to use LOCAL is set as False by default. As your server and client should be configured to have LOCAL permited, some of you might get a permission error.

In case of permission error, we need to override it by enabling the
local_infile

:
SET GLOBAL local_infile = true;

Note: Overriding this flag is not a security solution but rather an acknowledgement for accepting the risks, you can refer this documentation for more information.

?‍? Conclusion

MySql LOAD DATA statement is used to read files within very less time.

You can use this code with any backend API to provide the source file.

Source file can be any text file, we have used .csv in this example.

LOAD DATA LOCAL

copies the source file to your server via MySql hence a security measures on the server side should be implemented.

mysqlimport

utility uses the LOAD DATA statement internally. You can ignore the headers in CSV by adding

IGNORE 1 LINES

in the statement.How to Bulk Upload CSV file data into MySql Table? A very fast way using LOAD DATA.

? Reference

Official Docs

The above is the detailed content of How to Bulk Upload CSV file data into MySql Table? A very fast way using LOAD DATA.. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimizing MySQL for Gaming Applications and LeaderboardsOptimizing MySQL for Gaming Applications and LeaderboardsJul 29, 2025 am 03:43 AM

TooptimizeMySQLforgamingapplicationsandleaderboards,useefficientdatatypesandindexing,implementcachinglayers,optimizequeries,andhandlewritesefficiently.First,chooseappropriatedatatypeslikeINTandUNSIGNED,andcreatecompositeindexessuchas(scoreDESC,update

Managing MySQL User Privileges and Role-Based Access ControlManaging MySQL User Privileges and Role-Based Access ControlJul 29, 2025 am 03:43 AM

To create a MySQL user and grant permissions, you can use the CREATEUSER and GRANT commands; 1. Create a user: CREATEUSER'app_user'@'localhost'IDENTIFIEDBY'secure_password'; 2. Grant permissions: GRANTSELECT,INSERTONmydb.mytableTO'app_user'@'localhost'; 3. Use roles to simplify management: first create a role and authorize it, and then assign the role to the user, such as CREATEROLE'data_reader'; GRANTSELECTONmydb.*TO'dat

Understanding MySQL character set and collation issuesUnderstanding MySQL character set and collation issuesJul 29, 2025 am 03:42 AM

MySQL character set and collation issues mainly affect data storage, comparison and migration. 1. The character set setting level includes server, database, table, and fields, and priority is overwritten step by step. It is recommended to explicitly specify the character set when creating to avoid default value dependence; 2. Sorting rules such as utf8mb4_unicode_ci (case insensitive) and utf8mb4_bin (case insensitive) affect the query results. Please pay attention to index and constraint behavior when modifying; 3. Data transmission must ensure that the character set is consistent in the connection stage, and the program configuration should include character set parameters such as SETNAMES or JDBC configuration; 4. When migrating existing data, the character set cannot be modified directly. It needs to be exported and then transcoded before importing to avoid the risk of garbled code. Unified standards in the early stage of planning can be reduced

Automating MySQL Patching and MaintenanceAutomating MySQL Patching and MaintenanceJul 29, 2025 am 03:31 AM

To automate MySQL patching and maintenance, the key is to establish stable and repeatable processes. First, choose the appropriate tools, such as Ansible, suitable for lightweight deployment, Puppet and Chef are more suitable for complex environments, and ensure that they support version control and rollback mechanisms, combining cron or CI/CD to improve execution efficiency; second, clarify the scope of automation tasks, including daily table optimization, weekly backup verification, monthly slow query analysis, and log cleaning, and add exception detection and status records; patch updates must be verified in the test environment first, and then write scripts to go online, backup data before update, check logs after update, and the source of the patch must be trustworthy; finally, the monitoring and notification mechanism must be configured, using Prometheus, Grafana,

Troubleshooting MySQL Temp Table PerformanceTroubleshooting MySQL Temp Table PerformanceJul 29, 2025 am 03:26 AM

The performance problems of MySQL temporary tables mainly stem from disk drop-down, improper sorting operations, unreasonable explicit use, and disk IO pressure. 1. Temporary tables are preferred to be created in memory, but big data types or characteristics will cause drops. You can judge and optimize fields, parameters and indexes by monitoring Created\_tmp\_disk\_tables; 2. Sorting or grouping without indexes will cause temporary table inflation. Indexes should be added, column participation should be reduced, and join order should be optimized; 3. Explicit temporary tables should pay attention to connection validity and reuse to avoid frequent creation; 4. Disk temporary tables may cause IO and space problems, tmpdir should be monitored, SSD should be used and the number of temporary tables should be limited.

Implementing MySQL Database Incident Response PlansImplementing MySQL Database Incident Response PlansJul 29, 2025 am 03:05 AM

Developing a MySQL incident response plan can reduce failure losses, including clarifying the types and priorities of failures, formulating response processes and division of labor, regular drills and update plans, and preparing tools and documents. Common failures such as connection failure, master-slave synchronization exception, slow query, insufficient disk space, errors in permissions, etc., should be processed in accordance with the urgency level; the response process should include event discovery → preliminary judgment → notification → location → repair → review, and clarify the responsible person; plan should be practiced regularly and documents should be updated according to system changes; monitoring tools, backup scripts, log analysis tools and complete documents should be prepared in advance to ensure efficient and timely response.

Advanced SQL Features in MySQL 8.0: A Deep DiveAdvanced SQL Features in MySQL 8.0: A Deep DiveJul 29, 2025 am 03:04 AM

MySQL 8.0 introduces multiple advanced SQL functions, which significantly improves the efficiency and expressive capabilities of data operations. 1. Common table expressions (CTEs) replace complex nested subqueries, making the logic clearer and more maintainable; 2. Recursive queries combine with CTEs to support hierarchical data processing, such as organizational structure or classification tree traversal; 3. Window functions realize inter-row calculation without grouping and aggregation, and are suitable for scenarios such as ranking and cumulative statistics; 4. JSON enhancement function optimizes the query and index of JSON fields, improving semi-structured data processing performance. These features not only simplify SQL writing, but also enhance the functional boundaries of the database.

Troubleshooting MySQL Index CorruptionTroubleshooting MySQL Index CorruptionJul 29, 2025 am 03:03 AM

MySQL index corruption may cause query failure, performance degradation or service interruption, and it needs to be handled in time. 1. Confirm the problem: the query is slow, the EXPLAIN is not indexed, the error log prompts the index is damaged, and CHECKTABLE returns an exception; 2. Repair method: InnoDB table uses OPTIMIZETABLE or export and import, and MyISAM table uses REPAIRTABLE; 3. Preventive measures: Regular CHECKTABLE, use stable storage, pay attention to version compatibility, set monitoring alarms, and reduce frequent write operations.

See all articles

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft