use of Isnull() in optimization [duplicate] - sql-server

This question already has an answer here:
Does wrapping nullable columns in ISNULL cause table scans?
(1 answer)
Closed 5 years ago.
Is any diff in below query in term of performance??
select COLUMN1,column2 from table
where COLUMN1 is not null
and COLUMN1 <>'';
select COLUMN1,column2 from table
where isnull(Column1,'')<>'';

You can obtain an execution plan for your query to determine the cost of that operation.
How to do it and the output format really depends on each database product.
In the case of SQL server it may look like:
My intuition tells me that operation in particular should not be expensive. What would rather be expensive is to query a column that is not indexed.

Related

nHow can I use GROUP_CONCAT() in MSSQL and sort data based on the entire row? [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
MSSQL - GROUP_CONCAT
(1 answer)
I want to do group_concat in SQL Server
(1 answer)
Closed 2 years ago.
I have a bit of an issue. I have a dataset with around 6000 records stored in a MSSQL 2016 database, where I need to concatenate each column, sorted on row basis and grouped by an ID.
It is no problem to sort the data within the column itself, but the position of the concatenated values is the key in the system that needs the data. So, if I merge four rows with the same ID, each row value must be in the same position throughout the whole resultset row.
To make it even more messy, I must be able to apply some logic to determine which row should be in position one.
If I use the the sort functionality in GROUP_CONCAT_DS (I'm using a semicolon delimiter), it sorts on the column values, which make the index position of each merge inconsistent related to the rows that holds the data.
Example data:
id col1 col2
--------------------
1 foo bar
1 lorem ipsum
2 hi cowboy
Expected output:
id, concat_col1, concat_col2
----------------------------
1 foo;lorem bar;ipsum
2 hi cowboy
Absolutely unacceptable output:
id concat_col1 concat_col2
----------------------------
1 bar;foo lorem;ipsum
2 hi cowboy
In pseudo-SQL, this is a simplified example of what I want to achieve:
SELECT
[id]
,dbo.GROUP_CONCAT_D([col1], ';') AS [concat_col1]
,dbo.GROUP_CONCAT_D([col2], ';') AS [concat_col2]
FROM [hrm].[vEmploymentsPositions]
GROUP BY [id]
ORDER BY [id] asc, [col1] desc, [col2] asc
... But for loical reason the sort doesn't work that way.
How can I approach this issue and make a robust solution that fit our needs?

SQL server - Query optimization [duplicate]

This question already has answers here:
IN vs OR in the SQL WHERE clause
(8 answers)
Closed 5 years ago.
I would like to know from below queries, which one would give better performance and how?
select * from TableA where (Name = 'ABC' or Name = 'DEF' or Name = 'GHI')
or
select * from TableA where Name in ('ABC','DEF','GHI')
Internally both IN and OR operators perform the same action. More importantly you need to see if you have proper index on the Name column.You can have a non-clustered index on the Name column and have one clustered index on this table.

Mixing indexed and calculated fields in a table-valued function

I work with SQL Server 2008, but can use a later version if it would matter.
I have 2 tables with pretty similar data about some people but in different formats (no intersections between these 2 sets of people).
Table 1:
int personID
bit IsOldPerson //this field is indexed
Table 2:
int PersonID
int Age
I want to have a combined view that has the same structure as the Table 1. So I write the following script (a simplified version):
CREATE FUNCTION CombinedView(#date date)
RETURNS TABLE
AS
RETURN
select personID as PID, IsOldPerson as IOP
from Table1
union all
select personID as PID, dbo.CheckIfOld(Age,#date) as IOP
from Table2
GO
The function "CheckIfOld" returns yes/no depending on the input age at the date #date.
So I have 2 questions here:
A. if I try select * from CombinedView(TODAY) where IOP=true, whether the SQL Server will do the following separately: 1) for the Table 1 use the index for the field IsOldPerson and do a "clever" index-based selection of results; 2) for the Table 2 calculate CheckIfOld for all the rows and during the calculation pick up or rejecting rows on the row-by-row basis ?
B. how can I check the execution plan in this particular case to understand whether my guess in the question (A) is correct or not?
Any help is greatly appreciated! Thanks!
Yes, if the query isn't too complex, the query optimizer should "see through" the view into its constituent UNION-ed SELECT statements, evaluate them separately, and concatenate the results. If there is an index on Table1, it should be able to use it. I tested this using tables we had and the same function concepts you presented. I reviewed the query plans of the raw SELECT to Table1 and the SELECT to the inline table-valued function with the UNION and the portion of the query plan relevant to Table1 was the same-- and it used the index.
Now if performance is a concern, I suggest you do one of two things:
If (a) Table2 is read-heavy rather than write-heavy, (b) you have the space, and (c) you can write CheckIfOld as a single CASE statement (as its name and context in your question implies), then you should consider creating a persisted calculated field in Table2 with the calculation from IsOldPerson and applying an index to it.
If Table2 is write-heavy, or you have no space for additional fields, you should at least consider converting CheckIfOld into an inline function. You will likely reap performance gains, depending on how it is used. In your case, it would be used like this:
select personID as PID, IOP.IsOldPerson from Table2 CROSS APPLY dbo.CheckIfOld(Age,#date) AS IOP

For copying data from one to another table which is efficient,using INTO or using INSERT? [duplicate]

This question already has answers here:
INSERT INTO vs SELECT INTO
(11 answers)
Closed 8 years ago.
There are two way to copy data from one table to another.
1. First create a new table then using "
INSERT INTO TABLE2_NAME
SELECT * FROM TABEL1_NAME"
OR
2. SELECT * INTO TABLE2_NAME FROM TABLE1_NAME
I am using SQL Server 2012.
They are different inasmuch that the first one needs to have the table already created, the second one will create the table too.
I would pretty much bet that the execute plan for the read on TABLE1_NAME will be the same ergo the second is probably a few millisecs slower as it has to create the table.
Now the next thing to take into account is where the table is being created. In the first on you may have placed the table on fast storage while in the second the it will go to to the default primary storage which may or may not be optimised.
If all things are equal - then it's likely a micro optimisation that isn't worth considering.
The INSERT INTO TABLE2_NAME SELECT * FROM TABEL1_NAME creates TABLE2_NAME and inserts the values of TABEL1_NAME in them. It is more efficient but if the table is already created that statement would give an error.
The SELECT * INTO TABLE2_NAME FROM TABLE1_NAME only inserts the values of TABLE1_NAME in TABEL2_NAME.

Concatenate on JobID [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate a selected column in a single query?
I know this is fairly simple in MySQL, but i don't know how to do it in SQLServer. I have a table containing two columns. UserID & TagID. I'd like to concatenate TagID into a comma separated string, where the result is grouped by UserID. How can i do that?
I would ythink something lik ethis might be what you are looking for :
SELECT ''+Table1.UserID+','+Table2.TagID+'' AS CombinedCSVColumn
, SUM(Table1.Value) AS Value
From dbo.Table1 as Table1
INNER JOIN db.Table2 as Table2
ON Table1.EmployeeID = Table2.EmployeeID
GROUP BY ''+Table1.UserID+','+Table2.TagID+''
That is if they come from two Tables. For one table is similar.
Regards
Mac

Resources