是否可以INSERT
从SELECT
语句DEFAULT
中将值放入 PostgreSQL 表并使用空列的值?
就我而言,该SELECT
语句是从 JSON 中选择的。
在下面的尝试中,我通过显式检索序列取得了成功,但我希望有一种方法可以使用 DEFAULT 值进行 INSERT。或者选择 DEFAULT 值而不必显式调用默认函数。
-- Example table
create table animals
(
id serial,
nm character varying (30) NOT NULL, --name
typ character varying(10),
tvi integer,
tvf numeric(8,3)
);
insert into animals VALUES (DEFAULT,'mouse','m',4,12.45);
select row_to_json(a) from animals a;
select * from json_populate_record(null::animals,'{"id":null,"nm":"mouse","typ":"m","tvi":4,"tvf":12.450}');
--All good.
-- Attempt #1
INSERT INTO animals
select id ,nm,typ,tvi,tvf from json_populate_record(null::animals,'{"id":null,"nm":"mouse","typ":"m","tvi":4,"tvf":12.450}');
/*
ERROR: null value in column "id" violates not-null …
Run Code Online (Sandbox Code Playgroud) 我正在插入一个具有 5 个属性和 1000 行的简单小表。
我观察到当引擎是 INNODB 时,每个插入需要 0.03 - 0.05 秒。我将引擎更改为 MYISAM,然后插入速度更快。它需要 0.001 - 0.003。
问题是什么。默认情况下 innodb_flush_log_trx_commit = 1。我就是这个设置。这是我的 innodb 设置。
innodb_log_buffer_size : 1MB
innodb_log_file_size : 5MB
innodb_buffer_pool_size: 8MB
innodb_flush_log_trx_commit = 1
mysql> desc table ;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| count | int(10) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 …
Run Code Online (Sandbox Code Playgroud) 我必须将 .pdf 文件存储在表格中。
我有一个表,state
列:
id_state,
name,
pdffile (bytea)
Run Code Online (Sandbox Code Playgroud)
我想将 pdf 文件存储在 pdffile 列中。
我怎样才能做到这一点?
我正在从事一个项目,该项目将测量文件中的数据解析到 Posgres 9.3.5 数据库中。
核心是一个表(按月分区),其中包含每个测量点的一行:
CREATE TABLE "tblReadings2013-10-01"
(
-- Inherited from table "tblReadings_master": "sessionID" integer NOT NULL,
-- Inherited from table "tblReadings_master": "fieldSerialID" integer NOT NULL,
-- Inherited from table "tblReadings_master": "timeStamp" timestamp without time zone NOT NULL,
-- Inherited from table "tblReadings_master": value double precision NOT NULL,
CONSTRAINT "tblReadings2013-10-01_readingPK" PRIMARY KEY ("sessionID", "fieldSerialID", "timeStamp"),
CONSTRAINT "tblReadings2013-10-01_fieldSerialFK" FOREIGN KEY ("fieldSerialID")
REFERENCES "tblFields" ("fieldSerial") MATCH SIMPLE
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT "tblReadings2013-10-01_sessionFK" FOREIGN KEY ("sessionID")
REFERENCES "tblSessions" ("sessionID") …
Run Code Online (Sandbox Code Playgroud) 在数据仓库中,我将一个事实表连接到 20 个维度。事实表有 3200 万行和 30 列。这是一个临时登台表,因此我不必处理其他用户读取或写入该表。我从基表中选择 10 列,从相应维度中选择 20 列。维度表很小(在 3 到 15.000 行之间)。连接的字段是整数和 nvarchars。我使用 SELECT ... INTO 语句。表上没有索引。
此查询的执行速度太慢而无用。
由于查询处理时间太长,我尝试了以下解决方案:
这些发现使我将实际执行计划包括在内,该计划表明 89% 的成本在于表插入。其他成本是 8% 的事实表表扫描和 2% 的内连接哈希匹配。
这是我面临的奇怪问题。我正在尝试使用以下查询输入数据
insert into product_product
(id, product_tmpl_id, make_equip, model_equip, name_template, serial_num_equip, location_equip, issue_date_equip, issue_to_equip, remarks_equip, pr, ch, categ_id,valuation)
values (700,700,'Nikon','Action 10x50 Lookout','Nikon Action 10x50 Lookout','671386','40 Wall St.','5/13/2004 12:00:00 AM','','OM''s OFFICE',62,72,502,'manual periodic');
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ERROR: duplicate key value violates unique constraint "product_product_pkey"
DETAIL: Key (id)=(700) already exists.
********** Error **********
ERROR: duplicate key value violates unique constraint "product_product_pkey"
SQL state: 23505
Detail: Key (id)=(700) already exists.
Run Code Online (Sandbox Code Playgroud)
我在该记录上运行选择查询,如下所示:
select * from product_product
where id=700
Run Code Online (Sandbox Code Playgroud)
它返回没有数据的列(数据也包括 id)
我尝试像这样运行更新查询:
update product_product set
product_tmpl_id=700,
make_equip='Nikon',
model_equip='Action 10x50 …
Run Code Online (Sandbox Code Playgroud) 我只是想知道 SQL 语句INSERT INTO TABLE1 SELECT * FROM TABLE2
,会像批量插入一样工作吗?
如果没有,有没有办法在插入记录时排除索引。进程在一次执行中插入 1.5 亿条数据。
我们计划创建阶段表(不会有任何索引Table1
),然后将其从阶段表转移到目标表(会有索引Table2
)
我们不是在从过程中创建平面文件的情况。
但是当我们将数据从Table1
(未编入索引)传输到Table2
(编入索引)时,我们正在寻找可以加快处理速度的方法。
任何方式使用BulkInsert
自Table1
到Table2
?
我想从 ARGV[] 转换到 PostgreSQL 中的 int 数组,我在代码中用TODO标记了伪代码。x86_64-unknown-linux-gnu 上 PostgreSQL 9.4.3 中的代码,由 gcc (Debian 4.9.2-10) 4.9.2 编译,64 位:
CREATE TABLE measurements (
measurement_id SERIAL PRIMARY KEY NOT NULL,
measurement_size_in_bytes INTEGER NOT NULL
);
CREATE TABLE events (
event_id SERIAL PRIMARY KEY NOT NULL,
measurement_id INTEGER NOT NULL,
event_index_start INTEGER NOT NULL,
event_index_end INTEGER NOT NULL
);
CREATE OR REPLACE FUNCTION insaft_function()
RETURNS TRIGGER AS
$func$
BEGIN
-- TODO Loop until TG_ARGV[0] empty
INSERT INTO events (measurement_id, event_index_start, event_index_end)
SELECT …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Postgres 9.3,我需要根据表中已有的特定行数来防止插入到表中。这是表:
Table "public.team_joins"
Column | Type | Modifiers
-----------------+--------------------------+---------------------------------------------------------
id | integer | not null default nextval('team_joins_id_seq'::regclass)
team_id | integer | not null
Indexes:
"team_joins_pkey" PRIMARY KEY, btree (id)
"team_joins_team_id" btree (team_id)
Foreign-key constraints:
"team_id_refs_teams_id" FOREIGN KEY (team_id) REFERENCES teams(id) DEFERRABLE INITIALLY DEFERRED
Run Code Online (Sandbox Code Playgroud)
因此,例如,如果一个 id 为 3 的团队只允许 20 名玩家,并且SELECT COUNT(*) FROM team_joins WHERE team_id = 3
等于 20,那么没有玩家应该能够加入团队 3。处理这种情况并避免并发问题的最佳方法是什么?我应该使用SERIALIZABLE
事务来插入,还是可以WHERE
在插入语句中使用这样的子句?
INSERT INTO team_joins (team_id)
VALUES (3)
WHERE (
SELECT COUNT(*) FROM team_joins WHERE …
Run Code Online (Sandbox Code Playgroud) 我目前有一个看起来像这样的表:
CREATE TABLE "PDPC".collection
(
col_no bigint NOT NULL DEFAULT nextval('"PDPC".collection_col_no_seq'::regclass),
q1 character varying(10000) COLLATE pg_catalog."default",
q2 character varying(10000) COLLATE pg_catalog."default",
q3 character varying(10000) COLLATE pg_catalog."default",
q4 character varying(10000) COLLATE pg_catalog."default",
dg_fkey bigint,
CONSTRAINT collection_pkey PRIMARY KEY (col_no),
CONSTRAINT collection_dg_fkey_fkey FOREIGN KEY (dg_fkey)
REFERENCES "PDPC".datagroup (dg_no) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE "PDPC".collection
OWNER to postgres;
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 postgresql 在 PHP 中执行 UPSERT 语句,但我收到了
致命错误:未捕获的 PDOException:SQLSTATE[42P10]:无效的列引用:7 …
insert ×10
postgresql ×7
performance ×2
sql-server ×2
array ×1
blob ×1
bulk-insert ×1
cast ×1
concurrency ×1
foreign-key ×1
innodb ×1
join ×1
json ×1
myisam ×1
mysql ×1
optimization ×1
plpgsql ×1
primary-key ×1
select ×1
update ×1