Querying speeds - sql

In a large database, is it faster to perform a query such as
select * from table where a = 1 and b = 2 or b = 3 or b = 4;
or
select * from table where a = 1 and b = 2;
select * from table where a = 1 and b = 3;
select * from table where a = 1 and b = 4;

You should rely on the DBMS to do the optimisation for you. If the second were quicker, then the DBMS would do it that way anyway.
Go with the first (but put parentheses round the b conditions, as Tommy suggests).

As others have pointed out, the queries are not equivalent. I assume the first query should read:
select * from table where a = 1 and (b = 2 or b = 3 or b = 4);
For clarity I would suggest:
select * from table where a = 1 and b in (2, 3, 4);
In general this will perform better than asking three different queries as in the second alternative.

Related

Why is Selecting From Table Variable Far Slower than List of Integers

I have a pretty big MSSQL stored procedure that I need to conditionally check for certain IDs:
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where b.SomeID in (1,2,3,4,5)
I wanted to conditionally check the SomeID field, so I did the following:
if #enteredText = 'This'
INSERT INTO #AwesomeIDs
VALUES(1),(2),(3)
if #enteredText = 'That'
INSERT INTO #AwesomeIDs
VALUES(4),(5)
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where b.SomeID in (Select ID from #AwesomeIDs)
Nothing else has changed, yet I can't even get the latter query to grab 5 records. The top query returns 5000 records in less than 3 seconds. Why is selecting from a table variable so much drastically slower?
Two other possible options you can consider
Option 1
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where
( b.SomeID IN (1,2,3) AND #enteredText = 'This')
OR
( b.SomeID IN (4,5) AND #enteredText = 'That')
Option 2
Select SomeColumns
From BigTable b
Join LotsOfTables l on b.LongStringField = l.LongStringField
Where EXISTS (Select 1
from #AwesomeIDs
WHERE b.SomeID = ID)
Mind you for Table variables , SQL Server always assumes there is only ONE row in the table (except sql 2014 , assumption is 100 rows) and it can affect the estimated and actual plans. But 1 row against 3 not really a deal breaker.

SQL query two queries and union

I've these two approaches for a problem and they look kind of okay to me. Is there a better way to do this? Please advise.
X,Y,Z are same for both queries
Two queries and add the values:
select sum(columnA1), sum(columnA2) from tableA
where columnA3 = X and columnA4 = Y and columnA5 = Z;
select sum(columnB1) from tableB
where columnB3 = X and columnB4 = Y and columnB5 = Z;
Union All:
select sum(columnA1), sum(columnA2), sum(columbB1)
from (select columnA1, columnA2, null from tableA
where columnA3 = X and columnA4 = Y and columnA5 = Z
union all
select null, null, columnB1 from tableB
where columnB3 = X and columnB4 = Y and columnB5 = Z) unionresult
Each of your 2 options will produce the same 3 numeric results, the only real difference is in the delivery (either as 2 queries or as a single, combined, query).
I cannot really know which suits your purpose best, but you are adding a tiny overhead to get get a combined result.
Here is another possible alternative for a combined result but I don't think it's any better than the union all approach you show (& it could even be worse).
SELECT
SUM(columnA1)
, SUM(columnA2)
, (
SELECT
SUM(columnB1)
FROM tableB
WHERE columnB3 = X
AND columnB4 = Y
AND columnB5 = Z
) as sum_columnB1
FROM tableA
WHERE columnA3 = X
AND columnA4 = Y
AND columnA5 = Z
;

sql query with comparison but without removing without subquerying

my question is, is it possible to select certain rows in a table according to a comparison rule without removing anything from the result. To clarify what i want to to imagine following example.
i have a table with two values,
A | B | C
1 0 hey
1 1 there
2 1 this
3 0 is
3 1 a
4 0 test
now i want to select the rows that have a 0 in the B column, and an a in the C column without removing the results that don't have a 0 in column B but the same value in column A.
For that i could do a
select C from T where A in (select A from T where B = 0);
but isn't it possible to select all C values where column B contains a 0 and that match column A with those?
I'd gladly stand by if more information is needed since it is a quite fuzzy question, but SQL can be confusing sometimes.
Tough to tell without your example result set; but maybe something like this:
SELECT A, B, C
FROM myTable
WHERE (B = 0 AND C LIKE '%A%')
OR (B <> 0 AND B = A)
I think you just want an or condition:
select C
from MyTable
where b = 0 or A in (select A from T where B = 0)
Is this the version you want:
select C
from MyTable
where C = 'a' or A in (select A from T where B = 0)

Order of operations for SQL?

Suppose I have this statement:
SELECT * FROM MyTable WHERE a = 1 or b = 2 and c = 3
Does that mean: (a = 1) OR (b = 2 AND c = 3) or does it mean (a = 1 or b = 2) AND c = 3? Can I change what it means, i.e. execute the OR before the AND or is this not possible?
From Technet:
When more than one logical operator is used in a statement, AND
operators are evaluated first. You can change the order of evaluation
by using parentheses.
So yes, it means (a = 1) OR (b = 2 AND c = 3).
You can force the behavior you want by writing the parentheses as you did above: (a = 1 OR b = 2) AND c = 3

Searching Multiple Rows at a time through a single SQL query

I have a table whose data is in this manner.
A B C
---------
0 6 2
0 3 4
1 0 2
1 1 4
I wrote a SQL query -
select A
from Table
where (B = 6 and C = 2) AND (B = 3 and C = 4).
Obviously it returned zero results since this query would search in the same row. Please help me with writing a better one to produce results such that it can check two rows with a single statement.
EDIT:
I am not looking for 'OR' statement. I need to find an element of A such that it has two corresponding rows AND each of the rows has elements 6,2 and 3,4 present in columns B,C correspondingly.
PS.
(I don't have the option of writing two queries and then finding the common elements of two set.)
Many thanks in advance
I guess you want something like this
select A
from YourTable
where (B = 6 and C = 2) or
(B = 3 and C = 4)
group by A
having count(distinct B) >= 2
Try here:
https://data.stackexchange.com/stackoverflow/q/123711/
Use OR instead of AND
select A from Table where (B=6 and C=2) OR (B=3 and C=4).
If you want the onlu result use DISTINCT
select DISTINCT A from Table where (B=6 and C=2) OR (B=3 and C=4).
If you need to check the equality of A, use this:
select t1.A
from Table t1
JOIN Table t2 ON t1.A = t2.A
where T1.B=6 and t1.C=2 AND t2.B=3 and t2.C=4
As you see - using AND again
Are you trying to get this??
SELECT A
FROM Table
WHERE (B = 6 AND C = 2) OR (B = 3 AND C = 4)
This would return the A column for all four rows again.
If not: WHAT exactly are you trying to select?
IF you want just two rows, one with A = 0, one with A = 1, then use DISTINCT:
SELECT DISTINCT A
FROM Table
WHERE (B = 6 AND C = 2) OR (B = 3 AND C = 4)
Maybe:
select A
from Table
where (B = 6 and C = 2)
INTERSECT
select A
from Table
(B = 3 and C = 4)