MySQL数据库设计性能和查询

时间:2016-02-03 08:32:31

标签: mysql performance

MySQL DB Design Schema

您好 如你所见,我们在这里有两种设计。它们都基于每个主表只有一行或其他关系表中的主表行。 [1:1]

设计1: 表有无效的外键;物品,其他,主电源表中的秒数

设计2: 在其他地方有主表的外键

我的问题是,哪种设计合适?对于进行Select SQL查询,在“Design 1”中需要使用UNION,而对于“Design 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查询方法..!哪一个?性能?

1 个答案:

答案 0 :(得分:0)

如果主表中的每条记录与item具有一对一的关系,则我更喜欢将其他表和第二表存储在一个表中,避免使用JOIN或UNION。