[Cubicweb] Entity deletion is slow

Aurélien Campéas aurelien.campeas at logilab.fr
Tue Sep 9 15:25:24 CEST 2014


On 09/09/2014 15:03, Adrien Di Mascio wrote:
> 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.

Yes, that's why I mention it as a starting point.

> 
> 
> 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)
> 
> 

For instance, one should:

* work at the eid level (this function wipes whole etypes)
* handle composite relation cascading delete

Imho we should eventually have such an utility, e.g. in dataio ?

Regards,
Aurélien.





More information about the Cubicweb mailing list