[Cubicweb] Entity deletion is slow

Adrien Di Mascio adrien.dimascio at logilab.fr
Tue Sep 9 15:03:06 CEST 2014


Hi everyone,

Le 09/09/2014 09:53, Aurélien Campéas a écrit :
> On 09/09/2014 09:38, Julien Cristau wrote:
>> On Fri, Aug 29, 2014 at 10:37:31 +0200, Yann Cointepas wrote:
> I know some people at logilab have "fast-delete-entity" script, which
> would be a good starting point.

Something like this exists, but it's just "brute-force" SQL code. There 
is absolutely no guarantee that you won't have your database in a 
strange state with relations pointing to something that has been 
deleted. But indeed, it is much (much) faster.


Here's an example of such code:

def _fast_delete_etype(session, etype):
     etype_eid = session.find_one_entity('CWEType', name=etype).eid
     print 'deleting from table cw_%s' % etype
     session.system_sql('DELETE FROM cw_%s' % etype.lower())
     print 'deleting from table is_relation'
     session.system_sql('DELETE FROM is_relation WHERE eid_to=%(eid)s',
                        {'eid': etype_eid})
     print 'deleting from table is_instance_of_relation'
     session.system_sql('DELETE FROM is_instance_of_relation '
                        'WHERE eid_to=%(eid)s',
                        {'eid': etype_eid})
     for table in ('cw_source_relation', 'owned_by_relation',
                   'created_by_relation'):
         print 'deleting from table %s' % table
         session.system_sql("DELETE FROM %s WHERE eid_from IN "
                            "(SELECT eid FROM entities WHERE type='%s')"
                            % (table, etype))
     print 'deleting from entities'
     session.system_sql("DELETE FROM entities WHERE type='%s'" % etype)


Cheers,
-- 
Adrien Di Mascio - LOGILAB, Paris (France).
Tél: 01.45.32.03.12
Formations - http://www.logilab.fr/formations
Développements - http://www.logilab.fr/services
Gestion de connaissances - http://www.cubicweb.org/



More information about the Cubicweb mailing list