How To Make SQL Server Search Faster - Part Two

In this article, we will discuss how to make faster SQL Server Search - Part Two.

This will be based on one table with 500k records that are filtered to build the weekly schedule. In this post, I'll show one of the things that I did in practice  to load more than 400 records for each day, from Monday to Monday, in less than 2 seconds, in my case already with the HTML embedded. 
 
Consider this table and then select from it,
  1. CREATE TABLE [Accounts](  
  2.    [accArea] [intNULL,  
  3.    [accSubArea] [intNULL,  
  4.    [accName] [nvarchar](50) NOT NULL,  
  5.    [accCodeID] [int] IDENTITY(1,1) NOT NULL,  
  6.    [accDateAccess] [datetime] NULL  
  7. );  
  8.    
  9. "Select * from Accounts where accArea=1 AND accSubArea=4 and accName like 'Maria%' ORDER BY accDateAccess DESC;"
What is wrong and what must be done
 
Do not use "*", name the fields that you want to work.
  1. "Select accName, accCodeID from Accounts where accArea=1 AND accSubArea=4 and accName like 'Maria%' ORDER BY accDateAccess DESC;" 
Indexes

Your table must be indexed in the same order of the WHERE clause.
  1. CREATE INDEX [IX_Accounts] ON [Accounts]  
  2. (  
  3.    [accArea] ASC,  
  4.    [accSubArea] ASC,  
  5.    [accName] ASC,  
  6.    [accDateAccess] DESC*  
  7. )  
*Observe that this field is indexed in descending mode.
 
Ordinate fields by numbers or booleans first in the WHERE clause is correct.
 
CONCLUSION
 
Create indexes as much you need if you have big tables.
 
This applies to relational databases. 
 
Have a nice coding experience.