Sargable Queries part 2. Examples
Dear Readers, Today we return to the SARGABLE Queries topic started some days ago. Let's take some examples of Queries that are SARGABLE and Query that are not! If you missed the first "episode" you still don't know the importance of SARGABLE Queries read my previous article here Ready? I hear you say: yes yes.. So let's go! Some Examples Let's create a simple test table and fill it with a bit of data: CREATE TABLE ELENCO (ID INT IDENTITY(1,1), CODICE VARCHAR(3) CONSTRAINT PK_Elenco_ID PRIMARY KEY CLUSTERED (id)) The ID field is self-incrementing and is the clustered index. As a first example , let's write one of the simpler Queries SELECT ID FROM LIST WHERE ID = 5 Let's see the execution plan: As we would expect the clustered index is used and a SEEK operation is performed. Now let's change the Query slightly: SELECT ID FROM LIST WHERE ID + 1 = 5 ...