Projet

Général

Profil

0001-sql-add-and-use-indexes-on-concerned_roles_array-and.patch

Pierre Ducroquet, 10 janvier 2022 19:52

Télécharger (4 ko)

Voir les différences:

Subject: [PATCH] sql: add and use indexes on concerned_roles_array and
 actions_roles_array (#60469)

After reading some slow queries log in lille-prod, I noticed these two columns
had no usable index (and their queries were not able to use them anyway).
This patch fixes it.
 wcs/sql.py | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
wcs/sql.py
533 533
        conn, cur = get_connection_and_cursor()
534 534

  
535 535
    table_name = get_formdef_table_name(formdef)
536
    new_table = False
536 537

  
537 538
    cur.execute(
538 539
        '''SELECT COUNT(*) FROM information_schema.tables
......
564 565
                                    formdata_id integer REFERENCES %s (id) ON DELETE CASCADE)'''
565 566
            % (table_name, table_name)
566 567
        )
567
        do_formdef_indexes(formdef, created=True, conn=conn, cur=cur)
568
        new_table = True
568 569

  
569 570
    cur.execute(
570 571
        '''SELECT column_name FROM information_schema.columns
......
705 706
        # them even if not asked to.
706 707
        redo_views(conn, cur, formdef, rebuild_global_views=rebuild_global_views)
707 708

  
709
    if new_table:
710
        do_formdef_indexes(formdef, created=True, conn=conn, cur=cur)
711

  
708 712
    if own_conn:
709 713
        conn.commit()
710 714
        cur.close()
......
751 755
                '%(create_index)s %(table_name)s_%(attr)s_idx ON %(table_name)s (%(attr)s)'
752 756
                % {'create_index': create_index, 'table_name': table_name, 'attr': attr}
753 757
            )
758
    for attr in ('concerned_roles_array', 'actions_roles_array'):
759
        if table_name + '_' + attr + '_idx' not in existing_indexes:
760
            cur.execute(
761
                '%(create_index)s %(table_name)s_%(attr)s_idx ON %(table_name)s USING gin (%(attr)s)'
762
                % {'create_index': create_index, 'table_name': table_name, 'attr': attr}
763
            )
754 764

  
755 765

  
756 766
@guard_postgres
......
2336 2346
            value = str(value)
2337 2347

  
2338 2348
        if '%s_array' % index in [x[0] for x in cls._table_static_fields]:
2339
            sql_statement = '''SELECT id FROM %s WHERE %%(value)s = ANY (%s_array)''' % (
2349
            sql_statement = '''SELECT id FROM %s WHERE %s_array @> ARRAY[%%(value)s]''' % (
2340 2350
                cls._table_name,
2341 2351
                index,
2342 2352
            )
......
3499 3509
# latest migration, number + description (description is not used
3500 3510
# programmaticaly but will make sure git conflicts if two migrations are
3501 3511
# separately added with the same number)
3502
SQL_LEVEL = (55, 'update full text normalisation (switch to unidecode)')
3512
SQL_LEVEL = (56, 'add gin indexes to concerned_roles_array and actions_roles_array')
3503 3513

  
3504 3514

  
3505 3515
def migrate_global_views(conn, cur):
......
3638 3648
        # 51: add index on formdata blockdef fields
3639 3649
        # 55: update full text normalisation (switch to unidecode)
3640 3650
        set_reindex('formdata', 'needed', conn=conn, cur=cur)
3641
    if sql_level < 46:
3651
    if sql_level < 56:
3642 3652
        from wcs.carddef import CardDef
3643 3653
        from wcs.formdef import FormDef
3644 3654

  
......
3646 3656
        # 35: add indexes on formdata(receipt_time) and formdata(anonymised)
3647 3657
        # 36: add index on formdata(user_id)
3648 3658
        # 45 & 46: add index on formdata(status)
3659
        # 56: add GIN indexes to concerned_roles_array and actions_roles_array
3649 3660
        for formdef in FormDef.select() + CardDef.select():
3650 3661
            do_formdef_indexes(formdef, created=False, conn=conn, cur=cur)
3651 3662
    if sql_level < 32:
3652
-