Insert/Update
Hi guys,
I've been having a bit of trouble here. My table consists of three columns - key1, key2 and count. What I would like to do is check if an entry exists for the combination of key1=some_value and key2=some_other_value. If it does, then I would like to increment the counter. If it doesn't, then I would like to insert a row for that pair of keys.
The closest I've come to doing this is by basically using this approach in what seems like a weird way. http://www.vertica.com/2012/08/27/load-new-data-and-modify-data-simultaneously/
This is what I've been doing:
- Insert a row into a temporary table
- Merge that temp table into the real table
- Commit the changes
- Truncate the temp table
- Repeat with the next entry
This was working but didn't seem very efficient as I was doing everything one entry at a time. I tried inserting a bunch of entries into the temp table, then merging them all at once, but instead of the counters going up as expected, I would instead get duplicate rows. I was hoping someone here could help me out.
Josh
I've been having a bit of trouble here. My table consists of three columns - key1, key2 and count. What I would like to do is check if an entry exists for the combination of key1=some_value and key2=some_other_value. If it does, then I would like to increment the counter. If it doesn't, then I would like to insert a row for that pair of keys.
The closest I've come to doing this is by basically using this approach in what seems like a weird way. http://www.vertica.com/2012/08/27/load-new-data-and-modify-data-simultaneously/
This is what I've been doing:
- Insert a row into a temporary table
- Merge that temp table into the real table
- Commit the changes
- Truncate the temp table
- Repeat with the next entry
This was working but didn't seem very efficient as I was doing everything one entry at a time. I tried inserting a bunch of entries into the temp table, then merging them all at once, but instead of the counters going up as expected, I would instead get duplicate rows. I was hoping someone here could help me out.
Josh
0
Comments
* create table TBL_UPDATED like TBL including projections
* populate TBL_UPDATED with insert/select...(like view from above) >> swap tables >> drop "old"
In any case COPY data as is without UPDATED/DELETES/MERGES will be faster.
PS
If projection is "suitable" for query/view, sometimes to create an updates data VIEW much more faster than to update it. If key1 and key2 have low cardinality and projection is optimized for their counting then probably VIEW will be a fastest way.
This worked perfectly, thanks. When I created my view, I used "from TBL_UPDATE" rather than "from TBL", because it didn't seem that the projection was being used with the way you had written it. I imagine that was just a typo, but maybe not?
Anyways, thanks again.
Optimizer choose a plan by COST and not by time execution. May be its a reason for choosing other projection.
Also previously I provided a very simple example, here its little more complicated - explicit grouping of columns, encoding definition. Other columns also require encoding. Play with it.