Download the PHP package jotagp/insert-multiple without Composer
On this page you can find all versions of the php package jotagp/insert-multiple. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jotagp/insert-multiple
More information about jotagp/insert-multiple
Files in jotagp/insert-multiple
Package insert-multiple
Short Description A simple PHP package, to insert multiple records into a MySQL/MariaDB database.
License MIT
Informations about the package insert-multiple
INSERT-MULTIPLE
Sumary
- Introduction
1.1 What is
1.2 Why
1.3 Licence - Get Started
2.1 Dependences
2.2 Instalation
2.3 Usage
2.4 Methods
2.5 Exemple - Enjoy
1. Introduction
1.1 What is
This is a simple PHP package to insert and update multiple data into a MySQL/MariaDB database.
1.2 Why
This package was created with the aim of reducing data insertion time. To understand the magnitude of the problem, let's assume that we want to go through the items in a list and insert them into a database.
The example above works. However, when working with a large volume of data, traditional insertion is not a viable option. This is because it inserts a single record at a time. So, if you have 100,000 records, there will be 100,000 insertions and, consequently, 100,000 trips to the hard disk to persist this data, and this task will take a long time (of course, it depends on the size of your data). One "option" is to control the transaction from the database manually, something like:
However, this approach still does not definitively solve our problem, as the time gain is not significant. What to do then? Simple! make a multiple insert:
However (there's always a however, right?), there is a transaction limit allowed by the bank, and this limit is easily reached when a very extensive query is set up. And that, my friends, is where this package comes in. It will partition your values into N Multiple slots, according to your bank's capacity:
And it's that simple.
1.3 License
This code is licensed under the MIT license.
2. Get Start
2.1 Dependences
The library depends only on:
2.2 Instalation
Run the following command:
2.3 Usage
Include the dependences in your PHP project:
2.4 Methods
There are three possible methods:
push($associative_array)
This method works to include new values in the insert. Note that the expected argument is an associative array, where the index of this array must always refer to the attribute of the table in question. Attributes that you do not specify will be included with their respective default values.
exec()
This method partitions your insert into N multiple inserts, then run, always taking into account the amount allowed in a transaction by your database instance (max_allowed_package) . That is, in an insert of 100 thousand records, hypothetically speaking the function will create 10 multiple inserts, each with 10 thousand records.
config($associative_array)
This method allows you to edit some behavior of the object. Possible configurations (so far) are:
- update_if_exists: updates a record if a corresponding key already exists.
- fields_to_update: inside update_if_exists, you can specify which fields should be updated.
- concat_new_values: allows concatenating new values to existing ones.
- skip_if_already_exists: preserves the value that already exists, ignoring the new one.
- skip_if_new_is_empty: preserves the value that already exists, in case the new one is empty or null.
- insert_multiple: if false, insert records one row per time.
2.5 Example
Inserting 100 thousand random data into database. Consider the following data structure:
Then:
3. Enjoy
Thanks for making it this far. I hope that this package will help you to have more performance in your day.
I remain at your disposal if you need assistance or have any suggestions.
You can contact me on Linkedin.