简体   繁体   English

POSTGRES - 子查询返回结果非常慢

[英]POSTGRES - Sub Query returns the result very Slow

I've a fairly simple query, but it includes a Sub - Query.我有一个相当简单的查询,但它包括一个子查询。 I want to fetch list of resource_id which pass through a filter in ORDER BY DESC;我想获取通过ORDER BY DESC;过滤器的resource_id列表ORDER BY DESC; order.命令。

MORE INFORMATION Basically we need to find the list of resource_id's which pass through a filter mentioned in the query below,更多信息基本上我们需要找到通过下面查询中提到的过滤器的resource_id's列表,

   SELECT rs.resource_id
FROM resource rs
WHERE (
    SELECT rc.resource_id
    FROM risk_child rc
    WHERE rc.resource_id = rs.resource_id 
    AND rc.cloudaccount_id = rs.cloud_account_id
    AND rs.reg_id= any(array[236]) 
    AND rc.risk_level= any(array['high','low'])
    AND rc.status = any(array['fail'])
    AND rc.cloudaccount_id= any (array['4ZiCmwslbjhmRtHAOjLG'])
    ORDER BY rc.id DESC
    LIMIT 1
) = rs.resource_id

these resources would then be passed into another query as mentioned here:然后,这些资源将被传递到此处提到的另一个查询中:

SELECT
  DISTINCT ON (rc.resource_id, rc.rule_id, s.id) MAX(rc.creationtime) as creationtime,
  rc.resource_id,
  rl.rule_tag,
  s.service,
  r.region,
  rc.status,
  rs.vpc_id,
  rc.cloudaccount_id,
  rc.organization_id,
  rs.owner_id,
  rc.description,
  f.function_name,
  g.group_name,
  rc.risk_level,
  rc.id,
  rc.user_id,
  rc.pro_id,
  c.category_name,
  rc.raw as rawResponse,
  rs.res_ca_id,
  rs.resource_name
FROM
  risk_child rc,
  resource rs,
  rule rl,
  service s,
  region r,
  function f,
  g_by g,
  category c
WHERE
  rc.resource_id = rs.resource_id
  AND rl.id = rc.rule_id
  AND s.id = rs.ser_id
  AND rs.reg_id = r.id
  AND f.id = rc.function_id
  AND c.id = rc.category_id
  AND g.id = rc.group_id
  AND rc.cloudaccount_id like any (array $ { modifiedCloudAccounts })
  AND rc.organization_id = $ { orgId }
  AND rc.rule_id > 0
  AND rc.cloudaccount_id = rs.cloud_account_id
  AND rs.resource_id like any (array $ { getResources }) $ { risk }
GROUP BY
  rc.rule_id,
  rc.creationtime,
  rc.creationtime,
  rc.resource_id,
  rl.rule_tag,
  rl.id,
  s.service,
  r.region,
  rc.status,
  rs.vpc_id,
  rc.cloudaccount_id,
  rc.organization_id,
  rs.owner_id,
  rc.description,
  f.function_name,
  g.group_name,
  rc.risk_level,
  rc.id,
  rc.user_id,
  rc.pro_id,
  c.category_name,
  rc.raw,
  s.id,
  rs.res_ca_id
ORDER BY
  rc.resource_id,
  rc.rule_id ASC;

PROBLEM Now the first query returns the result very slow, even after indexing the result comes in 5 - 6 seconds.问题现在第一个查询返回结果非常慢,即使在索引结果后 5 - 6 秒。 So keeping in mind the first query needs to run two times所以请记住第一个查询需要运行两次

  1. One to get total number of Rows (For pagination)一获取总行数(用于分页)
  2. Second time to get the resource_ids第二次获取resource_ids

I've mainly used NO - SQL for my apps so I am fairly new to SQL querying.我主要在我的应用程序中使用了 NO - SQL,所以我对 SQL 查询还很陌生。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

So finally this thing worked for my case:所以最后这件事对我的情况有用:

    SELECT rs.resource_id
FROM resource rs
WHERE EXISTS (SELECT *
              FROM risk_child rc
              WHERE rc.resource_id = rs.resource_id 
                AND rc.cloudaccount_id = rs.cloud_account_id
                AND rs.reg_id= any(array[236]) 
                AND rc.risk_level= any(array['high','low'])
                AND rc.status = any(array['fail'])
                AND rc.cloudaccount_id= any (array['4ZiCmwslbjhmRtHAOjLG'])
             )

Basically as I had explained this in my post I am fairly new to SQL, I didn't put proper indexing in my tables so I had to add the following indexes to make the query faster基本上,正如我在我的帖子中解释的那样,我对 SQL 还很陌生,我没有在我的表中放置正确的索引,所以我不得不添加以下索引以使查询更快

resource(resource_id, cloud_account_id) and risk_child(resource_id, cloudaccount_id) this helped me improve my performance even more. resource(resource_id, cloud_account_id)risk_child(resource_id, cloudaccount_id)这帮助我进一步提高了我的表现。

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

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