我正在尝试使用 PostgreSQL 数据库在 PHP 中开发一个问答网站。我有一个操作来创建一个具有标题、正文、类别和标签的页面。我设法插入了所有这些字段,但是在插入多个标签值时遇到了一些问题。
我使用这个函数将逗号分隔的值放入一个数组中,现在我想要将每个数组元素插入到表上的数据库中(避免重复),tags
然后插入到我的多对多关系表中questiontags
:
$tags = explode(',', $_POST['tags']); //Comma separated values to an array
Run Code Online (Sandbox Code Playgroud)
它打印出这样的东西:
Array ( [0] => hello [1] => there [2] => this [3] => is [4] => a [5] => test )
Run Code Online (Sandbox Code Playgroud)
动作/create_question.php
$category = get_categoryID_by_name($_POST['category']);
$question = [
'userid' => auth_user('userid'),
'body' => $_POST['editor1'],
'title' => $_POST['title'],
'categoryid' => $category
];
create_question($question, $tags);
Run Code Online (Sandbox Code Playgroud)
然后我create_question
应该在哪里插入标签。
function create_question($question, $tags) {
global $conn;
$query_publications=$conn->prepare("SELECT * FROM insert_into_questions(:body, :userid, :title, :categoryid); …
Run Code Online (Sandbox Code Playgroud) 我有自定义类型
CREATE TYPE mytype as (id uuid, amount numeric(13,4));
Run Code Online (Sandbox Code Playgroud)
我想将它传递给具有以下签名的函数:
CREATE FUNCTION myschema.myfunction(id uuid, mytypes mytype[])
RETURNS BOOLEAN AS...
Run Code Online (Sandbox Code Playgroud)
如何在postgres查询中调用它,并且不可避免地从PHP中调用?
根据此评论的建议,我构建了一个 intarray GIN 索引。我什至set local enable_seqscan = 'off';
。不过,当我EXPLAIN
查询时,它正在进行顺序扫描。为了演示,我创建了一个名为 的虚拟表deletethis
。
devapp=>
devapp=> \d deletethis;
Table "public.deletethis"
Column | Type | Collation | Nullable | Default
--------+-----------+-----------+----------+---------
col1 | integer[] | | |
Indexes:
"deletethis_idx" gin (col1 gin__int_ops)
devapp=>
devapp=>
devapp=> BEGIN; set local enable_seqscan = False; SHOW enable_seqscan; EXPLAIN SELECT * from deletethis where 1 = ANY(col1); COMMIT;
BEGIN
SET
enable_seqscan
----------------
off
(1 row)
Run Code Online (Sandbox Code Playgroud)
QUERY PLAN
-------------------------------------------------------------------------------
Seq Scan on deletethis (cost=10000000000.00..10000000040.60 …
Run Code Online (Sandbox Code Playgroud)