 |
|
 |
|
Next: What is an instance
|
| Author |
Message |
External

Since: Apr 26, 2010 Posts: 4
|
(Msg. 1) Posted: Mon Apr 26, 2010 5:28 am
Post subject: use a generated column in a where clause Archived from groups: comp>databases>ibm-db2 (more info?)
|
|
|
I have a huge table and I have the following requirement :
Table1
---------
client
day_of_sale
qty
Table2
---------
same ddl
Now table2 needs to have the same data except for the first
day_of_sale value for each client.
So I have the query
select client,day_of_sale,qty from table(select
client,day_of_sale,qty, row_number() over(partition by client order by
day_of_sale) as rownum ) from table1 where rownum>1
This gives me a huge cost since its going to build up the inner table
and then select the 'rest' of the columns in the outer table.
To stop doing the two scans (use lesser memory to store the whole
inner table in buffer and then loop through it), what we did was
1. add the row number column in table 2
2. put in a check constraint in the table 2 that has only >1
3. load to table 2
4. set integrity
This eats up the concurrency of our project since the loaded table
can't be used while the load happens.
So I would be very happy if someone can give me a single sql without
CTE to do the above.
We are playing with the OLAP and windowing functions, but no joy till
now.
Any help or suggestions plz >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Apr 26, 2010 Posts: 22
|
(Msg. 2) Posted: Mon Apr 26, 2010 10:25 am
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On 2010-04-26 14:28, Arun Srini wrote:
> I have a huge table and I have the following requirement :
> Table1
> ---------
> client
> day_of_sale
> qty
>
> Table2
> ---------
> same ddl
>
> Now table2 needs to have the same data except for the first
> day_of_sale value for each client.
>
> So I have the query
> select client,day_of_sale,qty from table(select
> client,day_of_sale,qty, row_number() over(partition by client order by
> day_of_sale) as rownum ) from table1 where rownum>1
>
> This gives me a huge cost since its going to build up the inner table
> and then select the 'rest' of the columns in the outer table.
> To stop doing the two scans (use lesser memory to store the whole
> inner table in buffer and then loop through it), what we did was
> 1. add the row number column in table 2
> 2. put in a check constraint in the table 2 that has only >1
> 3. load to table 2
> 4. set integrity
>
> This eats up the concurrency of our project since the loaded table
> can't be used while the load happens.
> So I would be very happy if someone can give me a single sql without
> CTE to do the above.
> We are playing with the OLAP and windowing functions, but no joy till
> now.
>
> Any help or suggestions plz
Just a thought, if I get it right the set of data NOT included in
"table2" is fairly stable, it only grows when a new client is added,
correct?
create table table3 (
client ...,
day_of_sale ...,
primary key (client, day_of_sale)
);
insert into table3 (client,day_of_sale)
select client, min( day_of_sale )
from table1
group by client;
If you can maintain table3 somehow (triggers for example), table 2 would be:
select client,day_of_sale,qty
from table1 x
left join table3 y
on (x.client, x.day_of_sale)
= (y.client, y.day_of_sale)
where y.client is null;
since table3 should be much smaller than table1, it might be an
improvement. As mentioned, just a thought.
/Lennart >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Jan 11, 2008 Posts: 1089
|
(Msg. 3) Posted: Mon Apr 26, 2010 7:56 pm
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Temporal data should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.
Sample data is also a good idea, along with clear specifications. It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html
But the real question is why such a pair of tables, loaded with
redundant data, exist at all!! Just make a VIEW on the larger table
to get the trimmed table for whatever reason you need it. >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Apr 26, 2010 Posts: 4
|
(Msg. 4) Posted: Tue Apr 27, 2010 4:07 am
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Table1
---------
create table table1(client char(5), day_of_sale date, qty bigint);
Table2
---------
Same as table 1
Requirement - table1 is a staging table, gets loaded using an ETL.
Table 2 is the production table.
Due to a problem with client data, the first record for everymonth,
every client,ex.
('Belgium','01/01/2010',-10)
This will always have a negative value, and is junk data. We have
asked the client to rectify this, but while we are waiting for them to
do it, we are planning to put a data cleanse solution ourselves.
So I need to just leave the first row for every month per client
alone, and move the rest
Source
----------
('Belgium','01/01/2010',-10)
('Belgium','01/07/2010',1)
('Belgium','01/04/2010',3)
('Megallen','01/01/2010',-10)
('Megallen','01/21/2010',10)
('Megallen','01/02/2010',4)
('Megallen','01/14/2010',9)
Target
--------
('Belgium','01/07/2010',1)
('Belgium','01/04/2010',3)
('Megallen','01/21/2010',10)
('Megallen','01/02/2010',4)
('Megallen','01/14/2010',9)
This is not only needed for moving the data but also reporting how
many rows are there per month per client. Like
Counts
----------
Jan -2010 Belgium - 2
Jan -2010 Megallen -3
What we do is
select * from table
(select row_number() over(partition by client order by day_of_sale) as
rownum, client, qty from table1) as x
where x.rownum !=1;
This gives me a huge cost since there is two reads here.
I want to do something that would require one read , like select <>
from table1;
Hope the additional data helps. >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Apr 26, 2010 Posts: 22
|
(Msg. 5) Posted: Tue Apr 27, 2010 10:25 am
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On 2010-04-27 13:07, Arun Srini wrote:
>
> Table1
> ---------
> create table table1(client char(5), day_of_sale date, qty bigint);
>
> Table2
> ---------
> Same as table 1
>
> Requirement - table1 is a staging table, gets loaded using an ETL.
> Table 2 is the production table.
> Due to a problem with client data, the first record for everymonth,
> every client,ex.
> ('Belgium','01/01/2010',-10)
>
> This will always have a negative value, and is junk data.
Are these the only ones with a negative value?
/Lennart
[...] >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Sep 15, 2003 Posts: 228
|
(Msg. 6) Posted: Tue Apr 27, 2010 11:56 am
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On 4/27/10 4:07 AM, Arun Srini wrote:
>
> Table1
> ---------
> create table table1(client char(5), day_of_sale date, qty bigint);
>
> Table2
> ---------
> Same as table 1
>
> Requirement - table1 is a staging table, gets loaded using an ETL.
> Table 2 is the production table.
> Due to a problem with client data, the first record for everymonth,
> every client,ex.
> ('Belgium','01/01/2010',-10)
>
> This will always have a negative value, and is junk data.
Can qty ever be negative? If not, then just a simple predicate for
qty >= 0 sounds like it should suffice.
If not, have you created any indexes on the staging table to help
with the query (i.e. to try and avoid a sort)? >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Apr 26, 2010 Posts: 4
|
(Msg. 7) Posted: Tue Apr 27, 2010 9:22 pm
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Apr 27, 6:59 pm, Lennart Jonsson
wrote:
> On 2010-04-27 13:07, Arun Srini wrote:
>
>
>
>
>
>
>
> > Table1
> > ---------
> > create table table1(client char(5), day_of_sale date, qty bigint);
>
> > Table2
> > ---------
> > Same as table 1
>
> > Requirement - table1 is a staging table, gets loaded using an ETL.
> > Table 2 is the production table.
> > Due to a problem with client data, the first record for everymonth,
> > every client,ex.
> > ('Belgium','01/01/2010',-10)
>
> > This will always have a negative value, and is junk data.
>
> Are these the only ones with a negative value?
>
> /Lennart
>
> [...]
sorry the data tended to be misleading. The qty can be negative, this
is because the company would use negative values to give additional
data about the 'returns' of their products from customers. >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
External

Since: Dec 10, 2008 Posts: 2
|
(Msg. 8) Posted: Mon May 03, 2010 11:35 pm
Post subject: Re: use a generated column in a where clause [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Apr 28, 9:22 am, Arun Srini wrote:
> On Apr 27, 6:59 pm, Lennart Jonsson
> wrote:
>
>
>
> > On 2010-04-27 13:07, Arun Srini wrote:
>
> > > Table1
> > > ---------
> > > create table table1(client char(5), day_of_sale date, qty bigint);
>
> > > Table2
> > > ---------
> > > Same as table 1
>
> > > Requirement - table1 is a staging table, gets loaded using an ETL.
> > > Table 2 is the production table.
> > > Due to a problem with client data, the first record for everymonth,
> > > every client,ex.
> > > ('Belgium','01/01/2010',-10)
>
> > > This will always have a negative value, and is junk data.
>
> > Are these the only ones with a negative value?
>
> > /Lennart
>
> > [...]
>
> sorry the data tended to be misleading. The qty can be negative, this
> is because the company would use negative values to give additional
> data about the 'returns' of their products from customers.
Hi Arun,
Can't we tackle this situation when your ETL tool reads the data and
loads the staging table. create one more staging table and have ETL
tool to write the records to this as well( here business logic should
be adapted at ETL level), i assume it won't cause much system
overhead; also its going to be just normal insert and concurrency
should not be a problem.
also have you tried constructing the MQT, which we used in our old
days
hope this helps.
Regards,
Sri >> Stay informed about: use a generated column in a where clause |
|
| Back to top |
|
 |  |
| Related Topics: | loading in to generated row change timestamp column - We're trying to take advantage of the new ROW CHANGE TIMESTAMP option. Here is a simple table: CREATE TABLE "ACCTASGN"."NUMBER_STATUS" ( "STATUS_CODE" CHAR(1) NOT NULL , "STATUS_DESCRIPTION" VARCHAR(40)...
how to reset start value of "generated always as identity" - I have the following table SMV: CREATE TABLE SMV( SET_ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, CACHE 500, MINVALUE 1, MAXVALUE 9223372036854775807, NO CYCLE, NO ORDER), SET_NAME VARCHAR(100) ) ; And need to..
Again against generated columns or (missing) row value con.. - Table definition: CREATE TABLE "SCHEMA1 "."X2" ( "C1" CHAR(20) NOT NULL , "C2" CHAR(10) NOT NULL , "C3" CHAR(30) NOT NULL GENERATED ALWAYS AS (C1|...
How to reset auto generated id in a DB2 table - Hi Is there a way to reset the auto generated id for this table. Drop table and recreating does not work.
DB2 Express UDB 9 with db2jcc JDBC driver- Issues with Aut.. - Hi All, We are testing one of our component with JDBC + DB2 using db2jcc driver. We afced a strange issue. Scenario is like this. 1. We insert 1 record to a table having IDENTITY column as the primary key and retrieve the newly inserted identity value.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|