Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Microsoft SQL Server

Microsoft SQL Server

Ratings: (0)|Views: 12|Likes:
Published by prasatha90
bb
bb

More info:

Categories:Types, Research, Science
Published by: prasatha90 on May 25, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

05/25/2011

pdf

text

original

 
Page 1 of 13
Microsoft SQL Server # Interview Questions - (last updated on )
 
y
 
Transact-SQL Optimization Tips
y
 
I
ndex Optimization tips
y
 
T-SQL Queries
y
 
D
ata Types
y
 
I
ndex
y
 
J
oins
y
 
Lock
y
 
Stored Procedure
y
 
Trigger
y
 
V
iew
y
 
Transaction
y
 
Other
y
 
XM
L
y
 
Tools
y
 
Permission
y
 
A
dministration
T
ransact-SQL Optimization
T
ips
 
y
 
U
se views and stored procedures instead of heavy-duty queries.
 This can reduce network traffic, because your client will send to serveronly stored procedure or view name (perhaps with some parameters)instead of large heavy-duty queries text. This can be used to facilitatepermission management also, because you can restrict user access totable columns they should not see.
y
 
T
ry to use constraints instead of triggers, whenever possible.
 Constraints are much more efficient than triggers and can boostperformance. So, you should use constraints instead of triggers,whenever possible.
y
 
U
se table variables instead of temporary tables.
 Table variables require less locking and logging resources thantemporary tables, so table variables should be used whenever possible.The table variables are available in SQL Server 2000 only.
y
 
T
ry to use
UN
IO
N
ALL statement instead of 
UN
IO
N
, wheneverpossible.
 The UN
I
ON
A
LL statement is much faster than UN
I
ON, because UN
I
ON
A
LL statement does not look for duplicate rows, and UN
I
ON statementdoes look for duplicate rows, whether or not they exist.
y
 
T
ry to avoid using the DIS
T
I
NCT
clause, whenever possible.
 Because using the
DI
ST
I
NCT clause will result in some performancedegradation, you should use this clause only when it is necessary.
y
 
T
ry to avoid using SQL Server cursors
 
, whenever possible.
 SQL Server cursors can result in some performance degradation incomparison with select statements. Try to use correlated sub-query orderived tables, if you need to perform row-by-row operations.
y
 
T
ry to avoid the HAVI
NG
clause, whenever possible.
 The H
A
VI
NG clause is used to restrict the result set returned by the
 
Page 2 of 13
GROUP BY clause. When you use GROUP BY with the H
A
VI
NG clause, theGROUP BY clause divides the rows into sets of grouped rows andaggregates their values, and then the H
A
VI
NG clause eliminatesundesired aggregated groups.
I
n many cases, you can write your selectstatement so, that it will contain only WHERE and GROUP BY clauseswithout H
A
VI
NG clause. This can improve the performance of yourquery.
y
 
If you need to return the total table's row count, you can usealternative way instead of SELE
CT
 
C
O
UNT
(*) statement.
 Because SELECT COUNT(*) statement make a full table scan to returnthe total table's row count, it can take very many time for the largetable. There is another way to determine the total row count in a table.You can use sysindexes system table, in this case. There is ROWScolumn in the sysindexes table. This column contains the total row countfor each table in your database. So, you can use the following selectstatement instead of SELECT COUNT(*): SELECT rows FRO
M
sysindexesWHERE id = OB
J
ECT_
ID
('table_name')
A
N
D
indid < 2 So, you canimprove the speed of such queries in several times.
y
 
Include SE
T
 
N
O
C
O
UNT
O
N
statement into your stored proceduresto stop the message indicating the number of rows affected by a
T
-SQL statement.
 This can reduce network traffic, because your client will not receive themessage indicating the number of rows affected by a T-SQL statement.
y
 
T
ry to restrict the queries result set by using the WHERE clause.
 This can results in good performance benefits, because SQL Server willreturn to client only particular rows, not all rows from the table(s). Thiscan reduce network traffic and boost the overall performance of thequery.
y
 
U
se the select statements with
T
OP keyword or the SE
T
 ROW
C
O
UNT
statement, if you need to return only the first nrows.
 This can improve performance of your queries, because the smallerresult set will be returned. This can also reduce the traffic between theserver and the clients.
y
 
T
ry to restrict the queries result set by returning only theparticular columns from the table, not all table's columns.
 This can results in good performance benefits, because SQL Server willreturn to client only particular columns, not all table's columns. This canreduce network traffic and boost the overall performance of the query.1.
I
ndexes2.avoid more number of triggers on the table3.unnecessary complicated joins4.correct use of Group by clause with the select list5.in worst cases
D
enormalization
Index Optimization tips
 
 
Page 3 of 13
y
 
Every index increases the time in takes to perform
I
NSERTS, UP
D
A
TESand
D
ELETES, so the number of indexes should not be very much. Try touse maximum 4-5 indexes on one table, not more.
I
f you have read-onlytable, then the number of indexes may be increased.
y
 
K
eep your indexes as narrow as possible. This reduces the size of theindex and reduces the number of reads required to read the index.
y
 
Try to create indexes on columns that have integer values rather thancharacter values.
y
 
I
f you create a composite (multi-column) index, the order of the columnsin the key are very important. Try to order the columns in the key as toenhance selectivity, with the most selective columns to the leftmost of the key.
y
 
I
f you want to join several tables, try to create surrogate integer keysfor this purpose and create indexes on their columns.
y
 
Create surrogate integer primary key (identity for example) if your tablewill not have many insert operations.
y
 
Clustered indexes are more preferable than nonclustered, if you need toselect by a range of values or you need to sort results set with GROUPBY or OR
D
ER BY.
y
 
I
f your application will be performing the same query over and over onthe same table, consider creating a covering index on the table.
y
 
You can use the SQL Server Profiler Create Trace Wizard with "
I
dentifyScans of Large Tables" trace to determine which tables in your databasemay need indexes. This trace will show which tables are being scannedby queries instead of using an index.
y
 
You can use sp_
M
Sforeachtable undocumented stored procedure torebuild all indexes in your database. Try to schedule it to execute duringCPU idle time and slow production periods.sp_
M
Sforeachtable @command1="print '?'
D
BCC
D
BRE
I
N
D
E
X
('?')"
T
-SQL Queries
 
1.
 
2 tables
Employee
 
Phone
 empidempnamesalarymgridempidphnumber
2.
 
Select all employees who doesn't have phone?SELECT empnameFRO
M
EmployeeWHERE (empid NOT
I
N(SELECT
DI
ST
I
NCT empidFRO
M
phone))3.
 
Select the employee names who is having more than one phonenumbers.SELECT empnameFRO
M
employee

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->