How can I keep Entity Framework from generating inefficient queries in SQL Server?

后端 未结 2 1954
天命终不由人
天命终不由人 2021-01-19 05:00

I have an entity defined in an EF 4.0 that is based on a view. The view lays over a table with about 18 million rows of data. I have selected the 4 deterministic properties

相关标签:
2条回答
  • 2021-01-19 05:07

    If there is consistency in the names and order of the columns in the 'order by' clause, there is likely desirability for this result by the users.

    Just put an index on the table, or modify the primary key, so that index can be used in the retrieval.

    create index x on [dbo].[ftCostBenchmark]
                          ([MonthBeginDt],
                           [dmCostBenchmarkKey],
                           [dmProductKey],
                           [IsImputedFlg])
                      include
                           (existing-primarykey-on-the-table)
    

    Odd that there is no where clause on the query. If you trimmed a where-clause for brevity, put those fields first in the index.

    0 讨论(0)
  • 2021-01-19 05:12

    Row limits don't make any sense without specifying an order.

    If you want to retrieve records in storage order, create a clustered index and explicitly specify it in your query.

    One way to do this is with a query interceptor.

    0 讨论(0)
提交回复
热议问题