Query Optimization By Rick Mason, SeedChoices.com
Just because you can do something in ColdFusion Doesn’t always mean ColdFusion is the best way to do it
Always have the database do as much of the work as possible - Ben Forta
First step : Create Indexes Greatest performance boost with the least effort MS Access automatically indexes primary keys and foreign keys SQL Server doesn’t – you must do it yourself
SQL Server indexes Clustered Non-Clustered
Clustered Indexes Mostly for tables primary key Only one clustered index per table Orders tables data in order you specify
Non-Clustered Indexes You can have multiple non-clustered indexes An important tool in query optimization All non-clustered indexes go through tables clustered index if one is available
Using SQL Server query analyzer
Second step: Stored procedures What is a stored procedure? Pre-compiled SQL statement residing in the database You can pass parameters in You can receive result sets(s) out
Why use stored procedures? Pre-compile once, use many times Stored procedure is a re-usable object Reduce network traffic Separate business rules and logic from presentation layer They’re faster
Writing stored procedures Create procedure[name] Input parameters Output parameters AS Declare local variables SQL statement
Stored procedure example
Calling stored procedures – Using CFQuery {CALL USP_CompanyProductList('#CompanyDistrictID#')}
Calling Stored Procedures - Using cfstoredproc <cfstoredproc procedure=“DealInv” Datasource=“MyData”> <cfprocparam type=“IN” Cfsql type = “CF_SQL_Integer” Value=‘#CompanyDistrictID#’>
You need to use cfstoredproc If you need more than one result set You’re using Oracle or DB2
Running a stored procedure CFQuery or CFStoredProc goes here Your query produced an error #Catch.Detail#
Stored procedure – Insert example Create Procedure varchar(20) AS INSERT Into Person(PersonUserName, Select AS PersonID
What about MS Access? CFStoredProc can call stored Access queries and pass variables CFQUERY CALL will run queries but not let you pass variables Make sure cfadministrator has execute remote procedures turned on for your data source
Calling a stored procedure in Access
Automating Stored Procedure Creation With the Stored Procedure Wizard ftp://ftp.productivityenhancement.com/storedprocwiz10.zip By the folks at ColdFusionTraining.com
Additional references Using Stored Procedures by Ian Rutherford in CF Dev. Journal – Oct, Dec 2001 & Feb, May 2002 Stored Procedures in Access by Charlie Arehart in CF Dev. Journal – Oct 1999 MS SQL Server 7 Administrators Guide by Ron Talmage – ISBN
Contact