Subject | How can I avoid temporary tables? |
---|---|
Author | Dimitry Sibiryakov |
Post date | 2002-11-06T05:40:15Z |
On 5 Nov 2002 at 18:07, c_pradelli@... wrote:
You also can try to denormalize your data moving sell-date into
items. It would remove one table from SELECT and increase selectivity
of the code-date index.
SELECT p.Code,
(SELECT sum(i.amount) FROM items i JOIN header h
ON i.billnumber=h.billnumber
WHERE i.Code=p.Code AND h.date BETWEEN dateA1 AND dateA2)
AS Sales1,
/* I believe that every item has appropriate header */
(SELECT sum(i.amount) FROM items i JOIN header h
ON i.billnumber=h.billnumber
WHERE i.Code=p.Code AND h.date BETWEEN dateB1 AND dateB2)
AS Sales2
FROM products p
/* I also believe that you want all products codes in result record
set even if this product wasn's sold in both periods */
Or you can try FULL JOIN of your SP_TEMPDATAs if you don't need to
see non-sold product codes.
SY, Dimitry Sibiryakov.
>The table items have about 5 millions of records,Right indices make query near independed from number of records.
>that's why I looking for a
>fast method to do the query.
You also can try to denormalize your data moving sell-date into
items. It would remove one table from SELECT and increase selectivity
of the code-date index.
>------------------------------------------------------------Try this:
>Does anybody knows how to simplify this query, or to
>avoid the use of
>temporary tables?
>any idea???
SELECT p.Code,
(SELECT sum(i.amount) FROM items i JOIN header h
ON i.billnumber=h.billnumber
WHERE i.Code=p.Code AND h.date BETWEEN dateA1 AND dateA2)
AS Sales1,
/* I believe that every item has appropriate header */
(SELECT sum(i.amount) FROM items i JOIN header h
ON i.billnumber=h.billnumber
WHERE i.Code=p.Code AND h.date BETWEEN dateB1 AND dateB2)
AS Sales2
FROM products p
/* I also believe that you want all products codes in result record
set even if this product wasn's sold in both periods */
Or you can try FULL JOIN of your SP_TEMPDATAs if you don't need to
see non-sold product codes.
SY, Dimitry Sibiryakov.