简体   繁体   中英

MySQL JOIN Query Index optimization

I have this query. Assuming no index is created yet, how can i optimize this using index so the query runs faster? How do i create the index?

select c.company_name, c.company_id FROM companies AS c 
JOIN users AS u USING(companyid) 
JOIN jobs AS j USING(userid) 
JOIN useraccounts AS ua USING(userid) 
WHERE j.jobid = 10;

Any help would greatly be appreciated.

You should have indexes on all the columns used in the JOIN clauses. You can create the indexes either with CREATE INDEX or ALTER TABLE commands:

CREATE INDEX u_companyid ON users (companyid);

ALTER TABLE users ADD INDEX (companyid);

Note that primary keys of tables are already indexed, you don't need to add an index explicitly.

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