Cara menggunakan mysql insert ignore performance

melakukan sebuah insert data kedalam database sangatlah sering dilakukan. tapi bagaimana jika yang di insert tersebut adalah data yang ribuan tapi ada beberapa code yang menjadi key. jika dipaksakan untuk melakukan insertnya maka akan terjadi error duplicate bukan. pada perintah sql terdapat sebuah perintah yang mana jika terdapat code yang sama maka akan dilakukan skipp insert. perintah tersebut kita kenal yaitu insert ignore.

tapi bagaimana jika melakukan insert ignore tersebut kita gunakan pada Framework Codeigniter? sampai pada artikel ini dibuat, codeigniter belum menambahkan perintah insert ignore tersebut. kita doain saja semoga untuk kedapannya Framework Codeigniter melakukan update tersebut.

untuk mengakali supaya perintah insert ignore dapat dilakukan pada codeigniter maka kita perlu menfaatkan perintah str_replace yang berguna untuk melakukan replcae perintah "INSERT" menjadi "INSERT IGNORE" untuk penjelasannya dapat dilihat pada pengalan script berikut ini :

When we want to insert the data into the tables of the MySQL database, we use the INSERT statement for performing this operation. Mysql provides the facility to add IGNORE keyword in the syntax of the INSERT statement. The usage of the INSERT IGNORE statement is always considered a good practice over the INSERT statement. This is because the INSERT IGNORE statement handles the error that arrives while the addition of the duplicate record and preventing the inconsistency and redundancy of the records in the tables of Mysql.

In this article, we will learn about the general syntax of INSERT IGNORE statement, its working, how the strict mode affects the working of INSERT IGNORE statement and learn the usage with the help of few examples to make the concept more clear.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

How INSERT IGNORE works in MySQL?

When we try to insert multiple records in a particular table of the Mysql database using the INSERT statement and due to some reason there is the occurrence of the error then MySQL will terminate the execution of the query and give the error without inserting any rows in the table that we tried to insert. But when we use INSERT IGNORE instead of just inserting statement then Mysql will give a warning and inserting all the records that were correct leaving and excluding the rows that caused the error.

Syntax

The syntax of the INSERT IGNORE statement is as follows –

All in One Data Science Bundle(360+ Courses, 50+ projects)

Price
View Courses

360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,112 ratings)

INSERT IGNORE INTO table(list_of_columns)
VALUES(record1),
(record2),

Where list_of_columns are the comma-separated names of the column that you wish to insert in the record and the record1,record2,.. are the values of the columns that you have mentioned in the list_of_columns in the same order as they have been mentioned in the list.

Examples to Implement MySQL INSERT IGNORE

Let us create one table named developers that contain one auto_incremented primary key column named id, one column that has a unique constraint on it named name and other related fields that are required to store using the following query statement –

Code:

CREATE TABLE `developers` (
`developer_id` int(11) AUTO_INCREMENT PRIMARY KEY,
`team_id` int(11) NOT NULL,
`name` varchar(100) DEFAULT NULL UNIQUE,
`position` varchar(100) DEFAULT NULL,
`technology` varchar(100) DEFAULT NULL,
`salary` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Output:

Now, we will try to insert multiple records having a single record value repeated for the name column on which we have defined the unique constraint to avoid insertion of records with the same name. As inserting such records is against the rules and breaking the constraint MySQL will issue an error saying the record with a duplicate value of name column cannot be inserted. In this case, all other columns that we have mentioned in our insert query were correct as per all the constraints and restrictions were also not inserted. We can try executing the following query statement –

Code:

INSERT INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Payal', 'Developer', 'Angular', 30000),
( 1, 'Heena', 'Developer', 'Angular', 10000),
( 3, 'Vishnu', 'Manager', 'Maven', 25000),
( 3, 'Rahul', 'Support', 'Digital Marketing', 15000),
( 3, 'Siddhesh', 'Tester', 'Maven', 20000),
( 7, 'Siddharth', 'Manager', 'Java', 25000),
( 4, 'Brahma', 'Developer', 'Digital Marketing', 30000),
( 1, 'Arjun', 'Tester', 'Angular', 19000),
( 2, 'Nitin', 'Developer', 'MySQL', 20000),
( 2, 'Ramesh', 'Administrator', 'MySQL', 30000),
( 2, 'Rohan', 'Admin', NULL, 20000),
( 2, 'Raj', 'Designer', NULL, 30000),
( 1, 'Raj', 'Manager', NULL, 56000);

Output:

Let us retrieve the records of the developer’s table by using the following query statement

Code:

SELECT * FROM developers;

Output:

We can see that none of the records got inserted even though other than the record with the Raj name were correct. Let us now try to insert the records by using the INSERT IGNORE statement instead of the INSERT statement. Our query statement will be as follows –

Code:

INSERT IGNORE INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Payal', 'Developer', 'Angular', 30000),
( 1, 'Heena', 'Developer', 'Angular', 10000),
( 3, 'Vishnu', 'Manager', 'Maven', 25000),
( 3, 'Rahul', 'Support', 'Digital Marketing', 15000),
( 3, 'Siddhesh', 'Tester', 'Maven', 20000),
( 7, 'Siddharth', 'Manager', 'Java', 25000),
( 4, 'Brahma', 'Developer', 'Digital Marketing', 30000),
( 1, 'Arjun', 'Tester', 'Angular', 19000),
( 2, 'Nitin', 'Developer', 'MySQL', 20000),
( 2, 'Ramesh', 'Administrator', 'MySQL', 30000),
( 2, 'Rohan', 'Admin', NULL, 20000),
( 2, 'Raj', 'Designer', NULL, 30000),
( 1, 'Raj', 'Manager', NULL, 56000);

Output:

We can see that 12 rows were affected and the query got executed with one warning. To see the warning we can execute the following command –

Code:

SHOW WARNINGS;

Output:

The warning shows that there was one record with the name Raj that had duplicate value for the name column having a unique constraint on it. Let us retrieve the records of the developers table and check which records got inserted using the following query –

Code:

SELECT * FROM developers;

Output:

We can see that 12 rows got inserted when in reality we tried to insert 13 rows but as Raj record was duplicated all the remaining rows got inserted successfully. Also, note that the autoincremented column of developer id has got values inserted beginning from 14,15 and so on because the previous 13 records that we tried to insert using just INSERT query caused an error and then all the records that were being inserted got rollbacked leaving the sequence value associated with auto-incremented column updated to 14.

Value Exceeding the Range of the Column

When we try to insert the value that exceeds the limit of the column size defined then the INSERT statement results in the error. However, usage of INSERT IGNORE inserts the truncated value and gives the warning saying that the range exceeded and hence value has been truncated for insertion. Consider the following example:

Length of salary columns is 11 and we try inserting 12- digit number using INSERT statement gives following output –

Code:

INSERT INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Pihu', 'Developer', 'Angular', 300000000000);

Output:

Let’s try using INSERT IGNORE statement –

Code:

INSERT IGNORE INTO `developers` (`team_id`, `name`, `position`, `technology`, `salary`) VALUES
( 1, 'Pihu', 'Developer', 'Angular', 300000000000);

Output:

with warning –

Code:

SHOW WARNINGS;

Output:

Let us retrieve and check the inserted value –

Code:

CREATE TABLE `developers` (
`developer_id` int(11) AUTO_INCREMENT PRIMARY KEY,
`team_id` int(11) NOT NULL,
`name` varchar(100) DEFAULT NULL UNIQUE,
`position` varchar(100) DEFAULT NULL,
`technology` varchar(100) DEFAULT NULL,
`salary` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;0

Output:

Conclusion

Using the INSERT IGNORE statement instead of just inserting statements is always a good practice as Mysql tries to adjust the values to arrange them in the correct format and inserts the correct records excluding the one that can cause an error.

Recommended Articles

This is a guide to MySQL INSERT IGNORE. Here we discuss an introduction to MySQL INSERT IGNORE, syntax, how does it works with examples. You can also go through our other related articles to learn more –

Postingan terbaru

LIHAT SEMUA