Better to run 2 sql queries or 1 and deal with a duplicate result set?

I have a webpage with an identifier as a variable GETthat I need to pull out name, cityand statefor that identifier (stored in 1 table), as well as any data associated with it (stored in another table).

These are the results of a single query:

SELECT
    info.name, info.city, info.state,
    data.data1, data.data2, data.data3, data.data4
FROM
    data_table data,
    info_table info
WHERE
    data.id = 12345 AND info.id = data.id

name | city | state | data1 | data2 | data3 | data4
---------------------------------------------------
test | temp | AL    | 12    | 9     | 1     | 14
test | temp | AL    | 63    | 8     | 1     | 6
test | temp | AL    | 46    | 66    | 1     | 723
test | temp | AL    | 7     | 5     | 2     | 99
test | temp | AL    | 4     | 2     | 3     | 0
test | temp | AL    | 2     | 11    | 1     | 1

But the data for the column name, city, stateeverything will be the same for each row, so I might as well do it with two queries and return the "right" amount of data (but obviously twice as much time to communicate with the server):

SELECT
    info.name, info.city, info.state,
FROM
    info_table info
WHERE
    info.id = 12345

name | city | state
-------------------
test | temp | AL   

... and ...

SELECT
    data.data1, data.data2, data.data3, data.data4
FROM
    data_table data,
WHERE
    data.id = 12345

data1 | data2 | data3 | data4
-----------------------------
12    | 9     | 1     | 14
63    | 8     | 1     | 6
46    | 66    | 1     | 723
7     | 5     | 2     | 99
4     | 2     | 3     | 0
2     | 11    | 1     | 1

, , 2 , ? - () , , , ?

, , total server communication time / 2 > time to transmit extra data, ?

+4
5

JOIN , JOIN .

, , , .

, , . , , , JOIN .

+5

, , .

"" ( MySQL) - . SQL- ... MySQL . MySQL , ( , ..), , ( , , ) ). MySQL , ( , ..

( MySQL) , .

resulset. , .

, , , , , ... .

+2

2 :

  • .

:

, . , 2 .

, , . , .

, , .

0

, . WHERE. INNER JOIN .

I recommend two queries because it is a parent record with several children, and you probably want to show a city or country, and then a list of other data.

-1
source

You should just use one query, but use JOIN to make sure you just get the data you need. This link will show you how all the different types of connections work.

http://www.sitepoint.com/understanding-sql-joins-mysql-database/

-2
source

Source: https://habr.com/ru/post/1525863/


All Articles