| Author |
Topic |
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-14 : 04:01:54
|
| Hi All,Is there anything like CREATE TABLEtemptableAS SELECT column1,column2... FROM table1 ...I know there SELECT column1,... INTO temptable FROM table1....How would I go about this when I have SELECT columns1,....FROM table1...UNIONSELECT columns1,...FROM tableA...UNOINSELECT columns1...FROM tableN..UNOIN .........Into a TempTable.Necessity is the mother of all inventions! |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-14 : 04:03:43
|
| [code]Create Table #temp(... -- column definitions here)INSERT INTO #tempSELECT columns1,....FROM table1...UNIONSELECT columns1,...FROM tableA...[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-14 : 04:04:15
|
[code]SELECT column1,... INTO temptable FROM ( SELECT columns1,....FROM table1... UNION SELECT columns1,...FROM tableA...) d[/code] KH |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-14 : 04:09:15
|
| Create Table #temp(... -- column definitions here)INSERT INTO #tempSELECT columns1,....FROM table1...UNIONSELECT columns1,...FROM tableA...-------------This is wat I am doing now. So seems like a lot of code.. coz there many such tables to be created. On the other hand SELECT colum1...INTO temptableFROM( SELECT columns1.. FROM table1.. UNOIN SELECT columns1..FROM tableA..)dWill mean I need to have all the columns in the SELECT columns1.... INTO TemptableIs that correct?Necessity is the mother of all inventions! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-14 : 04:18:43
|
quote: Will mean I need to have all the columns in the SELECT columns1.... INTO Temptable
Yes. KH |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-14 : 04:21:29
|
| Even though it has lot of code involved, it prevents locks on system tables when creating temp table which is the case with SELECT..INTO approach.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-14 : 04:28:41
|
| Even though it has lot of code involved, it prevents locks on system tables when creating temp table which is the case with SELECT..INTO approach.I wasnt aware of it locking system tables. harsh can you give me a example. Better still if you show .. wat are effects due to this. Thank youNecessity is the mother of all inventions! |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-14 : 04:51:12
|
| And to add to the same .. INSERT INTO temptable costs 50% more then SELECT INTO on the test which I did with a queryThe results are like 9:34 mins for SELECT INTO and 14:46 mins for INSERT INTO without the CREATE TABLE .Any ideas, as what to use since I am trying to reduce the time taken for the Execution. And I am still concerned abt the locks SELECT INTO will impose.Thank youNecessity is the mother of all inventions! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-14 : 05:02:56
|
| What is the UNION for?If there are no duplicates, add ALL to UNION and see if it runs faster.Peter LarssonHelsingborg, Sweden |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-14 : 05:15:08
|
| Peso.. there are duplicates..so I will have to stick with UNIONNecessity is the mother of all inventions! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-14 : 05:24:18
|
| I think UNION filters out the dupicates for each and every time it's used.Try this and see if there is a speed difference.INSERT TempTableSELECT DISTINCT column1, column2...FROM(SELECT column1, column2... FROM table1UNION ALLSELECT column1, column2... FROM tableA) as dPeter LarssonHelsingborg, Sweden |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-14 : 07:13:20
|
| hey, peso tried that.. it is running for more than 35 minsNecessity is the mother of all inventions! |
 |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-05-15 : 19:34:05
|
| Find and delete the dupes after the table is populated... it's quicker.--Jeff Moden |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-16 : 04:08:05
|
| Jeff .. that is a good idea.. wat do u think of SELECT col1,col2,col3.........COUNT(1) INTO temptable2 FROM temptable1GROUP BY col1,col2,col3.....DROP TABLE temptable1and use temptable2Necessity is the mother of all inventions! |
 |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2007-05-17 : 01:27:33
|
| That's one way to do it... but if it's taking 9 or 10 minutes just to do the original insert, you're gonna have a lot more time involved doing it that way. How many rows of data in the table are we talking about? And, can we make one of the columns in the table an IDENTITY column? If so, I have a method for deleting 18,000 dupes from a 4 million row table in about a minute, depending on your disk setup and ram, of course.--Jeff Moden |
 |
|
|
Vijaykumar_Patil
Posting Yak Master
121 Posts |
Posted - 2007-05-18 : 04:50:29
|
Jeff , now I am doing SELECT col1...INTO temptable FROM table1....UNIONSELECT col1... FROM table2...UNIONSELECT col1.. FROM table3.... This is seems to be faster too.. And the other thing about deleting .. from a table which is index it will be easiler but not a temptable. That is slower than this.Necessity is the mother of all inventions! |
 |
|
|
|