开发者

MyISAM Foreign Key like functionality

开发者 https://www.devze.com 2023-01-27 01:18 出处:网络
I have a table called TABLE1 which have 5 columns that I rarely use and design-wise should b开发者_运维问答e separated from that table. Therefore, I created a new table called TABLE-REF where its prim

I have a table called TABLE1 which have 5 columns that I rarely use and design-wise should b开发者_运维问答e separated from that table. Therefore, I created a new table called TABLE-REF where its primary key (called refID) is actually the ID of the corresponded columns in TABLE1. That way I separated these 5 columns from my main table. I did not use a foreign key since I am using MyISAM.

On the rare occasions when I do need to select these 5 columns for rows in TABLE1, what will be the most efficient way to select them?

SELECT * FROM TABLE-REF WHERE refID={$table1ID}

Beside indexing the column refID, is there any other improvement I can make to improve the complexity of the query?


You can get both rows as one if you use a join. so no more back to the database for each row in TABLE to get the TABLE-REF row.

SELECT 
    T.*, R.* 
FROM `TABLE` T 
INNER JOIN `TABLE-REF` R ON R.refID = T.ID
WHERE <your conditions>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号