Which is faster / more efficient - lots of little MySQL queries or one big PHP array?

前端 未结 6 1008
离开以前
离开以前 2021-02-06 01:55

I have a PHP/MySQL based web application that has internationalization support by way of a MySQL table called language_strings with the string_id,

6条回答
  •  生来不讨喜
    2021-02-06 02:18

    There isn't an answer that isn't case sensitive. You can really look at it on a case by case statement. Having said that, the majority of the time, it will be quicker to get all the data in one query, pop it into an array or object and refer to it from there.

    The caveat is whether you can pull all your data that you need in one query as quickly as running the five individual ones. That is where the performance of the query itself comes into play.

    Sometimes a query that contains a subquery or two will actually be less time efficient than running a few queries individually.

    My suggestion is to test it out. Get a query together that gets all the data you need, see how long it takes to execute. Time each of the other five queries and see how long they take combined. If it is almost identical, stick the output into an array and that will be more efficient due to not having to make frequent connections to the database itself.

    If however, your combined query takes longer to return data (it might cause a full table scan instead of using indexes for example) then stick to individual ones.

    Lastly, if you are going to use the same data over and over - an array or object will win hands down every single time as accessing it will be much faster than getting it from a database.

提交回复
热议问题