SQL Optimization Tips/Questions

Below are some of the tips for SQL query optimizations in the form of question/answers.

1. Which of the following query is the most optimized?
a. SELECT column_name FROM table_name WHERE LOWER(column_name) = ‘name’.
b. SELECT column_name FROM table_name WHERE column_name = ‘NAME’ or column_name = ‘name’.
c. SELECT * FROM table_name WHERE LOWER(column_name) = ‘name’
d. SELECT * FROM table_name WHERE column_name = ‘NAME’ or column_name = ‘name’.

Answer: B.
Reason: We should specify the columns in the Select queries and avoid functions like UPPER, LOWER etc as far as possible.

2. Which of the following query generally prevents (but not always) the query optimizer from using an index to perform a search?
a. SELECT member_number, first_name, last_name FROM members WHERE firstname like ‘m%’
b. SELECT member_number, first_name, last_name FROM members WHERE dateofbirth < DATEADD(yy,-21,GETDATE())
c. SELECT member_number, first_name, last_name FROM members WHERE DATEDIFF(yy,datofbirth,GETDATE()) > 21
d. All of these

Answer: C
Reason: Column name is mixed within a function. Hence index cannot be used by optimizer.

3. When we use “NOT IN” our SQL queries, the query optimizer uses which technique to perform the activity?
a. Indexing
b. Clustered Indexed scan
c. Nested table scan
d. None of these

Answer: C

4. Which of the following is the best way of inserting a binary image into database?
a. Use Insert statement
b. Use Stored procedure
c. Both give same performance
d. None of these

Answer: B
Reason: The reason for this is because the application must first convert the binary value into a character string (which doubles its size, thus increasing network traffic and taking more time) before it can be sent to the server. And when the server receives the character string, it then has to convert it back to the binary format (taking even more time).

5. SELECT INTO option locks the system tables. True or false?
Answer: True

6. Using which among “Derived table” and “Temporary table” can we reduce I/O and boost our application’s performance?
Answer: Derived table
Reason: A derived table is the result of using a SELECT statement in the FROM clause of an existing SELECT statement. By using derived tables instead of temporary tables, we can reduce I/O and boost our application’s performance.

7. Which of the following query is the best one in performance to verify the existence of a record in a table:
a. SELECT COUNT(*) FROM table_name WHERE column_name = ‘xxx’
b. IF EXISTS (SELECT * FROM table_name WHERE column_name = ‘xxx’)
c. Both give same performance

Answer: B
Reason: don’t use SELECT COUNT(*) in your Transact-SQL code to identify it, which is very inefficient and wastes server resources.

8. Which of the following is NOT recommended for UPDATE queries in order to reduce the amount of resources required to perform the query:
a. Try not to change the value of a column that is also the primary key.
b. Try to update a column that has a reference in the WHERE clause to the column being updated whenever possible
c. Try to avoid updating heavily indexed columns.
d. When updating VARCHAR columns, try to replace the contents with contents of the same length.

Answer: B
Reason: All points except “B” are recommended points for UPDATE queries. Instead we should try not to update a column that has a reference in the WHERE clause to the column being updated.

9. Omitting of which clause (if possible) can decrease the possibility that a sort operation will occur:
a. DISTINCT
b. ORDER BY
c. LIKE
d. Both A and B

Answer: D

10. Which of the following query will be better in performance?
a. SELECT * FROM Orders WHERE OrderID*3 = 33000
b. SELECT * FROM Orders WHERE OrderID = 33000/3
c. Both are same in terms of performance as well.

Answer: B
Reason: We should avoid computation on columns as far as possible and hence we will get an index scan instead of a seek.

kick it on DotNetKicks.com

Shout it

pimp it

Microsoft Windows 8 will be 128 bit architecture

Shocked? But yes its true. Microsoft has already planned for Windows 7’s successor(s) which are in the early stages of development.

Based on the information on the LinkedIn profiles of the Research team of Microsoft and the resumes floating on the internet it has been a matter of fact that Windows 8 will be having 128bit architecture compatibility.

Have a look at the Microsoft mulling 128-bit versions of Windows 8, Windows 9 to know the exact details of the information.

There’s a whole dedicated website Windows 8 News which is keeping a close eye on the whole project.

And yes we have some concept screen shots available as well on the internet for Windows 8. Following the the URL’s:
Windows 8 Screenshot Concepts
http://www.rajeshpatel.net/8-windows-7-based-themes-for-vista-most-beautiful/
http://www.imagesforme.com/show.php/589721_mojkonceptv1.jpg

Release Date: Rumors say it will be in December 2012. Lets wait and watch what Microsoft Windows 8 has in store for us.

kick it on DotNetKicks.com

Shout it

pimp it

.NET and J2EE. Status as of now and scope in future.(Updated with important note)

Big question today: What is the scope of .NET and J2EE platforms. Which one is more dominant in industry today?

I want to answer this question in this post with my viewpoint with no intentions of hurting anybody but with a sole intention of increasing the knowledge of J2EE for .NET people and .NET for the J2EE people
and being a developer in Microsoft technologies i agree my opinion may be aligned towards .NET but what i sincerely want is that through this platform i want to know the actual status of the platforms viz a viz features and invite everybody to add to my learning the new things happening in the J2EE world as well.

On Technology Front
I think that with the arrival of Mono, the only disadvantage that .NET had over J2EE platform i.e of platform independence has been resolved up to an extent. But with the introduction of WCF, WPF, Workflow Foundation, LINQ, ASP.NET MVC framework, Silverlight,ASP.NET Ajax and many such other features have only added to the popularity as well as usefulness of .NET both for programmers, designers and enterprises. With .NET 4.0 parallel programming framework, improved WCF and host of improvements along with Visual studio 2010 will be a great advantage for all..NET 4.0 will also be integrating cloud computing platforms. You will agree to the fact that coupling of IIS and SQL Server is superb in terms of performance as compared to any other options.

Today designers and developers can work together on the tool provided by Microsoft and the UI can be deployed to Web,Desktop or Mobile with an assurance of same effects.
I think JAVA have been left far behind on the technology terms as compared to .NET over past few years as I have not come across any such features being introduced in J2EE platform.

What customers want is fast development of the solution at low cost, and if we start of with .NET we can develop the solution at a very fast rate, thanks to the host of tools provided by Microsoft, and then use cloud computing to bring down the IT costs.You get .NET resources fast as well ,thanks to initiatives taken by Microsoft to teach students and the interest they are able to generate within the students community.

I believe that with the launch .NET 3.5 and onwards, there no looking back for .NET platform as it has enabled the developers to provide good quality extensible code at a fast rate to clients taking the full advantage of the latest operating systems plus giving the backward compatibility to most applications previously built on .NET platform. What more can customers ask for?

I also believe that yes J2EE is also here to stay for a long time as it also has got a big customer base but to compete with the .NET platform they need to pick up fast and offer some features which Microsoft has already done with, to at least get back into competition with .NET

Important NOTE:
I am in NO WAY attached to Microsoft except for the fact that i am a .NET developer and get excited with all the new things happening in the technology world and due to my busy schedule i am not able to keep myself updated on Java. So i started off with my little knowledge of the features i have about .NET and invite everybody here to discuss and put forward the corresponding features from J2EE platform. That’s it. It is of minimal importance for me as which of J2EE or .NET is more popular but what matters is i should know tomorrow that if i want to do something how is that possible with the help of two options and which one fits the best in that situation and what other features can we expect from both the fronts in the near future
I would request you not to comment as in way of showing down J2EE or .NET as i will delete those comments.

Constructive Comments are most welcome.

kick it on DotNetKicks.com

Shout it

pimp it