2017-09-14 15:09 GMT+02:00 Pavel Stehule <pavel.stehule@xxxxxxxxx>:
> The PostgreSQL cannot to push join - in slow case, the UNIONS should
be done first - and it requires full scan ar_tran_inv - used filter (posted AND
(deleted_id = 0) is not too effective - maybe some composite or partial index
helps.
>
> The fast query doesn't contains unions - so there are bigger space for
optimizer - ar_tran_inv is filtered effective - by primary key.
>
> So main problem is impossible to push information a.row_id = 1 to deep
to query.
> Sorry for banging on about this, but someone might be interested in the
following timings.
The only solution I could find was to ‘denormalise’ (if that is a word) and
create additional columns on ar_trans_due for cust_row_id and tran_date, to
avoid using any joins.
Once I had done that, I could run my query two ways –
1. using the newly created columns
2. as before, using a join to the view, which in turn retrieved data
from the underlying tables.
This was a more complex query than the example above – details available on
request.
Here are the timings for running the query on identical data sets using
Postgresql, Sql Server, and Sqlite3 -
PostgreSQL -
Method 1 - 0.28 sec
Method 2 – 1607 sec, or 26 minutes
Sql Server -
Method 1 – 0.33 sec
Method 2 – 1.8 sec
Sqlite3 -
Method 1 – 0.15 sec
Method 2 – 1.0 sec
It seems that Sql Server and Sqlite3 are able to analyse the ‘join’, and
execute an indexed read against the underlying physical tables.
Frank
|