Getting with SQL

Share This Class:

Overview

To review strategies and guidelines to follow in order to achieve a better development of applications in SQL Server platform. To encourage analysts and developers to apply an approach oriented to the optimisation of Interinsurance applications through the use of standards and good practices.

1. Table names should be singular, should not use blank spaces in the name and should be related to the data to be stored.


2. Trigger names must follow the ut_<name> standard.


3.View names must follow the vw_<name> standard.
4.Stored procedures names must follow the usp_<name> standard, do not use sp_which is an error.


5.Indexes are named considering the table they are related to and the purpose of the index.
-Primary keys use the text « PK » as a suffix or prefix, as deemed appropriate.
-Foreign keys use the text « FK » as suffix or prefix, as appropriate.
-Clustered indexes use the suffix or prefix « IDX » as appropriate

Documentation

 1.Attach comments at the beginning of stored procedures at the end of the stored procedure


/***************************************************************************************************

Purpose: <Description>.
Created by:
Date created:
Modified by:
Last Modified: <Description>
Fecha modificación

****************************************************************************************************/


Read

1.Avoid using SELECT *. Always read only the columns you need. This avoids bringing a lot of unneeded data to the client, so it relieves network congestion and the client feels it is faster.

2. For queries of lists of records, use the TOP n operator. This avoids bringing a lot of records to the client. It also relieves network congestion and the client feels it is faster.

3. Instead of SET ROWCOUNT n, use TOP n.

4. If you use the UNION operator and you are sure that both queries do NOT have duplicate records, then use UNION ALL, to avoid implicitly using the DISTINCT operator.

5. Avoid using SELECT … INTO table_name. This will lock tables in the system. Instead, create the tables first and then rewrite the statement as INSERT INTO table_nameSELECT ….


6. If you are going to read data from a single table, avoid doing so using views that use other tables.


7. Avoid using select statements with operations on the fields you are going to get.
Example:

 

Select convert(int(campo1)) + fn_funcion_propia( campo2, campo3) from
 
SELECT member_number, first_name, last_name, room_number
FROM members, vi_members
WHERE members.room_number = vi_members.room_number
(
la vista es:
SELECT member_number, first_name, last_name, room_number
FROM members where state = ‘A’
)
 
Script

1. Do not use the select … form. into table_name, you must first create the table and then execute insert into table_name (field1,field2…) select column3, column4 from t2

2. Specify the columns to insert informationSayInsert into Table values (…)Must sayInsert into Table (field1,field2, …) values (…)
 

Joins

3. Write joins in ANSI format (use the JOIN .. ON clause). This ensures that you write all constraints without the possibility of forgetting a constraint.

4.Avoid using the same table more than once in a single query. To improve this, use temporary tables

5.Prefer to use a join instead of a sub-query.
It says

SELECT member_number, first_name, last_name, room_number
FROM members
WHERE room_number IN (SELECT rooms.room_number FROM rooms)

Must say

SELECT member_number,first_name, last_name, room_number
FROM members m
INNER JOIN rooms r ON m.room_number = r.room_number

6.In case you decide to use nested queries, always qualify the fields of the subquery with the corresponding table

SELECT member_number, first_name, last_name, room_number
FROM members
WHERE room_number IN (SELECT rooms .room_number
FROM rooms)
 
7. Instead of using a statement with many joins where the tables involved are large, better create a temporary table with the data from the main table (codes) and then update this table by making joins with the secondary tables
 
8. Instead of using the IN clause in conjunction with a sub-query, use a JOIN statement.

It says
 
SELECT pub_name
FROM dbo.Publishers
WHERE pub_id IN
(SELECT Titles.pub_id FROM Titles)
 
It should read
 
SELECT pub_name
FROM dbo.Publishers JOIN dbo.Titles
ON Titles.pub_id = Publishers.pub_id
 
Where

1. Avoid using functions on columns in the WHERE clause For example instead of using
 
SELECT
member_number, first_name, last_name
FROM members
WHERE DATEDIFF(yy,datofbirth,GETDATE()) > 21
 
Use
 
SELECT member_number, first_name, last_name
FROM members
WHERE dateofbirth < DATEADD(yy,
21,GETDATE())
 
2. If you use LIKE in the WHERE clause, try to use at least 3 characters in front like « abc% »
 
3. If you use LIKE in the WHERE clause, avoid using the % operator at the beginning: « %abc « 
 
4. Where possible use the BETWEEN clause instead of INFor example instead of using
 
SELECT customer_number, customer_name
FROM customer
WHERE customer_number in (1000, 1001, 1002, 1003, 1004)
 
Use
 
SELECT customer_number, customer_name
FROM customer
WHERE customer_number BETWEEN 1000 and 1004
 
5. Avoid using concatenations in WHERE clauses.
 

General


1.Avoid using hardcode


2.Avoid using cursors. Instead use temporary tables with an integer field identity(1,1) which you can sweep sequentially. Don’t forget to index the identity field.


3.Consider that the MIN and MAX functions can use indexes. If possible, create indexes on the columns on which these functions are used.


4.When using temporary tables, consider creating indexes on them to increase the performance of your queries.


5.Check every query of a stored procedure and make sure it doesn’t do a table scan. To study this, enable the « Show Query Plan » directive in the Query Analyzer.


6.Try to make each query use an index on a column that has a high dispersion.


7. If possible, use derived tables instead of temporary tables. This is more reasonable if the data you would store in the temporary table is going to be used once. But if this data is going to be used many times within a stored procedure, then a temporary table is better.

For example instead of using
 
CREATE TABLE #Temp_Example (
[CategoryID] INT NOT NULL,
[Category_Count] INT NOT NULL)
 
INSERT INTO #Temp_Example (CategoryID, Category_Count)
 
SELECT CategoryID, COUNT(*) AS Category_Count
FROM Categories
GROUP BY CategoryID
 
SELECT C.CategoryID, C.CategoryName, P.ProductName, P.UnitPrice,
#Temp_Example.Category_Count
FROM Categories C
INNER JOIN Products P ON C.CategoryID = P.CategoryID
INNER JOIN #Temp_Example ON C.CategoryID = #Temp_Example.CategoryID
ORDER BY C.CategoryName
 
DROP TABLE #Temp_Example
 
Use
 
SELECT C.CategoryID, C.CategoryName
, P.ProductName, P.UnitPrice,
C_Count.Category_Count
 
FROM Categories C
 
INNER JOIN Products P ON C.CategoryID = P.CategoryID
 
INNER JOIN (
      SELECT CategoryID, COUNT(*) AS Category_Count
      FROM Categories
       GROUP BY CategoryID
)    C_Count ON C.CategoryID = C_Count.CategoryID
 
ORDER BY
 

If you are going to update, insert or delete very many records in a single statement, it is better to do it in small parts.

Transactionality


1. Never break a transaction into two transactions that are invoked consecutively from the client. This causes the database to be corrupted if the second transaction fails.


2. Keep your transactions small, i.e. access as few pages of the database as possible.


3. Avoid declaring a single large transaction for batch processes. It is better to have several small transactions and handle reprocessing.

 

What people are saying