pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
decompose big queries
7+ messages / 7 participants
[nested] [flat]

* decompose big queries
@ 2018-04-05 22:39 hmidi slim <hmidi.slim2@gmail.com>
  2018-04-05 22:51 ` Re: decompose big queries David G. Johnston <david.g.johnston@gmail.com>
  2018-04-06 13:06 ` Re: decompose big queries Edson Carlos Ericksson Richter <richter@simkorp.com.br>
  2018-04-27 15:18 ` Re: decompose big queries Steven Lembark <lembark@wrkhors.com>
  0 siblings, 3 replies; 7+ messages in thread

From: hmidi slim @ 2018-04-05 22:39 UTC (permalink / raw)
  To: pgsql-general

Hi,
I want to know what are the best practice to use in order to decompose a
big query which contains so many joins.Is it recommended to use stored
procedures ? or is there any other solution?

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: decompose big queries
  2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
@ 2018-04-05 22:51 ` David G. Johnston <david.g.johnston@gmail.com>
  2 siblings, 0 replies; 7+ messages in thread

From: David G. Johnston @ 2018-04-05 22:51 UTC (permalink / raw)
  To: hmidi slim <hmidi.slim2@gmail.com>; +Cc: pgsql-general

On Thu, Apr 5, 2018 at 3:39 PM, hmidi slim <hmidi.slim2@gmail.com> wrote:

> I want to know what are the best practice to use in order to decompose a
> big query which contains so many joins.Is it recommended to use stored
> procedures ? or is there any other solution?
>

Views are another solution.

https://www.postgresql.org/docs/10/static/tutorial-views.html

Though while "building views upon views" is not uncommon, and it does
increase the readability of each individual view, it add update fragility
to the system.

Encapsulating high-level API concepts in functions and then writing queries
within those functions with the benefit of views is one possibility.  Your
client access needs and general data model are necessary to choose a
preferred design.

David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: decompose big queries
  2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
@ 2018-04-06 13:06 ` Edson Carlos Ericksson Richter <richter@simkorp.com.br>
  2018-04-06 13:35   ` Re: decompose big queries pinker <pinker@onet.eu>
  2 siblings, 1 reply; 7+ messages in thread

From: Edson Carlos Ericksson Richter @ 2018-04-06 13:06 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Em 05/04/2018 19:39, hmidi slim escreveu:
> Hi,
> I want to know what are the best practice to use in order to decompose 
> a big query which contains so many joins.Is it recommended to use 
> stored procedures ? or is there any other solution?
>

I don't know if there are best practices (each scenario requires its own 
solution), but for plain complex SELECT queries, I do use "WITH" 
queries... They work really well.

https://www.postgresql.org/docs/9.6/static/queries-with.html


Regards,

Edson




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: decompose big queries
  2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
  2018-04-06 13:06 ` Re: decompose big queries Edson Carlos Ericksson Richter <richter@simkorp.com.br>
@ 2018-04-06 13:35   ` pinker <pinker@onet.eu>
  2018-04-06 14:03     ` Re: decompose big queries Melvin Davidson <melvin6925@gmail.com>
  2018-04-06 14:09     ` Re: decompose big queries Steven Hirsch <snhirsch@gmail.com>
  0 siblings, 2 replies; 7+ messages in thread

From: pinker @ 2018-04-06 13:35 UTC (permalink / raw)
  To: pgsql-general

Edson Carlos Ericksson Richter wrote
> I don't know if there are best practices (each scenario requires its own 
> solution), but for plain complex SELECT queries, I do use "WITH" 
> queries... They work really well.

Be cautious with CTE's. They weren't meant to be an alternative to
subqueries and will probably change the way your query is executed, because
they are optimisation fences:
https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: decompose big queries
  2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
  2018-04-06 13:06 ` Re: decompose big queries Edson Carlos Ericksson Richter <richter@simkorp.com.br>
  2018-04-06 13:35   ` Re: decompose big queries pinker <pinker@onet.eu>
@ 2018-04-06 14:03     ` Melvin Davidson <melvin6925@gmail.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Melvin Davidson @ 2018-04-06 14:03 UTC (permalink / raw)
  To: pinker <pinker@onet.eu>; +Cc: pgsql-general

On Fri, Apr 6, 2018 at 9:35 AM, pinker <pinker@onet.eu> wrote:

> Edson Carlos Ericksson Richter wrote
> > I don't know if there are best practices (each scenario requires its own
> > solution), but for plain complex SELECT queries, I do use "WITH"
> > queries... They work really well.
>
> Be cautious with CTE's. They weren't meant to be an alternative to
> subqueries and will probably change the way your query is executed, because
> they are optimisation fences:
> https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/
>
>
>
> --
> Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f184378
> 0.html
>
>
Often times, large queries like the example you have provided
were written as a generic query to obtain information for an
application. So developers should be cautioned to write queries
that are specific to the data needed for each situation.

Objectively speaking, you should look at two main areas.
First, examine the WHERE clause. Sometimes there are
redundant restrictions which can be removed.
Next, look at the columns that are selected. Are all those
columns really needed?
After you have cleaned the WHERE clause and columns, you
may find it is no longer necessary to join so many tables.

On Fri, Apr 6, 2018 at 9:35 AM, pinker <pinker@onet.eu> wrote:

> Edson Carlos Ericksson Richter wrote
> > I don't know if there are best practices (each scenario requires its own
> > solution), but for plain complex SELECT queries, I do use "WITH"
> > queries... They work really well.
>
> Be cautious with CTE's. They weren't meant to be an alternative to
> subqueries and will probably change the way your query is executed, because
> they are optimisation fences:
> https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/
>
>
>
> --
> Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f184378
> 0.html
>
>


-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!


-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: decompose big queries
  2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
  2018-04-06 13:06 ` Re: decompose big queries Edson Carlos Ericksson Richter <richter@simkorp.com.br>
  2018-04-06 13:35   ` Re: decompose big queries pinker <pinker@onet.eu>
@ 2018-04-06 14:09     ` Steven Hirsch <snhirsch@gmail.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Steven Hirsch @ 2018-04-06 14:09 UTC (permalink / raw)
  To: pinker <pinker@onet.eu>; +Cc: pgsql-general

On Fri, 6 Apr 2018, pinker wrote:

> Edson Carlos Ericksson Richter wrote
>> I don't know if there are best practices (each scenario requires its own
>> solution), but for plain complex SELECT queries, I do use "WITH"
>> queries... They work really well.

> Be cautious with CTE's. They weren't meant to be an alternative to 
> subqueries and will probably change the way your query is executed, 
> because they are optimisation fences: 
> https://blog.2ndquadrant.com/postgresql-ctes-are-optimization-fences/

+1

Recently I had a poorly performing view speed up by a factor of 6x when 
converted from CTEs to to nested subqueries.  In my case, the lack of 
predicate push-down was a real killer.  Pg would labor away to produce an 
enormous intermediate result that was then selected down to a rather 
modest final result set.  Showed up clearly in the access plan, however, 
and wasn't much of a problem to fix.


-- 




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: decompose big queries
  2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
@ 2018-04-27 15:18 ` Steven Lembark <lembark@wrkhors.com>
  2 siblings, 0 replies; 7+ messages in thread

From: Steven Lembark @ 2018-04-27 15:18 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; +Cc: lembark@wrkhors.com


> Hi,
> I want to know what are the best practice to use in order to
> decompose a big query which contains so many joins.Is it recommended
> to use stored procedures ? or is there any other solution?

The main problem with SP's is that they can really screw up
optimization. Most of the time you'll get more bang for the
buck by managing the SQL, breaking it into more manageable 
chunks.

Views can be a big help for this. Materialized views can help
by pushing lock-heavy or long-running subqueries into the wee
hours where resources can be managed a bit more easily (e.g.,
you don't collide with interactive queries or may be able to
just lock the tables and be done with it). They also give you 
a more stable dataset to start with (e.g., no need to worry
about inserts causing disconnects in totals or requiring 
table locks to avoid).

Temporary tables can help by breaking out messy portions of
the query and allowing you to pre-filter portions of the result.

Scratch tables (i.e., a permenant table that gets truncated after
use) can also help by staging portions of the query in indexed
areas. With careful use they can be a big help because you can 
pre-aggregate portions of the query into a table with nice, 
specific indexes. 

Many of the worst queries involve reporting on aggregates over time
that are re-calculated for each report. Pre-aggregating on, say, a
daily basis can both simplify the reporting query and allow you to
push some of the work off to the wee hours. You can also get the 
benefit of more specific values that may allow for unique indexes
on the subtotals. 

If the query involves "many" joins there is a good chance that 
they break into clumps of related data. Breaking, say, daily usage
vs. user account vs. accounting data into separate [materialized]
views or temp tables keeps the joins more manageable and helps
release resources that might otherwise get consumed for the entire
query.

-- 
Steven Lembark                                       1505 National Ave
Workhorse Computing                                 Rockford, IL 61103
lembark@wrkhors.com                                    +1 888 359 3508




^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2018-04-27 15:18 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-05 22:39 decompose big queries hmidi slim <hmidi.slim2@gmail.com>
2018-04-05 22:51 ` David G. Johnston <david.g.johnston@gmail.com>
2018-04-06 13:06 ` Edson Carlos Ericksson Richter <richter@simkorp.com.br>
2018-04-06 13:35   ` pinker <pinker@onet.eu>
2018-04-06 14:03     ` Melvin Davidson <melvin6925@gmail.com>
2018-04-06 14:09     ` Steven Hirsch <snhirsch@gmail.com>
2018-04-27 15:18 ` Steven Lembark <lembark@wrkhors.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox