0

I am using the current SNOMED data and examples and I want to create a transitive closure table, but something in my mysql5.6 default server settings are failing.

For those who do not know, SNOMED is a medical database. There are 2.1M relationships and 446697 concepts. The query is stalling on the second part - so I guess it is running out of RAM. But which settings do i tweak and to what? join_buffer_size?

here is the code:

DELIMITER ;;
CREATE DEFINER=`snomed`@`localhost` PROCEDURE `createTc`()
BEGIN
    drop table if exists tc;

    CREATE TABLE tc (
        source BIGINT UNSIGNED NOT NULL ,
        dest BIGINT UNSIGNED NOT NULL
        ) ENGINE = InnoDB CHARSET=utf8;
    insert into tc (source, dest)
        select distinct rel.sourceid, rel.destinationid
        from rf2_ss_relationships rel
        inner join rf2_ss_concepts con
            on rel.sourceid = con.id and con.active = 1
        where rel.typeid = 116680003 # IS A relationship
        and rel.active = 1;
    REPEAT
        insert into tc (source, dest)
            select distinct b.source, a.dest
            from tc a
            join tc b on a.source = b.dest
            left join tc c on c.source = b.source and c.dest = a.dest
            where c.source is null;
        set @x = row_count();
        select concat('Inserted ', @x);
    UNTIL @x = 0 END REPEAT;
    create index idx_tc_source on tc (source);
    create index idx_tc_dest on tc (dest);
END;;
DELIMITER ;
CREATE TABLE `rf2_ss_relationships` (
  `id` bigint(20) unsigned NOT NULL,
  `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `active` tinyint(4) DEFAULT '1',
  `moduleId` bigint(20) unsigned NOT NULL,
  `sourceId` bigint(20) unsigned NOT NULL,
  `destinationId` bigint(20) unsigned NOT NULL,
  `relationshipGroup` bigint(20) unsigned NOT NULL,
  `typeId` bigint(20) unsigned NOT NULL,
  `characteristicTypeId` bigint(20) unsigned NOT NULL,
  `modifierId` bigint(20) unsigned NOT NULL,
  PRIMARY KEY (`id`,`effectiveTime`),
  KEY `moduleId_idx` (`moduleId`),
  KEY `sourceId_idx` (`sourceId`),
  KEY `destinationId_idx` (`destinationId`),
  KEY `relationshipGroup_idx` (`relationshipGroup`),
  KEY `typeId_idx` (`typeId`),
  KEY `characteristicTypeId_idx` (`characteristicTypeId`),
  KEY `modifierId_idx` (`modifierId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 CREATE TABLE `rf2_ss_concepts` (
  `id` bigint(20) unsigned NOT NULL,
  `effectiveTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `active` tinyint(4) DEFAULT NULL,
  `moduleId` bigint(20) unsigned NOT NULL,
  `definitionStatusId` bigint(20) unsigned NOT NULL,
  PRIMARY KEY (`id`,`effectiveTime`),
  KEY `moduleId_idx` (`moduleId`),
  KEY `definitionStatusId_idx` (`definitionStatusId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
pgee70
  • 3,707
  • 4
  • 35
  • 41
  • i now use this: https://github.com/WestCoastInformatics/SNOMED-CT-Transitive-Closure to do the transitive closure in java and then import the text file. it is far faster than mysql. – pgee70 Oct 31 '16 at 23:14

2 Answers2

2

I don't know if it is the best answer, but it does work... I changed the create table syntax to add an index on creation - not after completion. I changed the mysqld setting for innodb_buffer_pool_size=8G

    CREATE TABLE tc (
    source BIGINT UNSIGNED NOT NULL ,
    dest BIGINT UNSIGNED NOT NULL,
    KEY source_idx (source),
    KEy dest_idx (dest)
    ) ENGINE = InnoDB CHARSET=utf8;

even still the execution on my i7 mac with SSD was not fast but it did work, and the transitive closure table is 5180059 rows ....

mysql> call createTc;
+-------------------------+
| concat('Inserted ', @x) |
+-------------------------+
| Inserted 654161         |
+-------------------------+
1 row in set (1 min 55.13 sec)

+-------------------------+
| concat('Inserted ', @x) |
+-------------------------+
| Inserted 1752024        |
+-------------------------+
1 row in set (3 min 5.60 sec)

+-------------------------+
| concat('Inserted ', @x) |
+-------------------------+
| Inserted 2063816        |
+-------------------------+
1 row in set (10 min 42.07 sec)

+-------------------------+
| concat('Inserted ', @x) |
+-------------------------+
| Inserted 275904         |
+-------------------------+
1 row in set (28 min 5.49 sec)

+-------------------------+
| concat('Inserted ', @x) |
+-------------------------+
| Inserted 280            |
+-------------------------+
1 row in set (46 min 29.78 sec)

+-------------------------+
| concat('Inserted ', @x) |
+-------------------------+
| Inserted 0              |
+-------------------------+
1 row in set (1 hour 5 min 20.05 sec)

Query OK, 0 rows affected (1 hour 5 min 20.05 sec)
pgee70
  • 3,707
  • 4
  • 35
  • 41
  • subsequent to this, i found a java implementation that solve the problem much faster. check this out: https://github.com/WestCoastInformatics/SNOMED-CT-Transitive-Closure/blob/master/src/main/java/com/wcinformatics/snomed/TransitiveClosureGenerator.java – pgee70 Mar 09 '16 at 05:07
0

I use this recursive method. While reading the relationships, I have already added all direct descendants into list in the concept-objects (I use hibernate), so they are available.

Then I start this recursive function by walking down the list of concepts. See the example. It walks for every concept through the list of all direct parents:

for (Sct2Concept c : concepts.values()) {
    for(Sct2Relationship parentRelation : c.getChildOfRelationships()){
        addParentToList(concepts, sct2TransitiveClosureList, parentRelation, c);
    }
}

As you can see, the TransitiveClosure memory storage is a Set, so that cheques for unique values on smart and very matured Java-library internal code.

private void addParentToList(Map<String, Sct2Concept> concepts, Set<Sct2TransitiveClosure> sct2TransitiveClosureList, Sct2Relationship parentRelation, Sct2Concept c){
        if(!parentRelation.isActive())
            return;
        Sct2TransitiveClosure tc = new Sct2TransitiveClosure(parentRelation.getDestinationSct2Concept().getId(), c.getId());
        if(sct2TransitiveClosureList.add(tc)){
            Sct2Concept s = concepts.get(Long.toString(tc.getParentId()));
            for(Sct2Relationship newParentRelation : s.getChildOfRelationships()){
                addParentToList(concepts, sct2TransitiveClosureList, newParentRelation, c);
        }
    }
}
Bert Verhees
  • 1,057
  • 3
  • 14
  • 25