0

ほぼ 2 分かかるクエリがあります。改善方法を教えていただけないでしょうか。Ads クラスでわかるように、データベースの設計は良くありません...列の型を変更せずにクエリを最適化することは可能ですか?

AdStatus列を整数に簡単に変更できます...しかし、変更するOperationTypeAdTypeは大きな移行が必要になります。ところで、緯度と経度の値だけを使用してクエリを実行しようとしましたが、これも非常に遅かったです。

Adsテーブルには 230 万のアイテムがあり、テーブルPictureadsには 1,100 万のアイテムがあります

Amazon RDS t2.micro を使用しています

また、モデルに変更が必要な点がありましたら、お知らせください。

var queryResults = (from d in ApplicationContext.Ads.AsNoTracking()
    where (d.Latitude >= latitude - radio)
    && (d.AdStatus != "Pending")
    && (d.Latitude <= latitude + radio)
    && (d.Longitude >= longitude - radio)
    && (d.Longitude <= longitude + radio)
    && (String.IsNullOrEmpty(operationType) || d.OperationType == operationType)
    && (!(priceMax > priceMin && (priceMin > 0 || priceMax > 0)) || (d.USDPrice >= priceMin && d.USDPrice <= priceMax))
    && (String.IsNullOrEmpty(adType) || d.AdType == adType)
    orderby d.USDPrice ascending
    select new
    {
        d.AdsID,
        d.Username,
        d.administrative_area_level_1,
        d.administrative_area_level_2,
        d.administrative_area_level_3,
        d.neighborhood,
        d.Address,
        d.PictureUrl,
        d.Latitude,
        d.Longitude,
        d.ExpirationDate,
        d.FeaturedAd,
        d.PremiumAd,
        d.Views,
        d.Code,
        d.OperationType,
        d.Price,
        d.USDPrice,
        d.PriceCurrency,
        d.AdType,
        d.Rooms,
        d.Restrooms,
        d.Description,
        d.AdStatus
    });

queryResults.Count() または queryResults.ToArray() を呼び出すと、ほぼ 2 分かかります。

モデル:

public class Ads
{
    [Key]
    [Index]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int AdsID { get; set; }
    [Index]
    [MaxLength(128)]
    public string Username { get; set; }
    [Index]
    [MaxLength(64)]
    public string country { get; set; }
    [Index]
    [MaxLength(64)]
    public string administrative_area_level_1 { get; set; }
    [Index]
    [MaxLength(64)]
    public string administrative_area_level_2 { get; set; }
    [MaxLength(64)]
    public string administrative_area_level_3 { get; set; }
    [MaxLength(64)]
    public string neighborhood { get; set; }
    [MaxLength(64)]
    public string Address { get; set; }
    // Navigation property
    public virtual ICollection<PictureAds> Pictures { get; set; }
    [MaxLength(256)]
    public string PictureUrl { get; set; }
    [MaxLength(256)]
    public string UserPicUrl { get; set; }
    [Index]
    [DisplayFormat(DataFormatString = "{0:n15}", ApplyFormatInEditMode = true)]
    public decimal Latitude { get; set; }
    [Index]
    [DisplayFormat(DataFormatString = "{0:n15}", ApplyFormatInEditMode = true)]
    public decimal Longitude { get; set; }
    [DataType(DataType.Date)]
    public DateTime CreatedDate { get; set; }
    [DataType(DataType.Date)]
    public DateTime ExpirationDate { get; set; }
    public int MonthsAlive { get; set; }
    public decimal PriceSurfaceRatio { get; set; }
    public bool FeaturedAd { get; set; }
    public bool PremiumAd { get; set; }
    public int Views { get; set; }
    [MaxLength(256)]
    public string Code { get; set; }
    [MaxLength(256)]
    public string Title { get; set; }
    [MaxLength(32)]
    public string SunOrientation { get; set; }
    public bool IsFurnished { get; set; }
    [Required]
    [MaxLength(64)]
    public string Name { get; set; }
    [Required]
    [MaxLength(64)]
    public string Email { get; set; }
    [Required]
    [MaxLength(32)]
    public string Phone { get; set; }
    [Required]
    [MaxLength(32)]
    public string UserType { get; set; }
    [Required]
    [Index]
    [MaxLength(32)]
    public string OperationType { get; set; }
    [Required]
    public int Price { get; set; }
    [Index]
    public int? USDPrice { get; set; }
    [Required]
    [MaxLength(16)]
    public string PriceCurrency { get; set; }
    [Required]
    [Index]
    [MaxLength(32)]
    public string AdType { get; set; }
    [Required]
    public int SizeTotal { get; set; }
    public int SizeIndoor { get; set; }
    public int SizeOutdoor { get; set; }
    public int Expenses { get; set; }
    [Index]
    public int Rooms { get; set; }
    [Index]
    public int Restrooms { get; set; }
    [MaxLength(16)]
    public string Age { get; set; }
    public int Garage { get; set; }
    public string Description { get; set; }
    [Index]
    [MaxLength(32)]
    public string AdStatus { get; set; } //Pending/Approved/Denied/OnHold
}

public class PictureAds
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int PictureAdID { get; set; }
    public string url { get; set; }

    // Foreign key
    public int AdsID { get; set; }

    // Navigation properties
    public virtual Ads AdsModels { get; set; }
}

================================================== ===========

これは、Clay Ver Valen の提案を使用して更新されたコードです。

var queryResults = (from d in ApplicationContext.Ads.AsNoTracking()
                                    where (d.Latitude >= latitude - radio)
                                    && (d.Latitude <= latitude + radio)
                                    && (d.Longitude >= longitude - radio)
                                    && (d.Longitude <= longitude + radio)
                                    && (String.IsNullOrEmpty(operationType) || d.OperationType == operationType)
                                    && (!(priceMax > priceMin && (priceMin > 0 || priceMax > 0)) || (d.USDPrice >= priceMin && d.USDPrice <= priceMax))
                                    && (String.IsNullOrEmpty(adType) || d.AdType == adType)
                                    orderby d.USDPrice ascending
                                    select new
                                    {
                                        d.AdsID,
                                        d.Username,
                                        d.administrative_area_level_1,
                                        d.administrative_area_level_2,
                                        d.administrative_area_level_3,
                                        d.neighborhood,
                                        d.Address,
                                        d.PictureUrl,
                                        d.Latitude,
                                        d.Longitude,
                                        d.ExpirationDate,
                                        d.FeaturedAd,
                                        d.PremiumAd,
                                        d.Views,
                                        d.Code,
                                        d.OperationType,
                                        d.Price,
                                        d.USDPrice,
                                        d.PriceCurrency,
                                        d.AdType,
                                        d.Rooms,
                                        d.Restrooms,
                                        d.Description,
                                        d.AdStatus
                                    });

複数列のインデックス:

public class Ads
{
    [Key]
    [Index]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int AdsID { get; set; }
    [Index]
    [MaxLength(128)]
    public string Username { get; set; }
    [Index]
    [MaxLength(64)]
    public string country { get; set; }
    [Index]
    [MaxLength(64)]
    public string administrative_area_level_1 { get; set; }
    [Index]
    [MaxLength(64)]
    public string administrative_area_level_2 { get; set; }
    [MaxLength(64)]
    public string administrative_area_level_3 { get; set; }
    [MaxLength(64)]
    public string neighborhood { get; set; }
    [MaxLength(64)]
    public string Address { get; set; }
    // Navigation property
    public virtual ICollection<PictureAds> Pictures { get; set; }
    [MaxLength(256)]
    public string PictureUrl { get; set; }
    [MaxLength(256)]
    public string UserPicUrl { get; set; }
    [Index("IX_FilterAds", 1)] 
    [DisplayFormat(DataFormatString = "{0:n15}", ApplyFormatInEditMode = true)]
    public decimal Latitude { get; set; }
    [Index("IX_FilterAds", 2)] 
    [DisplayFormat(DataFormatString = "{0:n15}", ApplyFormatInEditMode = true)]
    public decimal Longitude { get; set; }
    [DataType(DataType.Date)]
    public DateTime CreatedDate { get; set; }
    [DataType(DataType.Date)]
    public DateTime ExpirationDate { get; set; }
    public int MonthsAlive { get; set; }
    public decimal PriceSurfaceRatio { get; set; }
    public bool FeaturedAd { get; set; }
    public bool PremiumAd { get; set; }
    public int Views { get; set; }
    [MaxLength(256)]
    public string Code { get; set; }
    [MaxLength(256)]
    public string Title { get; set; }
    [MaxLength(32)]
    public string SunOrientation { get; set; }
    public bool IsFurnished { get; set; }


    [Required]
    [MaxLength(64)]
    public string Name { get; set; }
    [Required]
    [MaxLength(64)]
    public string Email { get; set; }
    [Required]
    [MaxLength(32)]
    public string Phone { get; set; }
    [Required]
    [MaxLength(32)]
    public string UserType { get; set; }
    [Required]
    [Index("IX_FilterAds", 3)] 
    [MaxLength(32)]
    public string OperationType { get; set; }
    [Required]
    public int Price { get; set; }
    [Index]
    [Index("IX_FilterAds", 4)] 
    public int? USDPrice { get; set; }
    [Required]
    [MaxLength(16)]
    public string PriceCurrency { get; set; }
    [Required]
    [Index("IX_FilterAds", 5)] 
    [MaxLength(32)]
    public string AdType { get; set; }
    [Required]
    public int SizeTotal { get; set; }
    public int SizeIndoor { get; set; }
    public int SizeOutdoor { get; set; }
    public int Expenses { get; set; }
    [Index]
    public int Rooms { get; set; }
    [Index]
    public int Restrooms { get; set; }
    [MaxLength(16)]
    public string Age { get; set; }
    public int Garage { get; set; }
    public string Description { get; set; }
    [Index]
    [MaxLength(32)]
    public string AdStatus { get; set; } //Pending/Approved/Denied/OnHold
}
4

1 に答える 1

1

デフォルトのオプションを使用して一連の単一列インデックスを作成しました。必要なのは、クエリに合わせて調整されたカバリング インデックスです。

複数列インデックスの作成の詳細については、MSDN の「複数列インデックス」セクションを確認してください。作成するときは、クエリと一致するように列の順序に特に注意してください。SQL の緯度部分をグループ化することも検討してください。

于 2016-02-10T21:16:15.767 に答える