我有一个主表与实际的用户信息
CREATE TABLE user
(
id bigint NOT NULL
PRIMARY KEY,
updated timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
username varchar(40) NULL,
full_name varchar(255) NULL,
biography varchar(512) NULL,
profile_pic_id varchar(40) NULL,
profile_pic_url varchar(255) NULL,
hd_profile_pic_url varchar(255) NULL,
follower_count int NULL,
following_count int NULL,
media_count int NULL,
usertags_count int NULL,
following_tag_count int NULL,
external_url longtext NULL,
reel_auto_archive varchar(255) NULL,
has_biography_translation tinyint(1) NULL,
has_anonymous_profile_picture tinyint(1) NULL,
has_highlight_reels tinyint(1) NULL,
is_business tinyint(1) NULL,
is_active tinyint(1) NULL,
is_verified tinyint(1) NULL,
is_private tinyint(1) NULL,
is_blocked tinyint(1) NULL
)
保存历史记录的表几乎相同:
CREATE TABLE user_history
(
id int AUTO_INCREMENT
PRIMARY KEY,
user_id bigint NULL,
added timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
username varchar(40) NULL,
full_name varchar(255) NULL,
biography varchar(512) NULL,
profile_pic_id varchar(40) NULL,
profile_pic_url varchar(255) NULL,
hd_profile_pic_url varchar(255) NULL,
follower_count int NULL,
following_count int NULL,
media_count int NULL,
usertags_count int NULL,
following_tag_count int NULL,
external_url longtext NULL,
reel_auto_archive varchar(255) NULL,
has_biography_translation tinyint(1) NULL,
has_anonymous_profile_picture tinyint(1) NULL,
has_highlight_reels tinyint(1) NULL,
is_business tinyint(1) NULL,
is_active tinyint(1) NULL,
is_verified tinyint(1) NULL,
is_private tinyint(1) NULL,
is_blocked tinyint(1) NULL,
CONSTRAINT FK_F19A7E3C5AFE2D44
FOREIGN KEY (user_id) REFERENCES user (id)
)
COLLATE = utf8mb4_unicode_ci;
CREATE INDEX IDX_F19A7E3C5AFE2D44
ON user_history (user_id);
拯救历史的老虎:
CREATE TRIGGER user_update
AFTER UPDATE
ON user
FOR EACH ROW
BEGIN
INSERT INTO `user_history` (`user_id`, `username`, `full_name`, `biography`, `profile_pic_id`,
`profile_pic_url`, `hd_profile_pic_url`, `follower_count`, `following_count`,
`media_count`, `usertags_count`, `following_tag_count`, `external_url`,
`reel_auto_archive`, `has_biography_translation`, `has_anonymous_profile_picture`,
`has_highlight_reels`, `is_business`, `is_active`, `is_verified`, `is_private`,
`is_blocked`, `added`)
VALUES (NEW.id, NEW.username, NEW.full_name, NEW.biography, NEW.profile_pic_id, NEW.profile_pic_url,
NEW.hd_profile_pic_url,
NEW.follower_count, NEW.following_count, NEW.media_count, NEW.usertags_count, NEW.following_tag_count,
NEW.external_url,
NEW.reel_auto_archive, NEW.has_biography_translation, NEW.has_anonymous_profile_picture,
NEW.has_highlight_reels,
NEW.is_business, NEW.is_active, NEW.is_verified, NEW.is_private, NEW.is_blocked, now());
END;
所以我有一些问题:
UPDATE
。 “UPDATE”的内部处理涉及构建整个行的副本,以防某些其他连接访问同一行。has_%
和is_%
标志组合成单个SET
或TINYINT UNSIGNED
(最多8个,或SMALLINT UNSIGNED
最多16个)。has
标志 - 当你需要检查“有”时,只需使用LEFT JOIN
或EXISTS
。is_%
标志(可能还有其他列)NOT NULL
。一般来说,你应该有意识地决定NULL
是否对业务逻辑有意义,而不是简单地将所有列都设为NULLable
。(我的建议适用于所有版本的MySQL和MariaDB。)