天啊。
经过一个多星期的努力,我认为我终于可以发挥作用了。这并不是针对性能进行优化(哦,循环!),但是在我可以提高性能的同时暂时完成了工作。产生的OrientDB服务器端函数(用javascript编写):
功能:
// Clear previous runsdb.command("truncate class tmp_Then");db.command("truncate class tmp_Events");// Get all distinct eventsvar distinctEvents = db.query("select from Events group by event");// Send 404 if null, otherwise proceedif (distinctEvents == null) { response.send(404, "Events not found", "text/plain", "Error: events not found" );} else { var edges = []; // Loop through all distinct events distinctEvents.forEach(function(distinctEvent) { var newEvent = []; var rid = distinctEvent.field("@rid"); var eventType = distinctEvent.field("event"); // The main query that finds all *direct* descendents of the distinct event var result = db.query("select from (traverse * from (select from Events where event = ?) where $depth <= 2) where @class = 'Events' and $depth > 1 and @rid in (select from Events group by event)", [eventType]); // Save the distinct event in a temp table to create temp edges db.command("create vertex tmp_Events set rid = ?, event = ?", [rid, event]); edges.push(result); }); // The edges array defines which edges should exist for a given event edges.forEach(function(edge, index) { edge.forEach(function(e) { // Create the temp edge that corresponds to its distinct event db.command("create edge tmp_Then from (select from tmp_Events where rid = " + distinctEvents[index].field("@rid") + ") to (select from tmp_Events where rid = " + e.field("@rid") + ")"); }); }); var result = db.query("select from tmp_Events"); return result;}外卖:
- 临时表似乎是必要的。 我尝试在没有临时表(类)的情况下执行此操作,但是我不确定是否可以执行此操作。我需要模拟原始数据中不存在的边缘。
- Traverse在编写主查询时非常有帮助。 遍历一个事件以找到其直接,唯一的后代非常简单。
- 能够用Javascript编写存储的proc的能力真是太棒了。 这在SQL中将是一场噩梦。
- omfg循环。 我计划对此进行优化,并继续使其更好,以便希望其他人可以从中找到一些用处。



