添加新索引

时间:2014-09-01 16:38:38

标签: symfony doctrine-orm

我有2个问题。我不确定我们是否可以为2个问题创建一个线程,所以请告诉我。

添加新索引

我要为性能添加新索引,但它会创建一个新表用户带有新索引

我的代码:

 * User
 *
 * @ORM\Table(name="fos_user")
 * @ORM\Table(indexes={@ORM\Index(name="last_activity_idx", columns={"last_activity"})})
 * @ORM\Table(indexes={@ORM\Index(name="avatar_idx", columns={"avatar"})})

所以我想,我必须在一个@ORM \ Table中列出所有索引。但是怎么样?

我尝试更新

时出错

当我尝试 - 强制执行此错误时:

C:\wamp\www\ados>php app/console doctrine:schema:update --dump-sql
CREATE TABLE User (id INT AUTO_INCREMENT NOT NULL, profil_id INT DEFAULT NULL, username VARCHAR(255) NOT NULL, username_canonical VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, emai
l_canonical VARCHAR(255) NOT NULL, enabled TINYINT(1) NOT NULL, salt VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, last_login DATETIME DEFAULT NULL, locked TINYINT(1) NOT NULL,
expired TINYINT(1) NOT NULL, expires_at DATETIME DEFAULT NULL, confirmation_token VARCHAR(255) DEFAULT NULL, password_requested_at DATETIME DEFAULT NULL, roles LONGTEXT NOT NULL COMMENT
'(DC2Type:array)', credentials_expired TINYINT(1) NOT NULL, credentials_expire_at DATETIME DEFAULT NULL, user_ip VARCHAR(255) NOT NULL, name VARCHAR(16) NOT NULL, avatar VARCHAR(255) NOT
 NULL, birthday DATE NOT NULL, sex TINYINT(1) NOT NULL, city SMALLINT NOT NULL, country SMALLINT NOT NULL, last_activity DATETIME NOT NULL, warning SMALLINT NOT NULL, status SMALLINT NOT
 NULL, notification INT NOT NULL, visitor INT NOT NULL, friend INT NOT NULL, message INT NOT NULL, antiflood TINYINT(1) NOT NULL, autodestruction TINYINT(1) NOT NULL, suspended TINYINT(1
) NOT NULL, UNIQUE INDEX UNIQ_2DA1797792FC23A8 (username_canonical), UNIQUE INDEX UNIQ_2DA17977A0D96FBF (email_canonical), UNIQUE INDEX UNIQ_2DA17977275ED078 (profil_id), INDEX avatar_id
x (avatar), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Verify (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, photo_id INT NOT NULL, status TINYINT(1) NOT NULL, date DATETIME NOT NULL, INDEX IDX_A6892F75A76ED395 (user_id)
, UNIQUE INDEX UNIQ_A6892F757E9E4C8C (photo_id), INDEX status_idx (status), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE User ADD CONSTRAINT FK_2DA17977275ED078 FOREIGN KEY (profil_id) REFERENCES profil (id);
ALTER TABLE Verify ADD CONSTRAINT FK_A6892F75A76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE Verify ADD CONSTRAINT FK_A6892F757E9E4C8C FOREIGN KEY (photo_id) REFERENCES photo (id) ON DELETE CASCADE;
DROP INDEX country_idx ON city;
CREATE INDEX name_idx ON city (name);
ALTER TABLE comment DROP FOREIGN KEY FK_9474526CA76ED395;
ALTER TABLE comment ADD CONSTRAINT FK_9474526CA76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE likes DROP FOREIGN KEY FK_49CA4E7DA76ED395;
ALTER TABLE likes ADD CONSTRAINT FK_49CA4E7DA76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE photo DROP FOREIGN KEY FK_14B78418A76ED395;
ALTER TABLE photo ADD CONSTRAINT FK_14B78418A76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE notification ADD CONSTRAINT FK_A765AD32F624B39D FOREIGN KEY (sender_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE notification ADD CONSTRAINT FK_A765AD32CD53EDB6 FOREIGN KEY (receiver_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE newsfeed ADD CONSTRAINT FK_2AEA5025F624B39D FOREIGN KEY (sender_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE visitor ADD CONSTRAINT FK_558D80370BEE6D FOREIGN KEY (visitor_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE visitor ADD CONSTRAINT FK_558D803CD53EDB6 FOREIGN KEY (receiver_id) REFERENCES User (id) ON DELETE CASCADE;
CREATE INDEX date_idx ON visitor (date);
ALTER TABLE bookmark DROP FOREIGN KEY FK_DA62921D5093B91;
ALTER TABLE bookmark DROP FOREIGN KEY FK_DA62921D707B33D2;
ALTER TABLE bookmark ADD CONSTRAINT FK_DA62921D5093B91 FOREIGN KEY (bookmarked_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE bookmark ADD CONSTRAINT FK_DA62921D707B33D2 FOREIGN KEY (bookmarker_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE ask ADD CONSTRAINT FK_506BAC00F624B39D FOREIGN KEY (sender_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE ask ADD CONSTRAINT FK_506BAC00CD53EDB6 FOREIGN KEY (receiver_id) REFERENCES User (id) ON DELETE CASCADE;
CREATE INDEX status_idx ON ask (status);
ALTER TABLE block DROP FOREIGN KEY FK_831B972244972A0E;
ALTER TABLE block DROP FOREIGN KEY FK_831B9722548D5975;
ALTER TABLE block ADD CONSTRAINT FK_831B972244972A0E FOREIGN KEY (victim_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE block ADD CONSTRAINT FK_831B9722548D5975 FOREIGN KEY (blocker_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE friend DROP FOREIGN KEY FK_55EEAC616A62BC6F;
ALTER TABLE friend DROP FOREIGN KEY FK_55EEAC6178D71381;
ALTER TABLE friend ADD CONSTRAINT FK_55EEAC616A62BC6F FOREIGN KEY (friend2_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE friend ADD CONSTRAINT FK_55EEAC6178D71381 FOREIGN KEY (friend1_id) REFERENCES User (id) ON DELETE CASCADE;
CREATE INDEX weight_idx ON category (weight);
ALTER TABLE follow DROP FOREIGN KEY FK_68344470A76ED395;
ALTER TABLE follow ADD CONSTRAINT FK_68344470A76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE post ADD CONSTRAINT FK_FAB8C3B3A76ED395 FOREIGN KEY (user_id) REFERENCES User (id);
CREATE INDEX date_idx ON post (date);
ALTER TABLE topic ADD CONSTRAINT FK_5C81F11FA76ED395 FOREIGN KEY (user_id) REFERENCES User (id);
CREATE INDEX home_idx ON topic (status, date);
ALTER TABLE report ADD CONSTRAINT FK_C38372B2A76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
CREATE INDEX priority_idx ON report (priority);
ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C5A76ED395;
ALTER TABLE log ADD CONSTRAINT FK_8F3F68C5A76ED395 FOREIGN KEY (user_id) REFERENCES User (id);
ALTER TABLE search DROP FOREIGN KEY FK_B4F0DBA7A76ED395;
ALTER TABLE search ADD CONSTRAINT FK_B4F0DBA7A76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE conversation ADD CONSTRAINT FK_92BFFD1156AE248B FOREIGN KEY (user1_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE conversation ADD CONSTRAINT FK_92BFFD11441B8B65 FOREIGN KEY (user2_id) REFERENCES User (id) ON DELETE CASCADE;
CREATE INDEX date_idx ON conversation (date);
ALTER TABLE message DROP FOREIGN KEY FK_B6BD307FA76ED395;
ALTER TABLE message ADD CONSTRAINT FK_B6BD307FA76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE;
ALTER TABLE ban ADD CONSTRAINT FK_5AB3960544972A0E FOREIGN KEY (victim_id) REFERENCES User (id);
ALTER TABLE ban ADD CONSTRAINT FK_5AB39605684EC833 FOREIGN KEY (banner_id) REFERENCES User (id);
CREATE INDEX date_idx ON ban (date);

C:\wamp\www\ados> php app/console doctrine:schema:update --force
Updating database schema...



  [Doctrine\DBAL\DBALException]
  An exception occurred while executing 'ALTER TABLE comment ADD CONSTRAINT FK_9474526CA76ED395 FOREIGN KEY (user_id) REFERENCES User (id) ON DELETE CASCADE':

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`rencontreados`.`#sql-1be0_46`, CONSTRAINT `FK_9474526CA76ED3
  95` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE)






  [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`rencontreados`.`#sql-1be0_46`, CONSTRAINT `FK_9474526CA76ED3
  95` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE)



doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]

(抱歉长篇文章)

我知道它想要删除所有索引并重新创建所有索引。但是我没有删除像user_id FK这样的学说创建的默认索引,我juste想要添加一些因为我的网站很慢。我完全失去了......

1 个答案:

答案 0 :(得分:1)

通常,您的方法是有效的。具有FK约束的事情是,Doctrine有时无法自行解决它们。

您必须打开MySQL shell并首先停用FK检查:

SET FOREIGN_KEY_CHECKS = 0;

然后你必须将app/console doctrine:schema:update --dump-sql --complete的输出发布到同一个MySQL shell中。

这应该应用所有挂起的更新(希望)没有问题。提示:连接所有行,以便在出现警告或错误时快速查看。

之后,您可以关闭shell并再次运行app/console doctrine:schema:update --dump-sql --complete,以查看您的数据库现在是否与架构同步。