我有以下表格结构。添加了两个触发器,但由于触发器使用字符串值并且它们搜索字符串,因此将来每个表都会变大时,数据库性能可能会降低。我对索引没有很强的经验,并且不知道要索引哪些字段以使触发器的搜索操作快速,即使有数百万行。
你有什么建议?
CREATE TABLE `ofRoster` (
`rosterID` bigint(20) NOT NULL,
`username` varchar(64) NOT NULL,
`jid` varchar(1024) NOT NULL,
`sub` tinyint(4) NOT NULL,
`ask` tinyint(4) NOT NULL,
`recv` tinyint(4) NOT NULL,
`nick` varchar(255) DEFAULT NULL,
PRIMARY KEY (`rosterID`),
KEY `ofRoster_unameid_idx` (`username`),
KEY `ofRoster_jid_idx` (`jid`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `ofUser` (
`username` varchar(64) NOT NULL,
`plainPassword` varchar(32) DEFAULT NULL,
`encryptedPassword` varchar(255) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`creationDate` char(15) NOT NULL,
`modificationDate` char(15) NOT NULL,
PRIMARY KEY (`username`),
KEY `ofUser_cDate_idx` (`creationDate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
delimiter ;;
CREATE TRIGGER `UpdateNickOnInsert` BEFORE INSERT ON `ofRoster` FOR EACH ROW BEGIN
SET NEW.nick=(SELECT name FROM ofUser where username=NEW.username);
END
;;
delimiter ;
delimiter ;;
CREATE TRIGGER `UpdateRosterNicksOnUpdate` AFTER UPDATE ON `ofUser` FOR EACH ROW BEGIN
IF NEW.name <> OLD.name
THEN
UPDATE ofRoster r SET r.nick=NEW.name WHERE r.username=OLD.username;
END IF;
END
;;
delimiter ;