Linq to SQL: select optimization

后端 未结 6 1013
梦如初夏
梦如初夏 2021-01-18 11:16

On large tables in MSSQL; selecting specific columns results in greater speed of the query. Does the same apply to Linq to SQL?

Would this:

var perso         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 12:11

    There are 3 aspects with "faster" here.

    1. less data transmitted means faster. On the other hand it will not get that significantly faster, unless you select more than one row or if your Person contains some other "heavy" columns - long varchars, image etc.
    2. as J. Curran pointed out, less memory allocated means faster. Same remark as in 1. applies here.

    3. Your query executes faster if you have an index containing all selected columns (or attached to it starting from SQL Server 2005). In this case the SQL Server engine does not need to load the page with the row in memory - if it's not there yet.

    Personally I wouldn't bother trying to optimize my queries this way (unless, as I said your rows contain binary data or very long strings that you don't need), partially because if you decide later that you'd like to have more information about this selected Person, you would need to change your DB access code vs. just accessing a property in your POCO/anonymous class.

提交回复
热议问题