[firebird-support] Equality on NULL column values

2014-10-31 Thread 'Louis van Alphen' lo...@nucleo.co.za [firebird-support]
Hello all, I have a table that contains columns that describe an item. E.g.

 

ITEM table

  ID bigint not null

  COLOUR bigint  not null  (indexed)

  FINISH bigint (indexed)

 

Then another table that contains a qty of items that is reserved. I.e. the 
specification and qty of items reserved. Colour is mandatory and Finish is 
optional

 

RESERVATION table

  ID bigint not null

  COLOUR bigint  not null  (indexed)

  FINISH bigint (indexed)

  QTY integer not null

 

 

Now to determine available stock (item count - reservations) I issue the 
following query:

 

select

  STOCK.COLOUR,

  STOCK.FINISH,

  STOCK. STOCK_QTY,

  R.QTY as RESERVED_QTY,

  STOCK. STOCK_QTY - R.QTY as RESERVED_QTY as AVAILABLE_QTY

  from

   (

/* Get summary of item stock */

   select COLOUR,FINISH,count(*) as STOCK_QTY

  from ITEM

 group by COLOUR,FINISH

) STOCK

 

left outer join RESERVATION R /* Get Reservations */

  on R.COLOUR = STOCK.COLOUR  --  Match mandatory field

  and coalesce(R.FINISH,-1) = coalesce(STOCK.FINISH,-1)  --  Match optional 
field

 

The reason for using the coalesce in the join is to force NULL Finishes to 
match. The problem is that the query plan does not use the indices on FINISH 
because of the coalesce. This results in HUGE number of reads on the 
RESERVATION table. Think it table scans the RESERVATION table for each ITEM 
row. I tried a computed index, but the query did not use that index. The only 
(non FB) way to optimise the query is to actually refactor the tables and 
extract a SPECIFICATION table. In my case this will mean a HUGE amount of work 
and not feasible at this stage.

 

So my question is: How to optimise this query given that I use FB 2.5.3…?

 

Thanks

 

 

 

 

 

 

 



[firebird-support] Re: Equality on NULL column values

2014-10-31 Thread Dmitry Yemanov dim...@users.sourceforge.net [firebird-support]
31.10.2014 09:03, 'Louis van Alphen' wrote:

 and coalesce(R.FINISH,-1) = coalesce(STOCK.FINISH,-1)

and R.FINISH is not distinct from STOCK.FINISH


Dmitry





RE: [firebird-support] Re: Equality on NULL column values

2014-10-31 Thread 'Louis van Alphen' lo...@nucleo.co.za [firebird-support]
Thanks! Never too old to learn

 

From: firebird-support@yahoogroups.com 
[mailto:firebird-support@yahoogroups.com] 
Sent: 31 October 2014 09:13 AM
To: firebird-support@yahoogroups.com
Subject: [firebird-support] Re: Equality on NULL column values

 

  

31.10.2014 09:03, 'Louis van Alphen' wrote:

 and coalesce(R.FINISH,-1) = coalesce(STOCK.FINISH,-1)

and R.FINISH is not distinct from STOCK.FINISH

Dmitry





[Non-text portions of this message have been removed]