MySQL query in a loop vs using a SQL join

前端 未结 2 433
我在风中等你
我在风中等你 2021-01-12 06:05

I\'m having an inner debate at my company about looping queries in this matter:

$sql = \"
  SELECT foreign_key
  FROM t1\";

foreach(fetchAll($sql) as $row)
         


        
相关标签:
2条回答
  • 2021-01-12 06:24

    The join method is generally considered better, if only because it reduces the overhead of sending queries back and forth to the database.

    If you have appropriate indexes on the tables, then the underlying performance of the two methods will be similar. That is, both methods will use appropriate indexes to fetch the results.

    From a database perspective, the join method is far superior. It consolidates the data logic in one place, making the code more transparent. It also allows the database to make optimizations that might not be apparent in application code.

    0 讨论(0)
  • 2021-01-12 06:28

    Because of driver overhead, a loop is far less efficient

    This is similar to another question I answered, but different enough not to cv. My full answer is here but I'll summarize the main points:

    Whenever you make a connection to a database, there are three steps taken:

    1. A connection to the database is established.
    2. A query, or multiple queries, to the database is executed.
    3. Data is returned for processing.

    Using a loop structure, you will end up generating additional overhead with driver requests, where you will have a request and a return per loop cycle rather than a single request and single return. Even if the looped queries do not take any longer than the single large query (this is very unlikely as MySQL internals have a lot of shortcuts built in to prevent using a full repetitive loop), you will still find that the single query is faster on driver overhead.

    Using a loop without TRANSACTIONS, you will also find that you run into relational data integrity issues where other operations affect the data you're iterating between loop cycles. Using transactions, again, increases overhead because the database has to maintain two persistent states.

    0 讨论(0)
提交回复
热议问题