2

I wonder if there are any advantage or disadvantage using multiples values in a single query, or whether is better to insert those values into a separate queries?

For example, supposed that I have the table tester_tbl which has 2 columns of type int both, id1 and id2 respectively and I want insert the values 2, 3, 4 to the column id2 and the value 1 to the column id1, something like this :

+-----+-----+
| id1 | id2 |
+-----+-----+
|   1 |   2 |
|   1 |   3 |
|   1 |   4 |
+-----+-----+

To accomplish it, what do you recommends me?, use a single query that inserted those values, so INSERT INTO tester_tbl (id1, id2) VALUES (1, 2), (1,3), (1,4);, or do multiples queries to insert those values, so

`INSERT INTO tester_tbl (id1, id2) VALUES (1, 2);`,
`INSERT INTO tester_tbl (id1, id2) VALUES (1, 3);`,
`INSERT INTO tester_tbl (id1, id2) VALUES (1, 4);`

If anyone can tell me a better way to do this, I'd be grateful.

Best regards.

0

3 Answers 3

3

According to the documentation, MySQL inserts with multiple values in one query is more efficient and faster.

Sign up to request clarification or add additional context in comments.

Comments

1

The first way is better. More efficient and faster, especially with many inserts. A lot faster. I speak from experience.

1 Comment

Thanks for your answer @Uncle Iroh, I'm grateful about you.
0

In my mind the only reason you might want to use separate queries, is if you needed to enforce transactional integrity on each one individually. Outside of having that need, use a single query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.