Impacted versions:
8.0 and so on

Steps to reproduce:
create a many2many field in product.product such as:
`'company_ids':fields.many2many('res.company','res_company_product_rel','partner_id','company_ids',string=u'multi company''),`
and add a ir.rule for product.product with  domain such as
`['|',('company_id','=',False),('company_ids','in',[c.id for c in user.company_ids])]`
and import 10 thousand product.
then login with normal user(not super) and open the product.produt list view
it taks several minus to open

Current behavior:
when I search product.product
all product id have been hard coded in the sql 
lead to low performance.

Expected behavior:
not hard code produt id

Video/Screenshot link (optional):
in the sql below 10 thousand product id have been hard code in the position %s
AND ("product_product__product_tmpl_id"."company_id" IS NULL   OR  ("product_product"."id" in (%s)))

ids1 = select_distinct_from_where_not_null(cr, column._fields_id, comodel._table)
                    else:
                        ids2 = comodel.search(cr, uid, [(column._fields_id, '!=', False)], context=context)
                        recs = comodel.browse(cr, SUPERUSER_ID, ids2, {'prefetch_fields': False})
                        ids1 = recs.mapped(column._fields_id).ids
                    push(create_substitution_leaf(leaf, ('id', o2m_op, ids1), model))
            elif column._type == 'many2many':
                rel_table, rel_id1, rel_id2 = column._sql_names(model)
                #F
                if operator == 'child_of':
                    ids2 = to_ids(right, comodel, context)
                    dom = child_of_domain('id', ids2, comodel)
                    ids2 = comodel.search(cr, uid, dom, context=context)
                    if comodel == model:
                        push(create_substitution_leaf(leaf, ('id', 'in', ids2), model))
                    else:
                        subquery = 'SELECT "%s" FROM "%s" WHERE "%s" IN %%s' % (rel_id1, rel_table, rel_id2)
                        # 
                        subquery = cr.mogrify(subquery, [tuple(ids2)])
                        push(create_substitution_leaf(leaf, ('id', 'inselect', (subquery, [])), internal=True))
                else:
                    call_null_m2m = True
                    if right is not False:
                        if isinstance(right, basestring):
                            res_ids = [x[0] for x in comodel.name_search(cr, uid, right, [], operator, context=context, limit=None)]
                            if res_ids:
                                operator = 'in'
                        else:
                            if not isinstance(right, list):
                                res_ids = [right]