[英]MySQL Database Design Performance And Query
嗨,正如您所看到的,这里有2种设计。 两者都基于每个主表只有一行,而其他关系表中只有主表行。 [1:1]
设计1:有可为空的表外键; 电源表中的项目,其他,秒
设计2:其他人都有主表的外键
我的问题是,哪种设计合适? 为了进行Select SQL查询,在“设计1”中需要使用UNION,而对于“设计2”,我想选择LEFT JOIN。 那么,就通过大量数据的性能而言,合适的方法和sql查询应该如何?
更新 :
SELECT mains.id as mainId, items.id as id, mains.title as title, items.name as name
FROM items, mains where mains.itemId=items.id
UNION
SELECT mains.id as mainId, others.id as id, mains.title as title, others.name as name
FROM others, mains where mains.otherId=others.id
UNION
SELECT mains.id as mainId, seconds.id as id, mains.title as title, seconds.name as name
FROM seconds, mains where mains.secondId=seconds.id
------------------------------
SELECT *
FROM mains
LEFT JOIN items ON mains.itemId=items.Id
LEFT JOIN seconds ON mains.secondId=seconds.id
LEFT JOIN others ON mains.otherId=others.id
------------------------------
SELECT main.id as mainId, item.id as id, main.title as title, item.name as name
FROM item, main where main.id=item.mainId
UNION
SELECT main.id as mainId, other.id as id, main.title as title, other.name as name
FROM other, main where main.id=other.mainId
UNION
SELECT main.id as mainId, second.id as id, main.title as title, second.name as name
FROM second, main where main.id=second.mainId
-------------------------------
SELECT *
FROM mains
LEFT JOIN items ON mains.itemId=items.Id
LEFT JOIN seconds ON mains.secondId=seconds.id
LEFT JOIN others ON mains.otherId=others.id
不同的SQL查询方法..! 哪一个? 性能?
如果主表中的每个记录与项具有一对一的关系,那么我更喜欢将其他表和第二个表全部存储在一个表中,而不必使用JOIN或UNION。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.