Save Database Resources by Being More Selective

When doing a database query with a SQL statement, it is convenient to use the asterisk (*) to retrieve all of the fields in the table whether you need them or not. However, this inefficient query wastes server time and bandwith. The database spends time figuring out the available field names and data types before getting around to fetching the data. For the most efficiency, use the name of each field that you want in the SQL statement. For example, instead of this code:

 'Set rs = conn.Execute("SELECT * FROM Employees")


 Set rs = conn.Execute("SELECT LastName FROM Employees")

Here’s an ASP script that uses this tip to read only one field, the last name:

 <%@ Language=VBScript %><%Set conn = CreateObject("ADODB.Connection")strConn="Provider=SQLOLEDB;Data Source=p450;Initial Catalog=Northwind;User ID=sa;Password=;"conn.Open strConnSet rs = conn.Execute("SELECT LastName FROM Employees")While Not rs.EOF    Response.Write rs("LastName") & "
" rs.MoveNextWendrs.Closeconn.CloseSet rs = NothingSet conn = Nothing%>
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: