SQL Server nesting subquery performance? - sql-server

I am trying to evaluate and see what the best practice is to accomplish the following. I need to do 2 queries from the same tables only with different criteria, and then join them together with a unique ID. At the end, what I want to accomplish is getting all the records that exists in Query A but not Query B.
SELECT *
FROM (
Select
t1.ID from t1
Left Join t2
Left join t3
where
t1.field1 IN ('criteria 1', 'criteria 2')) QueryA
RIGHT JOIN
(
Select
t1.ID from t1
Left Join t2
Left join t3
where
t1.field1 IN ('criteria 3', 'criteria 4')) QueryB
ON QueryA.ID = QueryB.ID
WHERE QueryA is Null
Each QueryA and QueryB return about 2500 records, and took each less than 10 seconds . The result of the whole query returns about 30 records and over 3 minutes.
Thank you so much.

Try using SQL's "except." Do you need the joins to t2 and t3? It looks like you're only filtering on and returning t1 columns.
Select ID from t1 where t1.Field in (criteria)
except
Select ID from t1 where t1.Field in (other criteria)

Related

Creating a new column at same time as LEFT JOIN within SELECT statement

I am trying to create a table within SQL Server that LEFT JOINS a number of tables together with the required information. Lets call them t1, t2, t3.
With one of the tables, I would like to create a new column in t3 (t3.text2) based off of another column within t3. I've managed to do this really inefficiently by running one query to create a new t3 #temptable by selecting required info and then selecting from that but I presume there is much better way to use a SELECT subquery with CASE WHEN to produce a new column alongside the LEFT JOIN in the overall query, sadly I can't get it working and would welcome any suggestions...
SELECT
t1.location, t2.name, t3.text1, t3.text2
FROM
t1
LEFT JOIN
t2 ON t1.a = t2.a
LEFT JOIN
t3 ON t2.b = t3.b
SELECT t1.location, t2.name, t3.text1,
CASE
WHEN t3.some_column = some_value THEN new_value
ELSE other_new_value
END as text2
FROM t1
LEFT JOIN t2 ON t1.a = t2.a
LEFT JOIN t3 ON t2.b = t3.b

MSSQL - Adding condition on the where and on clause performance

Consider the following two queries:
select *
from
table1 t1
left join
table2 t2
on t1.Id = t2.t1Id and (t1.Status = 1 or t2.Id is not null)
And this one
select *
from
table1 t1
left join
table2 t2
on t1.Id = t2.t1Id
where
t1.Status = 1 or t2.Id is not null
The first one runs in 2 seconds. The second one in 2 minutes. Shouldn't the execution plan be the same?
The query plans are different because the queries (and results) are different.
You're using a LEFT JOIN, so the first query will return rows with NULL values where not in table 2.
The second query will not return those rows.
If it was an INNER JOIN, they would essentially be the same query.
Here the Below Query Returns all the "Table1" results with additional matching Columns based on the "ON Clause" condition.
select * from table1 t1
left join table2 t2
on t1.Id = t2.t1Id and (t1.Status = 1 or t2.Id is not null)
Now, the below query matches the 2 tables and returns the rows based on the ON Clause and an additional WHERE Clause filters the Rows again based on the Condition.
select * from
table1 t1
left join table2 t2 on t1.Id = t2.t1Id
where t1.Status = 1 or t2.Id is not null
Here, Even though we used LEFT JOIN But in this case it acts like an INNER JOIN
So, Here Both the Queries produce Different Result Sets. The Execution Plan Also Vary which results in Different Execution Time.
The best way to deal with an OR is to eliminate it (if possible) or break it into smaller queries. Breaking a short and simple query into a longer, more drawn-out query may not seem elegant, but when dealing with OR problems, it is often the best choice:
select *
from table1 t1
left join table2 t2 t1.Id = t2.t1Id
where t1.Status = 1
union all
select *
from table1 t1
left join table2 t2 t1.Id = t2.t1Id
where t2.Id is not null
You can read more in this article:
https://www.sqlshack.com/query-optimization-techniques-in-sql-server-tips-and-tricks/

insert records except in sql server

I have two tables t1 and t2.
t1 having 10k records and t2 having 2k records. The 2k records of t2 is present in t1.
I wanted the 8k different records from t1 which is not present in t2.
I'm doing this as below:
select id, second_telphon from t1
except
select id, second_telphon from t2
However, I'm still getting all the 10k records. Is "except" keyword not working?
how can I achieve this?
you can perform a Join to get the unique data from the tables .
like the tables t1 & t2 both you cna perform left or right join .
example:
SELECT T1.*
FROM T1
WHERE NOT EXISTS(SELECT NULL
FROM T2
WHERE T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour)
OR .
SELECT T1.*
FROM T1
LEFT JOIN T2
ON T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour
WHERE T2.ID IS NULL
Try this:
SELECT *
FROM T1
WHERE NOT EXISTS(SELECT id,second_telphon FROM t2)
If ID is a unique value, Try this also:
SELECT *
FROM T1
WHERE ID NOT IN(SELECT ID FROM t2)
You could try a union, followed by an aggregation to restrict to those records in the first table which were not duplicated by the second table:
SELECT id, second_telphon
FROM
(
SELECT id, second_telphon FROM t1
UNION ALL
SELECT id, second_telphon FROM t2
) t
GROUP BY id, second_telphon
HAVING COUNT(*) = 1;
If a record, being defined as an id, second_telphon pair, has a record count of only one after the union, it implies that this record was unique to the first table.
Just do left join
select t1.id,t1.second_telphon from t1
left join t2 on
t1.id = t2.id
and t1.second_telphon =t2.second_telphon
where t2.id is null

SQL - Using a join on a column with multiple values in cells

I have two tables I am trying to join. One table has a column with IDs in it, and I am trying to do a left join to a different table that has the same IDs in it, although the second table could contain more than one ID per cell. For example, if my first table has an ID value of 123, and the second table has an ID value of 123;724;823, is there any way to get it to join the two rows?
You tried in query designer? Is very easy to make joins there.
SELECT column_names
FROM table-name1 LEFT JOIN table-name2
ON ID_column-name1 = ID_Column-name2
WHERE condition X,Y,Z
Hope will help you.
select *
from
(
select '123' as id
union select '124'
) as t1
left join
(
select '123;001;002' as id
union select '001;123;002'
union select '001;002;123'
) as t2 on
t2.id = t1.id
or t2.id like t1.id + ';%'
or t2.id like '%;' + t1.id + ';%'
or t2.id like '%;' + t1.id
Using the multiple like operators is probably the fastest way, but if you have a string splitter function like this one DelimitedSplit8K, you can split the values out into a table and join to it.
SELECT *
FROM table1 t1
LEFT JOIN (
SELECT *
FROM table2 t2
OUTER APPLY (
SELECT *
FROM dbo.[DelimitedSplit8K] (t2.id,';') -- splits the values in multi id column
) t
) t ON t.Item = t1.id -- t.Item is the value generated from the DelimitedSplit8K TVF

How to do search on a table while table have millions of record

I have to run this below query in sybase database and this will return millions of record.
but when i run it took lot of time and many of times timeout.
select * from table1 t1
where t1.oneid IN
(
select t2.oneid
from table2 t2
where t2.twoid IN
(
select DISTINCT t3.twoid from table3 t3
)
)
at isolation 0
so can any one suggest me a way so i can do some bulk operation for getting data?
Try this :
SELECT
t1.*
FROM (
SELECT
DISTINCT t3.twoid
FROM table3 t3
) as tmp
INNER JOIN table2 t2
ON t2.twoid = tmp.twoid
INNER JOIN table1 t1
ON t1.oneid = t2.oneid
It should be faster especially if you have indexes on t2.twoid and t1.oneid

Resources