• ahmed.net (10/9/2010)


    Witch query is more efficient??

    Table Structure:

    CREATE TABLE [dbo].[tbl_test](

    [ID] [int] NOT NULL,

    [Title] [varchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

    CONSTRAINT [PK_tbl_test] PRIMARY KEY CLUSTERED

    (

    [ID] ASC

    )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]

    )

    Query1:

    SELECT ID, Title

    FROM tbl_test

    WHERE (ID = 1) OR (Title LIKE '%abc%')

    Query2:

    SELECT ID, Title

    FROM tbl_test

    WHERE (Title LIKE '%abc%') OR (ID = 1)

    I just want to know

    Whether or not changing the order of columns in where clause can effect performance(using OR in where)?

    comparing ID field is faster than the like statement on Title column. so if the first statement in OR is true next statement should not be checked..

    I think Query1 is faster as Like statement on title will not always be checked. and in query2 Like statement on title will always be checked...

    What u say ??

    What I say is... trust no one on such a thing. Not even the "pros" even though they're mostly right ;-). Instead, do a test with a million rows and find out for yourself. Many good things will come from that...

    1. You'll know the truth for sure because you will have seen it with your own eyes.

    2. You'll remember the correct answer longer because you will have seen it with your own eyes.

    3. You'll know how to build a million row test table.

    😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)