Question about Join conditions and Indexes

  • Hi,

    I am confused at one point and need some directions.

    Lets say that I have a query which has a join between two tables on a column which is a 'int' in one table and varchar in another.

    Now, during the execution I have seen that one of the columns will be cast and index will be used accordingly. I learnt that 2008 optimizer has improved a lot and it might show index seek but under the carpet it still has to do more work to compare two columns with different data types.

    So the question is should i introduce a cast during the join only or shall i leave it for the optimizer to do that.

    Best regards

    Chandan

  • You should avoid joining on different data types if you possibly can.

    If you use cast() in the join, at least everyone can see what's happening. An implicit cast is 'hidden' and can be more difficult to find.

    Both implicit & explicit casts will usually result in the optimiser not being able to use any indexes which are available.

    If you must do this, test it carefully to check for the most efficient option. Also put a note in the code to highlight the issue.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply