我越来越喜欢CREATE VIEW
. 例如,它允许我拥有全局值和特定值,COALESCE(post.publish, profile.publish)
这样如果publish
是NULL
,则获取全局值。
从性能和逻辑的角度来看,我有点好奇的部分是我应该如何将它与现有表一起使用。假设我有一张桌子:
CREATE TABLE post (
id INT,
profile_id INT,
name VARCHAR,
publish ENUM('TRUE', 'FALSE') NULL
)
最CREATE VIEW
好像这样运行:
CREATE VIEW post_info AS
SELECT post.*, COALESCE(post.publish, profile.publish) AS publish
FROM post
INNER JOIN profile
ON post.profile_id = profile.id
并且仅post_info
在SELECT
某些情况下使用,或者:
CREATE VIEW post_info AS
SELECT post.id, COALESCE(post.publish, profile.publish) AS publish
FROM post
INNER JOIN profile
ON post.profile_id = profile.id
以及何时需要额外JOIN
post_info
的价值?post
SELECT
请分享您对此的见解和想法。我想听听您对每种解决方案的优缺点的意见。也可以是我没有提到的。