Xavier ALT (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-server/6.1-opw-577963-odo into 
lp:openobject-server/6.1.

Requested reviews:
  OpenERP Core Team (openerp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-server/6.1-opw-577963-odo/+merge/120586

Hi,

This improve the speed of ir.attachment search() method - making this usable 
for database with 100k+ attachments.
(coutesy of Mr. Olivier Dony)

Regards,
Xavier
-- 
https://code.launchpad.net/~openerp-dev/openobject-server/6.1-opw-577963-odo/+merge/120586
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-server/6.1-opw-577963-odo.
=== modified file 'openerp/addons/base/ir/ir_attachment.py'
--- openerp/addons/base/ir/ir_attachment.py	2012-06-27 08:57:09 +0000
+++ openerp/addons/base/ir/ir_attachment.py	2012-08-21 14:46:23 +0000
@@ -64,10 +64,18 @@
                 return 0
             return []
 
+        # Work with a set, as list.remove() is prohibitive for large lists of documents
+        # (takes 20+ seconds on a db with 100k docs during search_count()!)
+        orig_ids = ids
+        ids = set(ids)
+
         # For attachments, the permissions of the document they are attached to
         # apply, so we must remove attachments for which the user cannot access
         # the linked document.
-        targets = super(ir_attachment,self).read(cr, uid, ids, ['id', 'res_model', 'res_id'])
+        # Use pure SQL rather than read() as it is about 50% faster for large dbs (100k+ docs),
+        # and the permissions are checked in super() and below anyway.
+        cr.execute("""SELECT id, res_model, res_id FROM ir_attachment WHERE id = ANY(%s)""", (list(ids),))
+        targets = cr.dictfetchall()
         model_attachments = {}
         for target_dict in targets:
             if not (target_dict['res_id'] and target_dict['res_model']):
@@ -92,9 +100,10 @@
             for res_id in disallowed_ids:
                 for attach_id in targets[res_id]:
                     ids.remove(attach_id)
-        if count:
-            return len(ids)
-        return ids
+
+        # sort result according to the original sort ordering
+        result = [id for id in orig_ids if id in ids]
+        return len(result) if count else list(result)
 
     def read(self, cr, uid, ids, fields_to_read=None, context=None, load='_classic_read'):
         self.check(cr, uid, ids, 'read', context=context)

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to