G
G
gene40002015-12-14 10:42:38
PHP
gene4000, 2015-12-14 10:42:38

Which is better - one query with two JOINs or three queries?

When programming in PHP, the question arose: is it better to make a more complete query to get all the data from three tables at once, or to make three different queries?
In the first request by date, information containing two identifiers is selected, for which detailed data corresponding to these identifiers is pulled out.
That is, in one request, information can be duplicated in the returned data (in different lines, part of the data corresponding to the identifier can be repeated).
The query is unlikely to return more than a hundred rows, most likely up to twenty. Is there a noticeable performance difference when making three queries?
Simplified structure:

t1 [ time, t2_id, t3_id ]
t2 [ t2_id, t2_info ]
t3 [ t3_id, t3_info ]

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Entelis, 2015-12-14
@DmitriyEntelis

If the tables are conditionally large (> 1,000,000), and little data is expected in the query results ( < 1,000 ), it will be noticeably faster to make 3 queries of the form where id in (1,2,3)explicitly listing the necessary id in the queries

R
Robot, 2015-12-14
@iam_not_a_robot

It can be different, it is necessary to measure in each specific case separately.

D
Dmitry Kovalsky, 2015-12-14
@dmitryKovalskiy

With a small number of rows, JOIN is better. There is another question here - if you create a connection to the database after each request and then disconnect - there will be just ballast operations that can be avoided like 2 fingers.

V
Vladimir Chernyshev, 2015-12-14
@VolCh

In general, one is better. You need to start with it. When it starts to slow down, then only then start profiling, look at query plans and start optimizing. Premature optimization is evil.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question