简体   繁体   English

如何在“ json_array”类型的列上使用Doctrine在Symfony 3中添加复合索引?

[英]How do I add compound index in Symfony 3 using Doctrine on column of type “json_array”?

I have a table and I want to add indexes like this: 我有一个表,我想添加这样的索引:

 @Index(columns={"child_id", "abilities"}),
 @Index(columns={"parent_id", "abilities"}),

... ...

 * @ORM\Column(type="json_array", length=256)
 */
protected $abilities;

However, even after adding the appropriate limits in the migration: 但是,即使在迁移中添加了适当的限制后:

$this->addSql('CREATE INDEX IDX_1088BF61DD62C21BB8388DA4 ON xxx (child_id, abilities(512))');
$this->addSql('CREATE INDEX IDX_1088BF61727ACA70B8388DA4 ON xxx (parent_id, abilities(512))');

Errors like this occur when running tests, which recreate the database: 当运行测试以重新创建数据库时,会发生类似这样的错误:

An exception occurred while executing ' 执行'

CREATE TABLE xxx (
    id INT AUTO_INCREMENT NOT NULL, 
    parent_id INT DEFAULT NULL, 
    child_id INT DEFAULT NULL, 
    type VARCHAR(125) NOT NULL, 
    abilities LONGTEXT NOT NULL COMMENT '(DC2Type:json_array)', 
    created_at DATETIME NOT NULL, 
    modified_at DATETIME NOT NULL, 
    INDEX IDX_1088BF61727ACA70 (parent_id), 
    INDEX IDX_1088BF61DD62C21B (child_id), 
    INDEX IDX_1088BF61DD62C21B8CDE5729 (child_id, type), 
    INDEX IDX_1088BF61727ACA708CDE5729 (parent_id, type), 
    INDEX IDX_1088BF618CDE5729 (type), 
    INDEX IDX_1088BF61DD62C21BB8388DA4 (child_id, abilities), 
    INDEX IDX_1088BF61727ACA7 0B8388DA4 (parent_id, abilities), 
    INDEX IDX_1088BF61B8388DA4 (abilities), 
    UNIQUE INDEX UNIQ_1088BF61727ACA70DD62C21B (parent_id, child_id), 
    PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB

': ':

SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'abilities' used in key specification without a key length SQLSTATE [42000]:语法错误或访问冲突:1170在密钥规范中使用的BLOB / TEXT列“ abilities”,没有密钥长度

What do I need to do to successfully create indexes on these columns? 我需要怎么做才能在这些列上成功创建索引?

There are limitations on indexing long strings. 索引长字符串有限制。

Since the string in question contains "json", it is useless to have a regular index on that column. 由于所讨论的字符串包含“ json”,因此在该列上使用常规索引是没有用的。

Solution: Get rid of these 3 indexes: 解决方案:摆脱这三个索引:

INDEX IDX_1088BF61DD62C21BB8388DA4 (child_id, abilities), 
INDEX IDX_1088BF61727ACA7 0B8388DA4 (parent_id, abilities), 
INDEX IDX_1088BF61B8388DA4 (abilities), 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM