加快MySQL中的插入速度

时间:2013-08-26 20:24:10

标签: mysql innodb

我有三张表InnoDB,这三张表有一些外键。

这是架构:

CREATE TABLE IF NOT EXISTS `tbl_member` (
  `id_member` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(80) COLLATE utf8_bin NOT NULL,
  `lastname` varchar(80) COLLATE utf8_bin NOT NULL,
  `username` varchar(40) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(64) COLLATE utf8_bin NOT NULL,
  `phone` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `email` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `status` enum('active','inactive') COLLATE utf8_bin NOT NULL DEFAULT 'active',
  PRIMARY KEY (`id_member`),
  KEY `username` (`username`,`password`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=107 ;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_notification`
--

CREATE TABLE IF NOT EXISTS `tbl_notification` (
  `id_notification` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Notification ID',
  `type` enum('notification','announcement') COLLATE utf8_bin NOT NULL DEFAULT 'notification',
  `title` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'Title',
  `notification` text COLLATE utf8_bin NOT NULL COMMENT 'Body',
  `datestart` int(13) NOT NULL COMMENT 'Date when the notification will start to be deployed',
  `dateend` int(13) NOT NULL,
  PRIMARY KEY (`id_notification`),
  KEY `datestart` (`datestart`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_notification_member`
--

CREATE TABLE IF NOT EXISTS `tbl_notification_member` (
  `id_notification_member` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `id_notification` int(10) unsigned NOT NULL COMMENT 'Notification ID',
  `id_member` int(10) unsigned NOT NULL COMMENT 'Member ID',
  `status` enum('read','unread') NOT NULL DEFAULT 'unread' COMMENT 'Defines if the notification was viewed already or not',
  `timestamp` int(15) NOT NULL COMMENT 'Time when the notification was saw',
  PRIMARY KEY (`id_notification_member`),
  KEY `id_notification` (`id_notification`),
  KEY `id_member` (`id_member`),
  KEY `timestamp` (`timestamp`),
  KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Constraints for table `tbl_notification_member`
--
ALTER TABLE `tbl_notification_member`
  ADD CONSTRAINT `tbl_notification_member_ibfk_2` FOREIGN KEY (`id_member`) REFERENCES `tbl_member` (`id_member`),
  ADD CONSTRAINT `tbl_notification_member_ibfk_3` FOREIGN KEY (`id_notification`) REFERENCES `tbl_notification` (`id_notification`) ON DELETE CASCADE;

因此,您可以看到我是否删除了一个通知,通知和成员之间的关系将在级联上删除,这很棒,但问题是当我尝试插入数千行时。有没有办法像cascade那样以某种方式做到这一点?使用索引或类似的东西? (我删除了表格中的另一个关系,例如部分,因此您只能为一个部分添加通知,并划分将收到通知的用户数量)

1 个答案:

答案 0 :(得分:3)

您可以按Innodb Performance Optimization Basics

增加它
  

同时检查您的应用程序是否可以在READ-COMMITED隔离模式下运行    - 如果是 - 将其设置为默认值   事务隔离= READ-COMMITTED。此选项有一些性能   好处,尤其是锁定5.0甚至更多   MySQL 5.1和行级复制。

同时检查this