SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
SQL Azure
Developer
General DBA
ASP.NET / ADO.NET
SQL Azure

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server - Part ...
A High Level Comparison Between Oracle and SQL Server

More     
 
Latest FAQ's

Add Node to A SQL Server failover Cluster failed with invalid ...
SQL Server Destination remote server error
Setting Up Data And Log Files For SQL Server
Will Check Constraints Improve Database Performance?

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

articles >> general dba >> Pros & Cons of Using SELECT, Views, ...

Pros & Cons of Using SELECT, Views, and Stored Procedures in SQL Server

By : G. Vijayakumar
Jan 08, 2004

Page 5 / 6

Overall, stored procedure will compile once and use the same compiled plan and increment the Usecounts value of the executable plan. A stored procedure is pre-loaded in memory for faster execution.  It will really augment your system on performance base. This really shows us the importance of the stored procedure as compared with the SELECT statement and views.

Now, let’s execute the following commands to clear the cache before the upcoming view experiment.

DBCC FREEPROCCACHE

GO

 

Views

We will create a view and how it is differs from theSELECT statement and stored procedures.

CREATE VIEW vwDummyTable1 AS

SELECT EmpID, EmpName FROM DummyTable1

GO

Now, let’s execute the following commands to display the data and cache information for the vwDummyTable1 we created.

SELECT EmpId, EmpName from vwDummyTable1

GO

SELECT cacheobjtype, refcounts, usecounts, sql FROM master.dbo.Syscacheobjects

GO

Cacheobjtype Refcounts Usecounts Sql
Executable Plan

1

1

()SELECT EmpID, EmpName FROM [vwDummyTable1]
Compiled Plan

2

2

()SELECT EmpID, EmpName FROM [vwDummyTable1]

SQL Server has displayed the compiled and executable plan for the spDummyTable1 stored procedure.

Let’s now execute the same statement again and view the cache details.

SELECT EmpId, EmpName from vwDummyTable1

GO

SELECT cacheobjtype, refcounts, usecounts, sql FROM master.dbo.Syscacheobjects

GO

Cacheobjtype Refcounts Usecounts Sql
Executable Plan

1

2

()SELECT EmpID, EmpName FROM [vwDummyTable1]
Compiled Plan

2

2

()SELECT EmpID, EmpName FROM [vwDummyTable1]

The value of Usecounts has been incremented. SQL Server has used the same compiled plan for the SELECT statement on the view and incremented the Usecounts of the executable plan. N number user will use the same compiled plan when we execute the same SELECT statement on view.

Now, let’s add a WHERE clauses on the view SELECT statement and view the results from the master.dbo.Syscacheobjects.

SELECT EmpId, EmpName from vwDummyTable1 WHERE EmpID = 4

GO

SELECT cacheobjtype, refcounts, usecounts, sql FROM master.dbo.Syscacheobjects

GO

SQL Server has not used the existing cache plan because of change in the view’s SELECT statement. SQL Server generates a new cache plan for the SELECT statement on view along with old cache plan.

Cacheobjtype Refcounts Usecounts Sql
Executable Plan

1

1

(@1 tinyint)SELECT EmpID, EmpName FROM [vwDummyTable1] WHERE [EmpID]=@1
Compiled Plan

2

2

(@1 tinyint)SELECT EmpID, EmpName FROM [vwDummyTable1] WHERE [EmpID]=@1
Executable Plan

1

2

()SELECT EmpID, EmpName FROM [vwDummyTable1]
Compiled Plan

2

2

()SELECT EmpID, EmpName FROM [vwDummyTable1]

Let’s execute the same view SELECT statement with different empid and view the results.

SELECT EmpId, EmpName FROM vwDummyTable1 WHERE EmpID = 8

GO

SELECT cacheobjtype, refcounts, usecounts, sql FROM master.dbo.Syscacheobjects

GO

Cacheobjtype Refcounts Usecounts Sql
Executable Plan

1

2

(@1 tinyint)SELECT [EmpId]=[EmpId],[EmpName]=[EmpName] FROM [DummyTable1] WHERE [EmpId]=@1
Compiled Plan

2

2

(@1 tinyint)SELECT [EmpId]=[EmpId],[EmpName]=[EmpName] FROM [DummyTable1] WHERE [EmpId]=@1
Executable Plan

1

2

()SELECT [EmpId]=[EmpId],[EmpName]=[EmpName] FROM [DummyTable1]
Compiled Plan

2

2

()SELECT [EmpId]=[EmpId],[EmpName]=[EmpName] FROM [DummyTable1]

Though we have given different empid value, SQL Server has used the same compiled plan for the SELECT statement on the view and incremented the Usecounts of the executable plan.


<< Prev Page     Next Page>>    








C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved