+ All Categories
Home > Documents > SQL Optimization

SQL Optimization

Date post: 22-Nov-2014
Category:
Upload: mikhail-joseph-torres
View: 1,668 times
Download: 4 times
Share this document with a friend
Description:
SQL or Structured Query Language has been the standard protocol of most Relational Database Systems of the past decades. It runs the backbone of practically the whole Internet as all the major database systems (Oracle, MSSQL, MySQL, etc...) utilizes it. As such it is a critical language to know for web programmers. A very basic knowledge of SQL can help even the most novice programmer to start developing simple web or software applications such as Blogs, Web Stores, Dynamic Websites and others. With the large computing power available to us even in the simplest web hosting servers, it is oftentimes neglected that our Database design and queries are optimized for the fastest possible performance. However, when we are dealing with tens of thousands of website visitors or hundreds of thousands of database entries, SQL optimization is a must. The following presentation discusses simple SQL optimization techniques that can greatly improve your software or web application's performance.
22
By Sarah Mae Seguenza Software Developer EACOMM Corporation
Transcript
Page 1: SQL Optimization

By Sarah Mae Seguenza Software Developer

EACOMM Corporation

Page 2: SQL Optimization

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

Page 3: SQL Optimization

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.

Page 4: SQL Optimization

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.

Page 5: SQL Optimization

Instead of:

SELECT *

FROM dbo.GeneralJournal

Write the query like this:

SELECT JournalID,

Amount,

DateCreated

FROM dbo.GeneralJournal

Page 6: SQL Optimization

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.

Page 7: SQL Optimization

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.

Page 8: SQL Optimization

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.

Page 9: SQL Optimization

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';

Page 10: SQL Optimization

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.

Page 11: SQL Optimization

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%'

Page 12: SQL Optimization

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.

Page 13: SQL Optimization

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.

Page 14: SQL Optimization

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.

Page 15: SQL Optimization

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

Page 16: SQL Optimization

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.

Page 17: SQL Optimization

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

)

Page 18: SQL Optimization

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.

Page 19: SQL Optimization

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.

Page 20: SQL Optimization

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX IndexName

ON

TableName(Column1, Column2, ….)

e.g. CREATE UNIQUE INDEX IDX_Accounts

ON

Accounts

(AccountID, AccountCode, AccountTitle)

Page 22: SQL Optimization

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


Recommended