I have 1 temporary table and I am doing something like:
Insert into #table1 select ... from #temporal
Insert into #table2 select ... from #temporal
Insert into #table3 select ... from #temporal
As every select take a considerable time I would like to paralelize these 3 queries is there any way to do this in SQL Server 2008?
It sounds like the root of the problem is that the SELECT from #temporal is a performance problem. Do you have an index on that temp table #temporal? Likely an index (or statistics update) would help you out to avoid writing more code to work around this problem.
Are you able, or have you tried measuring performance against storing the results from that one #temporal SELECT into a table variable?
DECLARE #myTemporal TABLE (id int, foo varchar(100))
INSERT INTO #myTemporal (id, foo)
SELECT id, foo FROM #temporal;
Then your n INSERTs can pull from the table variable, rather than the expensive/nonperformant query.
Insert into #table1 select id, foo from #myTemporal;
Insert into #table2 select id, foo from #myTemporal;
Insert into #table3 select id, foo from #myTemporal;
The benefit is that you won't have to execute the SELECT 3x against your temp table. You'd be inserting into your 3 temp tables from the table variable. All rows, no WHERE clause.
Related
I need to Need to fetch all the records of table "#Tbl1" which "DisplayId" not present in table "#Tbl2".
"#Tbl1" having Max 100 records
"#Tbl2" is growing table
Create Table #Tbl1(Id1 Int Identity(1,1), DisplayId Nvarchar(200), Name Nvarchar(200))
Insert Into #Tbl1(DisplayId, Name) Values ('d1', 'ABC'),('d2', 'PQR')
Create Table #Tbl2(Id2 Int Identity(1,1), DisplayId Nvarchar(200))
Insert Into #Tbl2(DisplayId) Values ('d1')
Below query is working, but looking for efficient query and please suggest what kind of Index is required to which table's column?
I am using SQL Server 2008 R2
Select * From #Tbl1
Where DisplayId Not In (Select DisplayId From #Tbl2)
Using the in statement does seem to be slow generally, I would use an exists statement if possible but I'm not sure if it is supported on SQL Server 2008. Other option you have is a left join which will also be slow.
An exists or (not exists) statement works like this
select * from #tbl1 t1
where not exists (select displayid from #tbl2 where displayid = t1.displayid)
Notice the use of the alias t1, you do not have to select a column in the sub select, you can use a wildcard *. I've found this type of query is far more efficient because it uses the index links directly between the 2 tables instead of first selecting all the columns and then filtering them.
If i insert records in table T1, using output I want to insert these records from logical table 'inserted' into table T2. Below is my code. If I use the below code records get inserted into T2 but table T1 shows blank...can someone tell me where I am going wrong..
create table T1(id int identity(1,1), name vachar(100))
create table T2(id int, name varchar(100))
declare #t table(id int,name varchar(100))
insert into t1(name)
output inserted.id,inserted.name into #t
values('deepak')
insert into t2
select * from #t
this is strange ..If I do
select * from t1
select * from t2
I get value 'deepak' inserted in table T2 and table T1 shows blank.How to insert a record in both the tables T1 and T2 at the same time without using triggers.
A good way to get traction on your problem is to utilize a tool like SQLFiddle. Makes it easy to share and get others looking at your problem quickly :)
SQLFiddle Output Test
That seems to work for me, although I did see the issue you had with the same exact code you had above. Not sure if it's something related to temp tables, but there's definitely something weird happening.
I would like to write a query which:
as short as possible
grammatically correct to MySQL server
makes server returning an error when executed
My current solution is:
CREATE TABLE FOO (i INT);
INSERT INTO FOO VALUES (1);
INSERT INTO FOO VALUES (2);
SELECT 1 FROM FOO WHERE 1 = (SELECT i FROM FOO);
However I don't like it - it is too long.
CREATE TABLE FOO (i INT);
CREATE TABLE FOO (i INT);
It will not accept to create the table because it already exists after the first statement.
Here are some short queries that are all "correct" but will fail
SELECT *
SELECT 1e333
SELECT i FROM(SELECT 1i,1i)
The verbose versions are
SELECT * FROM DUAL
SELECT 1e333 FROM DUAL
SELECT i FROM (SELECT 1 as i, 1 as i FROM DUAL)
They fail for different reasons:
1) DUAL does not have any columns.
2) 1e333 does not fit into a float.
3) The inner query returns a table with two identical named columns. Thus specifying this column name is not admissible for the outer query.
I need to insert metadata values in a temp table. what is the easites thing to do it?
I have values like 3390,3391,8978,9899,7677,9656,5463 about 30-40 of them. I want to insert them into a temp table. Do not want to query a table since that is a big table and using an IN operator is very low in performance.
IS this the best way?
INSERT INTO #Table
Select '3390'
UNION ALL
select '3391'
UNION ALL
select '8978'
Any other suggetions?
It could be bit easier this way;
Insert into #temp (field)
select number
from (values (123),(456),(678),...,(432)) as t(number)
Or search for a split function and do it like;
insert into #temp (field)
select item from dbo.split('123,456,789',',')
To create and insert at the same time, you could use 'Select Into' syntax.
I want to run a T-SQL script where I create a temp table that will be aggregated by a certain field of another table, call it table X. The remaining fields of this temp table will be populated by performing aggregate functions on the fields of table X. Then I would like to do a MERGE / WHEN MATCHED with my temp table on a different table (call it table Y) after I have populated the temp table.
How do I create this temp table and populate it with aggregate functions? (I have already coded the MERGE part of the problem).
to create a temp table you will can do the following:
create table #temp
(
id int,
col1 int
)
then you will write an INSERT INTO
INSERT INTO #temp
SELECT col1, sum(col2)
FROM yourTable
Once you have created your temp table you can use it in your store procedure.
What bluefeet posted, or:
with rsAggregated as
(
select id, sum(x)
from tableX
group by id
)
merge...
Or, not seeing your merge statement, just
merge tableY using
(
select id, sum(x)
from tableX
group by id
) rsAggregated
on rsAggregated.id = tableY.id
when matched
...
when not matched
...
You can do the whole thing with one SELECT statement
SELECT col1 AS ID, sum(col2) AS col1 INTO #temp FROM yourTable