Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:19:39+00:00 2026-05-25T15:19:39+00:00

i need optimize this query, please see the comented line: SELECT p.NUM_PROCESSO, p.NUM_PROC_JUD, p.Num_Proc_Jud_Antigo1,

  • 0

i need optimize this query, please see the comented line:

SELECT p.NUM_PROCESSO,
       p.NUM_PROC_JUD,
       p.Num_Proc_Jud_Antigo1,
       p.Num_Proc_Jud_Antigo2,
       p.Num_Proc_Jud_Novo,
       a.assunto,
       su.subassunto,
       u.UNIDADE,
       s.SERVIDOR,
       dvj.data_vinc,
       p.TIPO,
       c.DESC_CLASSIF
FROM   processo p
       LEFT OUTER JOIN assunto a
         ON a.cod_assunto = p.cod_assunto
       LEFT OUTER JOIN subassunto su
         ON su.cod_subassunto = p.cod_subassunto
       LEFT OUTER JOIN Distrib_VincJud dvj
         ON dvj.num_processo = p.num_processo
       LEFT OUTER JOIN servidor s
         ON S.COD_SERVIDOR = dvj.COD_SERVIDOR
       LEFT OUTER JOIN unidade u
         ON u.COD_UNIDADE = s.COD_UNIDADE
       LEFT OUTER JOIN Classif_Processo c
         ON C.COD_CLASSIF = p.COD_CLASSIF
WHERE  p.TIPO = 'J'
       AND p.NUM_PROCESSO NOT IN (SELECT d.num_processo
                                  FROM   distribuicao d
                                  WHERE  d.COD_SERVIDOR in ( '0', '000' )
                                         AND d.num_distribuicao IN
                                             (SELECT MAX(num_distribuicao)
                                              FROM   Distribuicao
                                              GROUP  BY num_processo)
                                         --this suquery return 100k lines !!! and consume all CPU:
                                         AND dvj.id_vinc IN
                                                (SELECT MAX(id_vinc)
                                                FROM Distrib_VincJud
                                                where  ativo = '1'
                                                GROUP  BY num_processo))
       AND p.NUM_PROCESSO NOT IN (SELECT num_processo
                                  FROM   Anexos)
       AND s.ATIVO = 1  

my horrible solution at this moment: http://pastebin.com/C4PHNsSc

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:19:39+00:00Added an answer on May 25, 2026 at 3:19 pm

    What I would do is convert the IN and NOT IN into joins:

    SELECT p.NUM_PROCESSO, p.NUM_PROC_JUD, p.Num_Proc_Jud_Antigo1,
        p.Num_Proc_Jud_Antigo2, p.Num_Proc_Jud_Novo, a.assunto,
        su.subassunto, u.UNIDADE, s.SERVIDOR, dvj.data_vinc, p.TIPO,
        c.DESC_CLASSIF
    FROM
        processo p
        INNER JOIN (
            SELECT p.num_processo,
                CASE WHEN dvj.id_vinc IS NOT NULL
                    AND d.num_distribuicao IS NOT NULL
                    OR a.num_processo IS NOT NULL THEN
                    1
                ELSE
                    0
                END exclude
            FROM
                processo p
                LEFT JOIN Anexos a
                    ON p.num_processo = a.num_processo
                LEFT JOIN (
                    SELECT num_processo,
                        MAX(num_distribuicao) AS max_distribuicao
                    FROM Distribuicao
                    GROUP BY num_processo
                ) md ON p.num_processo = md.num_processo
                LEFT JOIN (
                    SELECT num_processo, MAX(id_vinc) AS max_vinc
                    FROM Distrib_VincJud
                    WHERE ativo = '1'
                    GROUP BY num_processo
                ) mv on p.num_processo = mv.num_processo
                LEFT JOIN distribuicao d
                    ON p.num_processo = d.num_processo
                        AND md.max_distribuicao = d.num_distribuicao
                LEFT JOIN Distrib_VincJud dvj
                    ON p.num_processo = dvj.num_processo
                        AND mv.max_vinc = dvj.id_vinc
            WHERE d.COD_SERVIDOR in ('0', '000')
        ) IncExc
            ON p.num_processo = IncExc.num_processo
        LEFT OUTER JOIN assunto a
            ON a.cod_assunto = p.cod_assunto
        LEFT OUTER JOIN subassunto su
            ON su.cod_subassunto = p.cod_subassunto
        LEFT OUTER JOIN Distrib_VincJud dvj
            ON dvj.num_processo = p.num_processo
        LEFT OUTER JOIN servidor s
            ON S.COD_SERVIDOR = dvj.COD_SERVIDOR
        LEFT OUTER JOIN unidade u
            ON u.COD_UNIDADE = s.COD_UNIDADE
        LEFT OUTER JOIN Classif_Processo c
            ON C.COD_CLASSIF = p.COD_CLASSIF
    WHERE
        p.TIPO = 'J'
        AND IncExc.exclude = 0
        AND s.ATIVO = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would need some help on how to optimize the query. select * from
How can I optimize this mysql query: SELECT * FROM messages WHERE toid='.$mid.' AND
Trying to optimize this query, with a corollated subquery SELECT g.*, g.instaLikes + (
I'm in a need to optimize this really tiny, but pesky function. unsigned umod(int
I need to optimize a query for a ranking that is taking forever (the
I need to optimize a query that is being produced by a save (insert
The query I need help with is: SELECT d.bn, d.4700, d.4500, ... , p.`Activity
I just can't figure out how to optimize this into one query in HQL.
I need to know if there's a best way to optimize this kind of
I'm trying to optimize this query, and as simple as it sounds, I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.