简体   繁体   English

多个表的 SQL Postgres 查询速度非常慢

[英]SQL Postgres query very slow with multiple tables

I am using Postgres.我正在使用 Postgres。

Schema架构

+------------+  +-----------+  +---------------------+
| employee   |  | contact   |  | powwowpersalmapping |
+------------+  +-----------+  +---------------------+
| member_id  |  | member_id |  | id_number           |
| staff_code |  | idnumber  |  | persal_number       |
+------------+  +-----------+  +---------------------+

Data数据

在此处输入图片说明

Goal目标

As you can see, the staff_code is blank, so I am trying to set the staff_code on the employee table with the persal_number on the powwowpersalmapping table.如您所见, staff_code为空,因此我尝试使用staff_code表上的persal_number设置employee表上的powwowpersalmapping

Question

How do I construct the UPDATE query to copy the persal_number to the staff_code ?如何构造UPDATE查询以将persal_number复制到staff_code

在此处输入图片说明

Problem问题

I am creating an UPDATE query, but the equivalent SELECT query is very slow, so I think the UPDATE query will be slow too.我正在创建一个UPDATE查询,但等效的SELECT查询非常慢,所以我认为UPDATE查询也会很慢。

SQL SQL

I have the following:我有以下几点:

If I run this SELECT query with table joins.如果我使用表连接运行此SELECT查询。 it runs pretty fast.它运行得非常快。

SELECT e.* FROM employee e
INNER JOIN contact c ON c.member_id = e.member_id 
INNER JOIN powwowpersalmapping m ON m.id_number = c.idnumber
WHERE e.staff_code is null or coalesce(e.staff_code, '') = '';

Then I run this SELECT query with multiple tables (no joins).然后我用多个表(没有连接)运行这个SELECT查询。 it runs very slow.它运行得很慢。

SELECT e.* FROM employee e
, contact c, powwowpersalmapping m 
WHERE c.member_id = e.member_id 
AND m.id_number = c.idnumber
AND e.staff_code is null or coalesce(e.staff_code, '') = '';

So I am constructing an UPDATE query (not run yet), and have the following so far, but I am sure it will also be very slow.所以我正在构建一个UPDATE查询(还没有运行),到目前为止有以下内容,但我相信它也会很慢。

UPDATE employee e
SET e.staff_code = m.persal_number
FROM contact c, powwowpersalmapping m 
WHERE c.member_id = e.member_id 
AND m.id_number = c.idnumber
AND e.staff_code is null or coalesce(e.staff_code, '') = '';

How about the following?以下情况如何?

UPDATE employee e
SET e.staff_code = (
    SELECT m.persal_number FROM employee e
    INNER JOIN contact c ON c.member_id = e.member_id 
    INNER JOIN powwowpersalmapping m ON m.id_number = c.idnumber
    WHERE e.staff_code is null or coalesce(e.staff_code, '') = ''
);

You can use joins in the UPDATE statement after the FROM clause:您可以在FROM子句之后的UPDATE语句中使用连接:

UPDATE employee e
SET staff_code = m.persal_number
FROM contact c INNER JOIN powwowpersalmapping m 
ON m.id_number = c.idnumber
WHERE c.member_id = e.member_id AND COALESCE(e.staff_code, '') = '';

Since the SELECT query runs fast (I believe there are indexes for all the related columns) this will also run fast.由于SELECT查询运行得很快(我相信所有相关列都有索引)这也将运行得很快。
If you could also get rid of the COALESCE() function it would be even faster.如果您还可以摆脱COALESCE()函数,它会更快。
Is the column staff_code empty?staff_code是否为空? If yes, then remove AND COALESCE(e.staff_code, '') = '' .如果是,则删除AND COALESCE(e.staff_code, '') = ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM