私のASP.Netアプリケーションには、Productテーブルから高さ値の明確なリストを取得するために、次のLinq to SQL関数があります。LinqからSQLへのスロークエリ
public static List<string> getHeightList(string catID)
{
using (CategoriesClassesDataContext db = new CategoriesClassesDataContext())
{
var heightTable = (from p in db.Products
join cp in db.CatProducts on p.ProductID equals cp.ProductID
where p.Enabled == true && (p.CaseOnly == null || p.CaseOnly == false) && cp.CatID == catID
select new { Height = p.Height, sort = Convert.ToDecimal(p.Height.Replace("\"", "")) }).Distinct().OrderBy(s => s.sort);
List<string> heightList = new List<string>();
foreach (var s in heightTable)
{
heightList.Add(s.Height.ToString());
}
return heightList;
}
}
私はこのクエリが多くのリソースを使用していることを示すRedgate SQL Monitorを実行しました。
レッドゲートはまた、私は次のクエリを実行していることを示している:
select count(distinct [height]) from product p
join catproduct cp on p.productid = cp.productid
join cat c on cp.catid = c.catid
where p.enabled=1 and p.displayfilter = 1 and c.catid = 'C2-14'
私の質問は以下のとおりです。
- それが少ないリソースを使用するように、機能を変更する提案?
- また、linq to sqlは私の関数から上記のクエリをどのように生成しますか? (私はコード内の任意の場所にselect count(distinct [height])を書きませんでした)
製品には9万レコードあります。私は高さの明確なリストを取得しようとしています。このカテゴリには、50,000製品のレコードを持っている
、事前にありがとう
ニック
SQLをSQL Server Management Studioにコピーし、実行計画を取得します。あなたの質問に実行計画を掲載してください。 cat.catidはインデックスに登録されているのですか? –
投稿されたSQLクエリとlinqクエリがまったく一致しません – Rahul
Management Studioでそのクエリを試しましたか?期待される実行計画機能で調査します。これにより、テーブルにインデックスがない場合のヒントが得られます –