简体   繁体   中英

Should I use join for simple relationships in MySQL

Is JOIN the right way (only way?) to add information from other tables?

Eg If I have these tables:

Employees

name | pay_table
--------------------
John | 1
Lisa | 2

Pay

Id  | Pay
---------------------
1   | 30000
2   | 50000

and I need the name and the pay in one table, would the best approach be to write something like this:

SELECT * FROM Employees
LEFT JOIN Pay
ON Pay.id = Employees.pay_table

? Or is there a better way?

I ask because this often seems very slow even if I only have a few of those kinds of relationships in large tables.

Yes this is the right way.

Indexing correctly will solve most preformance problems with joins.

SHOW CREATE TABLE {table name} and EXPLAIN {query} is the main information required to help solve MySQL performance problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM