A SQL runs very slow: select * from t1 inner join t2 on t1.col1=t2.col1 where t2.col2=@v1, the variable @v1 is assigned an INT value right before. Then I changed it using a dynamic SQL:
SET @sql='select * from t1 inner join t2 on t1.col1=t2.col1 where t2.col2='+convert(varchar,@v1), exec (@sql). after the change, it runs much much faster. Did someone can help to explain why? Thanks. Note, in real job, the table joining is much more complicated than I write here, so the performance improvement is significant.
SET @sql='select * from t1 inner join t2 on t1.col1=t2.col1 where t2.col2='+convert(varchar,@v1), exec (@sql). after the change, it runs much much faster. Did someone can help to explain why? Thanks. Note, in real job, the table joining is much more complicated than I write here, so the performance improvement is significant.