D
D
Danil2016-04-06 14:07:38
MySQL
Danil, 2016-04-06 14:07:38

Is it possible to query the database in a loop?

I have an array of elements and I need to find out something about each of them, I do it in a loop and in the body of the loop I make queries to the database (about 100-200 iterations). Is this bad or can it be done?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Peter, 2016-04-06
@petermzg

You can, but it's bad for performance.
Consider using IN operator in sql query

I
Ilya, 2016-04-06
@geeek

This is fundamentally wrong. It is possible to form a line with a query in a loop, and then execute 1 (ONE!) query. Or to form XML and to transfer it in xn \function, besides that there was 1 request. You can't execute queries in a loop (well, or you can if your goal is to shit-code).

D
Dmitry Kovalsky, 2016-04-06
@dmitryKovalskiy

The plural in the word "requests" scares me a little. More than one per loop iteration? Such behavior should be avoided as much as possible. If you have a primitive script of the simplest select from one table, then it's not fatal. But at the very least complex scripts are better not to be called by a cycle. Alternatively, glue all the parameters into one string variable through a separator, and on the database server, cut the string into separate components and execute one tricky script.

I
Ilya, 2016-04-06
@rpsv

If queries are for one table, then it is probably better to make one query, and then parse it (fetch, for example, so as not to accept the whole mass at once).
--
Or you can think about caching, so as not to constantly pull the database like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question