1

I have working on two way to fetch datas from multiple tables.

  1. MYSQL JOIN query
  2. PHP foreach loop query

Can you provide the idea of which one work faster?

Thanks in Advance.

  • 2
    Possible duplicate of [MySQL query in a loop vs using a SQL join](https://stackoverflow.com/questions/18404913/mysql-query-in-a-loop-vs-using-a-sql-join) – AymDev Jul 17 '18 at 10:00
  • 2
    If you have been working on it, then show us! – KIKO Software Jul 17 '18 at 10:00
  • 1
    1/ We have no idea how your data looks like, 2/ We have no idea how many data you have, 3/ Can you please show us an example of what you tried? The best way is to test it yourself so you will have an idea of what's faster ! But the less you send request to your database, the better it is I think. Send ONE query with some join is better than sending thousand of query in a foreach loop – Mickaël Leger Jul 17 '18 at 10:01

1 Answers1

3

If SQL can do the task, it can usually do it faster. Reasons:

  • Not shoveling data between client (PHP) and server (mysqld).
  • Not having to parse and optimize multiple queries.
  • More optimization tricks that you might not think of. (The flip of this is that you might not have the best indexes, etc on the queries.)
Rick James
  • 135,179
  • 13
  • 127
  • 222