Date post: | 22-Nov-2014 |
Category: |
Documents |
Upload: | mikhail-joseph-torres |
View: | 1,668 times |
Download: | 4 times |
By Sarah Mae Seguenza Software Developer
EACOMM Corporation
Data search speeds up
Use less resources
Satisfies end-user needs
Overhead process time is directly proportional to the number of concurrent users accessing the database
Makes SQL Queries more efficient
Improves SQL Queries execution time
Try to restrict the queries result set by using the WHERE clause.
This can results in good performance benefits, because SQL Server will return to client only particular rows, not all rows from the table(s). This can reduce network traffic and boost the overall performance of the query.
Try to restrict the queries result set by returning only the particular columns from the table, not all table's columns.
The sql query becomes faster if you use the actual columns names in SELECT statement instead of than ‘*’.
This can results in good performance benefits, because SQL Server will return to client only particular columns, not all table's columns. This can reduce network traffic and boost the overall performance of the query.
Instead of:
SELECT *
FROM dbo.GeneralJournal
Write the query like this:
SELECT JournalID,
Amount,
DateCreated
FROM dbo.GeneralJournal
Use views and stored procedures instead of heavy-duty queries.
This can reduce network traffic, because your client will send to server only stored procedure or view name (perhaps with some parameters) instead of large heavy-duty queries text. This can be used to facilitate permission management also, because you can restrict user access to table columns they should not see.
Try to avoid using SQL Server cursors, whenever possible.
SQL Server cursors can result in some performance degradation in comparison with select statements. Try to use correlated subquery or derived tables, if you need to perform row-by-row operations.
If you need to return the total table's row count, you can use alternative way instead of SELECT COUNT(*) statement.
Because SELECT COUNT(*) statement make a full table scan to return the total table's row count, it can take very many time for the large table. There is another way to determine the total row count in a table. You can use sysindexes system table, in this case. There is ROWS column in the sysindexes table. This column contains the total row count for each table in your database.
Instead of: SELECT COUNT(*)
FROM dbo.GeneralJournal
Write the query like this: MSSQL
SELECT rows
FROMdbo.sysindexes
WHERE id = OBJECT_ID('dbo.GeneralJournal')
AND
indid < 2
MySQL SELECT table_rows,
table_name
FROM information_schema.TABLES
WHERE table_name = 'generaljournal';
Try to avoid the HAVING clause, whenever possible.
The HAVING clause is used to restrict the result set returned by the GROUP BY clause. When you use GROUP BY with the HAVING clause, the GROUP BY clause divides the rows into sets of grouped rows and aggregates their values, and then the HAVING clause eliminates undesired aggregated groups. In many cases, you can write your select statement so, that it will contain only WHERE and GROUP BY clauses without HAVING clause. This can improve the performance of your query.
Instead of: SELECT b.AccountTitle,
a.Amount
FROM dbo.GeneralJournal a
INNER JOIN dbo.Accounts b
ON
a.AccountID = b.ID
GROUP BY a.AccountID,
b.AccountTitle,
a.Amount
HAVING b.AccountTitle LIKE '%Check accom%'
Write the query like this: SELECT b.AccountTitle,
a.Amount
FROM dbo.GeneralJournal a
INNER JOIN dbo.Accounts b
ON
a.AccountID = b.ID
WHERE b.AccountTitle LIKE '%Check accom%'
Use the select statements with TOP keyword or the SET ROWCOUNT statement(MSSQL) or LIMIT(MySQL), if you need to return only the first N rows.
This can improve performance of your queries, because the smaller result set will be returned. This can also reduce the traffic between the server and the clients.
Try to use UNION ALL statement instead of UNION, whenever possible.
The UNION ALL statement is much faster than UNION, because UNION ALL statement does not look for duplicate rows, and UNION statement does look for duplicate rows, whether or not they exist.
Try to use FUNCTIONS or SCALAR-VALUED FUNCTIONS.
Sometimes you may have more than one subqueries in your main query or logical functions. Try to minimize the number of subquery block in your query.
Instead of: SELECT b.AccountTitle,
a.Amount,
(SELECT Amount
FROM dbo.GeneralJournal
WHERE JournalID = a.JournalID
AND Amount > 0) as DebitAmount,
(SELECT Amount
FROM dbo.GeneralJournal
WHERE JournalID = a.JournalID
AND Amount < 0) as CreditAmount
FROM dbo.GeneralJournal a
INNER JOIN dbo.Accounts b
ON
a.AccountID = b.ID
Write the query like this: SELECT b.AccountTitle,
a.Amount,
dbo.GetDebitCreditAmount('debit', a.Amount) as DebitAmount,
dbo.getDebitCreditAmount('credit', a.Amount) as CreditAmount
FROM dbo.GeneralJournal a
INNER JOIN dbo.Accounts b
ON
a.AccountID = b.ID
Use operator EXISTS, IN and table joins appropriately in your query.
a) Usually IN has the slowest performance. b) IN is efficient when most of the filter criteria is in the sub-query. c) EXISTS is efficient when most of the filter criteria is in the main query.
Instead of: SELECT *
FROM dbo.GeneralJournal a
WHERE AccountID IN(
SELECT AccountID
FROM dbo.Accounts
)
Write the query like this: SELECT *
FROM dbo.GeneralJournal a
WHERE EXISTS(
SELECT *
FROM dbo.Accounts
WHERE ID = a.AccountID
)
Index in SQL is created on existing tables to retrieve the rows quickly.
When there are thousands of records in a table, retrieving information will take a long time. Therefore indexes are created on columns which are accessed frequently, so that the information can be retrieved quickly. Indexes can be created on a single column or a group of columns. When an index is created, it first sorts the data and then it assigns a ROWID for each row.
Unique - creates a unique index on a table or view. A
unique index is one in which no two rows are permitted to have the same index key value. A clustered index on a view must be unique.
Clustered - creates an index in which the logical order of
the key values determines the physical order of the corresponding rows in a table. The bottom, or leaf, level of the clustered index contains the actual data rows of the table. A table or view is allowed one clustered index at a time.
Nonclustered - creates an index that specifies the
logical ordering of a table. With a nonclustered index, the physical order of the data rows is independent of their indexed order.
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX IndexName
ON
TableName(Column1, Column2, ….)
e.g. CREATE UNIQUE INDEX IDX_Accounts
ON
Accounts
(AccountID, AccountCode, AccountTitle)
http://www.mssqlcity.com/Tips/tipTSQL.htm
http://beginner-sql-tutorial.com/sql-query-tuning.htm
http://beginner-sql-tutorial.com/sql-index.htm
http://msdn.microsoft.com/en-us/library/ms188783.aspx
EACOMM Corporation
11th Floor CyberOne Bldg
Eastwood Cyberpark
Bagumbayan, Quezon City
PHILIPPINES 1110
Tel. +63 2 4382986
Fax. +63 2 912 6745
Email. [email protected]
Url. http://www.eacomm.com