Projet

Général

Profil

0001-create-index-only-when-needed-72010.patch

Pierre Ducroquet, 05 décembre 2022 10:38

Télécharger (2,24 ko)

Voir les différences:

Subject: [PATCH] create index only when needed (#72010)

 wcs/sql.py | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
wcs/sql.py
1661 1661
        conn, cur = get_connection_and_cursor()
1662 1662

  
1663 1663
    cur.execute(
1664
        '''SELECT COUNT(*) FROM information_schema.tables
1664
        '''SELECT COUNT(t.*), COUNT(i.*) FROM information_schema.tables t
1665
                    LEFT JOIN pg_indexes i
1666
                        ON (i.schemaname, i.tablename, i.indexname) = (table_schema, table_name, %s)
1665 1667
                    WHERE table_schema = 'public'
1666 1668
                      AND table_name = %s''',
1667
        ('wcs_meta',),
1669
        (
1670
            'wcs_meta_key',
1671
            'wcs_meta',
1672
        ),
1668 1673
    )
1669
    table_exists = cur.fetchone()[0] > 0
1674
    info_row = cur.fetchone()
1675
    table_exists = info_row[0] > 0
1676
    index_exists = info_row[1] > 0
1670 1677

  
1671 1678
    if not table_exists:
1672 1679
        cur.execute(
......
1676 1683
                                    created_at timestamptz DEFAULT NOW(),
1677 1684
                                    updated_at timestamptz DEFAULT NOW())'''
1678 1685
        )
1686
        cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS wcs_meta_key ON wcs_meta (key)')
1679 1687

  
1680 1688
        if insert_current_sql_level:
1681 1689
            sql_level = SQL_LEVEL[0]
......
1699 1707
            cur.execute('''ALTER TABLE wcs_meta ADD COLUMN created_at timestamptz DEFAULT NOW()''')
1700 1708
        if 'updated_at' not in existing_fields:
1701 1709
            cur.execute('''ALTER TABLE wcs_meta ADD COLUMN updated_at timestamptz DEFAULT NOW()''')
1702

  
1703
    cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS wcs_meta_key ON wcs_meta (key)')
1710
        if not index_exists:
1711
            cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS wcs_meta_key ON wcs_meta (key)')
1704 1712

  
1705 1713
    if own_conn:
1706 1714
        conn.commit()
1707
-