postgresql 用数字和布尔值填充的Postgres约束列表

wswtfjt7  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(149)

我正在将一个数据库从MySQL迁移到Postgres,其中包含大量数据(128GB)。因此,我在没有约束和外键的情况下加载数据库以优化性能,但它最终给了我奇怪的约束,我不明白。
我加载新的数据库没有约束,性能的目的和填充数据从旧数据库(所有代码可以在下面找到)。然而,在完成所有这些之后,我得到了一些我不理解的奇怪的约束。该列表充满了数字和布尔值,而我希望只存在外键、主键和唯一约束。有人能告诉我这是什么吗此外,当“仅”加载128GB时,是否可以使用唯一约束构建数据库?第一个代码片段显示了约束。先谢谢你了

约束条件

(33129, 'forecasts_pkey', 2200, 'p', False, False, True, 33124, 0, 33128, 0, 0, ' ', ' ', ' ', True, 0, True, [1], None, None, None, None, None, None)
(33176, 'fk_models', 2200, 'f', False, False, True, 33124, 0, 33115, 0, 33112, 'a', 'a', 's', True, 0, True, [2], [1], [96], [96], [96], None, None)
(33182, 'unique_seriesid_modelrundate_modelrun_valuetimeutc_publishedatu', 2200, 'u', False, False, True, 33124, 0, 33181, 0, 0, ' ', ' ', ' ', True, 0, True, [2, 3, 7, 5, 8], None, None, None, None, None, None)

构建脚本

create table if not exists ModelType (
        Id int primary key,
        ModelType varchar (50)
    );

    create table if not exists Models (
        SeriesId int primary key,
        model varchar(50),
        ModelGroup varchar(50),
        Type varchar(20),
        Country varchar(50),
        Provider varchar(50),
        WeatherSystem varchar(10),
        Unit varchar(50),
        Area Varchar(10),
        ModelTypeId int,
        constraint fk_modeltype
            Foreign key (ModelTypeId) 
            references ModelType (Id)
    );

    CREATE TABLE IF NOT EXISTS Forecasts (
        id Serial PRIMARY KEY,
        SeriesId int,
        ModelRunDate date,
        InsertedAtUTC timestamp,
        ValueTimeUTC timestamp,
        Value REAL,
        ModelRun int,
        PublishedAtUTC timestamp,
    );

数据库完全加载后在我设置约束后运行此代码,首先删除所有重复代码并使用以下代码设置约束:

def delete_duplicate_rows_from_forecast():
    cur = conn.cursor()
    sql = '''DELETE FROM forecasts f1
                    USING forecasts f2
                    WHERE f1.ctid < f2.ctid
                    AND f1.seriesid = f2.seriesid
                    AND f1.modelrundate = f2.modelrundate
                    AND f1.modelrun = f2.modelrun
                    AND f1.valuetimeutc = f2.valuetimeutc
                    AND f1.insertedatutc = f2.insertedatutc;'''
    cur.execute(sql)
    cur.close()
    conn.commit()

def set_constraints_after_load():
    delete_duplicate_rows_from_forecast()
    cur = conn.cursor()
    try:
        cur.execute('''ALTER TABLE Forecasts
            ADD CONSTRAINT fk_models
                FOREIGN KEY (SeriesId) 
                REFERENCES Models (SeriesId);''')
        cur.execute('''ALTER TABLE Forecasts
            ADD CONSTRAINT unique_seriesid_modelrundate_modelrun_valuetimeutc_publishedatutc
            UNIQUE (seriesid,modelrundate, modelrun,valuetimeutc, publishedatutc);''')
        conn.commit()

    except Exception as e:
        print(f'Set constraints error: {e}')
    finally:
        cur.close
        conn.close()

获取所有约束的脚本

def get_constraints(tablename):
    cur = conn.cursor()
    sql = f'''SELECT con.*
        FROM pg_catalog.pg_constraint con
        INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
        INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
        WHERE  rel.relname = '{tablename}';'''
    cur.execute(sql)
    for record in cur:
        print(record)
k5ifujac

k5ifujac1#

正如@Richard Huxton所说,你不需要担心pg_constraint条目。然而,短暂地

(33129, 'forecasts_pkey', 2200, 'p', ...

手段;

33129 -> unique object identifier for the constraint
forecasts_pkey-> the constraint name
2200 -> public schema' s oid
p -> tells you about the type of the constraint, so for this one, it is primary

而且还在继续有关详细信息,请查看pg_contraint
要快速了解数据库对象,使用psql、pgadmin或heidisql等管理工具可能更好、更容易。除此之外,要使用函数获取定义,可以使用pg_get_constraintdef作为约束,pg_get_indexdef作为索引。

相关问题