So we have 15million data is this table :allocation_plan_detail. I have put all necessary column in the non-clustered index and rest all other inside Include statement is while querying we are selecting *.
Here is the Index defination :
CREATE NONCLUSTERED INDEX [newNonClusteredIndex] ON [Allocation].[allocation_plan_detail]
(
[tenant_id] ASC,
[item_type] ASC,
[allocation_plan_status] ASC,
[allocation_plan_type] ASC,
[item_nbr] ASC,
[club_nbr] ASC
)
INCLUDE([item_config_id],[club_name],[dept_nbr],[subcat_nbr],[dc_nbr],[total_dc_on_hand_qty],[vc_nbr],[city_addr],[state_addr],[item_status_code],[base_unit_rtl_amt],[item_on_shelf_date],[item_off_shelf_date],[on_hand_qty],[on_order_qty],[total_inventory],[wkly_sales],[total_sales],[days_with_inventory],[avg_wos_planner],[forecast_level],[club_decile_nbr],[decile_rank],[current_wos],[projected_wos],[wos_target],[min_pres_qty],[max_inv_level],[oos_threshold],[moq],[lead_time],[mabd],[unconstrained_need_qty],[true_need_qty],[adjusted_planner_qty],[manual_override_planner_qty],[allocation_seq_nbr],[created_by],[created_on],[last_updated_by],[last_updated_on])
Now I am querying this :
select * from Allocation.allocation_plan_detail --with(index(newNonClusteredIndex))
where
allocation_plan_status='draft' and
item_type ='In season' and tenant_id='sams_us' and
allocation_plan_type='continuous' and
item_nbr in (980109489,326156,678518,980098926,980143632,299324,916205,207317,338064,433702,980427979,....upto 200 elements..)
With explicit index mention the plan:
Now even though I properly mentioned indexes why it scanning table? Now by some research in SO I am assuming its being done because IN clause has too many elements. But even with Index Seeking my query is taking on average to give output of 50k data for 5-6min avg!! Now I am completely clueless what exactly to do optimize the query ? I have tried with storing the IN elements into temporary table but yet I dont see any improvement. Any suggestions ?
Here is the table defination (Model)
public class AllocationPlanDetail extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull(groups = Existing.class)
@Null(groups = New.class)
private Long allocationPlanDetailId;
@NotNull
private Long itemConfigId;
@NotNull
private Long itemNbr;
@NotNull
private Integer clubNbr;
private String clubName;
private Integer deptNbr;
private Integer subcatNbr;
private Integer dcNbr;
private Integer totalDcOnHandQty;
private Integer vcNbr;
private String cityAddr;
private String stateAddr;
private String itemStatusCode;
private Float baseUnitRtlAmt;
private Date itemOnShelfDate;
private Date itemOffShelfDate;
private Integer onHandQty;
private Integer onOrderQty;
private Integer totalInventory;
@Column(name="wkly_sales",columnDefinition="nvarchar")
@Convert(converter = SalesConverterJson.class)
private Map<String,Float> wklySales;
private Float totalSales;
private Integer daysWithInventory;
@Column(name="avg_wos_planner")
private Float avgWOSPlanner;
private String forecastLevel;
private Integer clubDecileNbr;
private Integer decileRank;
@Column(name="current_wos")
private Float currentWOS;
@Column(name="projected_wos")
private Float projectedWOS;
private Integer wosTarget;
@Column(name="min_pres_qty")
private Integer minPres;
@Column(name="max_inv_level")
private Integer maxClubQty;
private Integer oosThreshold;
private Integer moq;
private Integer leadTime;
private Date mabd;
private Integer unconstrainedNeedQty;
private Integer trueNeedQty;
private Integer adjustedPlannerQty;
private Integer manualOverridePlannerQty;
private Integer allocationSeqNbr;
private String allocationPlanType;
private String allocationPlanStatus;
private String itemType;
@NotNull
private String tenantId;
I have asked similar quetion here : Getting Index Scan instead Index Seeking if IN clause larger in Azure Sql
But that time I havent included the INCLUDE clause in my index FYI. And second image got duplicated apology for that..