Declaration of variables improve or decrease performance [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
So I am trying to improve my SQL, one topic that arose, does declaration help with performance or not so like for instance lets say you want to do a if else statement but you need to know if the count is higher than 0 for example right
SELECT #COUNTER = COUNT(ID) FROM tblSome WHERE NUMBER > TOTAL
IF(#COUNTER > 0)
OR would it be better than something like this
IF((SELECT #COUNTER = COUNT(ID) FROM tblSome WHERE NUMBER > TOTAL)>0)
I am just trying to minimize the time it takes, but also it would be nice to know
For now I cannot really find a difference with the small amounts of data I am using and I am not sure how to test it further or go test it to the next level

Use of variables can help or hinder performance dependent on the exact circumstances. There isn't a single rule.
In this case the use of the separate variable assignment step can be harmful.
It gives the optimiser no choice but to count all the rows as it doesn't look ahead to how you use that variable in future statements.
using an IF (SELECT COUNT(*) ...) > 0 allows the optimiser to see that it is true as long as the count is >=1 and can sometimes be optimised to an IF EXISTS (semi join) and stop reading after a single row.
But you are better off just writing it as EXISTS anyway rather than relying on that.
(The discussion on this internet archived blog post has more about the circumstances where this optimisation might happen)

Related

Why does SQL want you to select the column and THEN refer to the table? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
So this isn't a technical question, but rather questioning why a language is designed the way it is.
I've been learning SQL and one thing that's been bothering me greatly is how SQL asks you to name the column you want and THEN name the table you want to get it from. To me, it would make more sense that you refer to the parent body (which is the table) and THEN the column it has. But SQL seems to forces users to do it the other way around. Why?
I'm just curious as to why the language is designed this way.
SELECT column
FROM table
why not
FROM table
SELECT column
SQL tries to mimic English language to some extent, so that it feels natural to formulate the query.
In spoken English you would say something like "I want the names of the employees". You would not say "I want of the employees their names" or something like that.
But you are right, it might have been a good idea to have the query represent the order of execution. And "From the employee table I want the names" would not be so far off the mark :-)
SQL is a descriptive language, not a procedural language. It describes the result set being produced. And, you can think of that result set as a report, with column headers.
As such, the basic querying construct returns those column headers. The rest of the query describes how they are produced.
You may find this post useful. Starting with FROM is the most logical way to think about a query (Why would anyone write SELECT before knowing what to SELECT from?). However, SQL guidelines were designed as if your query were a command. Thus, you are commanding the system to SELECT the data for you, and the FROM further specifies that command.
Of course, the actual execution is distinct from the lexical and logical orders above.

stop execution of a recursive loop on some validation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am having a recursive loop of a stored procedure and i want to stop the execution of that recursive loop on a validation and return an empty table.
How can i do that?
This is some sample code :
alter procedure Searching
(parameters)
begin
do something
if(validation)
--exit here
else
exec Searching(parameters);
end
and i have another question of how to declare a global variable in sql? which should not lose it's scope from one stored procedure to another.
T-SQL is a really poor choice for recursion. It won't allow more than 32 levels of nesting, see ##NESTLEVEL:
When the maximum of 32 is exceeded, the transaction is terminated.
As always, the answer is to think in sets, use a SELECT instead of a stored procedure. Read Recursive Queries Using Common Table Expressions.
As to why your example doesn't work: you obviously made a mistake in the code which you did not post.
you must break down the condition of the recursion so that it don't continue after you return. you can make use off an integer that is decremented after each loop when it is negative u can stop execution.

Complex SQL where clause: many 'and' and 'or' conditions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a generated query to MS SQL Server with complex where clause like this:
(t1.Column1 = SomeValue1 and t1.Column2 = SomeValue2)
OR (t1.Column1 = SomeValue3 and t1.Column2 = SomeValue4)
OR (t1.Column1 = SomeValue5 and t1.Column2 = SomeValue6)
...
(50 or more conditions like this)
Actually I have hierarchy list with 3 level depth. And I want to retrieve all 3rd level items with one query. So t1.Column1 here is the 1st parent, t1.Column2 is the 2nd parent. I have as many conditions in my WHERE clause, as many items are expanded on the 2nd level in list. There can be 1000 or more in the worst case.
I wonder if this can affect query performance. If so, is there any way to optimize this(table-value parameters, etc.)?
I've done some investigations with query plans and query compile time. Both look ok.
I'm going to go out on a limb here and answer; this answer will not be correct, because there isn't really a 'correct' answer given your level of detail. However, here's some possible tips (assuming that you NEED 1000 clauses in your query):
Try to keep the query consistent once created and executed. It will probably take some time to compile, but once compiled, the cache should do its job, unless you keep changing it (even if you just change the values around). Use bind variables if the values need to change.
Indexes - make sure both columns are indexed. Providing they both are and you always query both then you should be fine.
As a caveat to this - I'm not saying this is a good way to query a database, just that the two points above should help.

Replaces Nulls with 0 without using a function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to replace null values in a table but without using a function such as isnull because its dealing with a large amount of data and slowing it down.
everywhere online says isnull and coalesce but is there any way without using such functions.
I need this because the query
OPENING_OTHER + OPENING_FEE + OPENING_INT AS TOTAL_BALANCE
If one value is NULL then the total balance is always null
Cheers
No, how can you do something without doing anything?
You could permanenetly replace the NULL values with 0 but that would waste a lot of storage.
Transforming your data in a SELECT statement is not terribley costly if you use built in functions designed for that purpose.
The use of coalesce will be the quickest, most effiecient and expediant way to do this.
coalesce(OPENING_OTHER, 0) + coalesce(OPENING_FEE, 0) +
coalesce(OPENING_INT, 0) AS TOTAL_BALANCE
In fact, I'd suggest the actual cost of coalesce is so small that its hard to measure.

Display endless amount of data in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I have an endless amount of data, can I display all of it in sql?
I know there is obviously select *, but then it will never complete.
Is there a command for this?
You can use TOP to select subset of total records
SELECT TOP 100 * from table
This selects top 100 records.
By using Order By clause , you can specify the basis on which subset of records is returned.
Now if you are asking about limits of Sql Server database management system then please see this link - Maximum Capacity Specification of Sql Server
Eg
Max Databases per instance of SQL Server ( both 32 bit and 64 bit ) = 32,767
Usually, you will prefer to use some kind of paging, since you cannot actually show "endless amount of data" in user-friendly way on application.