|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:29 PM
Points: 853,
Visits: 258
|
|
Hi,
Most of the SPs in our db written in a old format style and i dont thing that is the best way of write a query.
eg: 1) in where criteria where 1=1 and ....
I dont think 1=1 is necessary in where criteria. is it affect performance?
2) all sps are written with RETURN stt. I dont think this RETURN required.
can you also suggest some best approach/practice to write a query?
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:04 PM
Points: 13,375,
Visits: 25,159
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:07 PM
Points: 37,687,
Visits: 29,946
|
|
Balaji (1/26/2008)
I dont think 1=1 is necessary in where criteria. is it affect performance?
Not necessary at all. It shouldn't affect performance. The optimiser should be smart enough to ignore it. however, better be safe and take it out.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 2:04 AM
Points: 1,342,
Visits: 1,946
|
|
Balaji (1/26/2008) 1) in where criteria where 1=1 and ....
The only time I've seen this done is in generated queries. That is, a query is built from selecting various options where any combination may occur.sqlstring = "SELECT ... WHERE 1=1" if (opt1) then sqlstring = sqlstring + " AND (...)" if (opt2) then sqlstring = sqlstring + " AND (...)" if (op33) then sqlstring = sqlstring + " AND (...)"
If the queries are 'hardcoded' then you may as well clean them up.
Balaji (1/26/2008) can you also suggest some best approach/practice to write a query? Write it so it returns the right results:)
Seriously... I've found that, although it doesn't affect performance, consistent good formatting helps keep track of what's going on. If your db has all the foreign keys defined properly, then SSMS' view creation is a good way to start; I then edit the SQL to include aliases (otherwise it's far too messy) and finallly switch to a query window to get the formatting right as the layout generated by SSMS is pretty unreadable.
Derek.
Derek
|
|
|
|