0

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..)  

without explicit index mention

With explicit index mention the plan: with explicit index mention

enter image description here

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..

SUVAM ROY
  • 103
  • 1
  • 10
  • An index won't be used if it's not very selective. If the combination of filters end up matching half the table, the query optimizer may decide it's not worth using the index at all. Forcing an index seek may actually slow things down. In the execution plan 98% of time is spent loading rows. How many rows are in the table? 50K rows is no data at all. – Panagiotis Kanavos Jan 18 '23 at 16:43
  • What is the table's schema? `SELECT *` returns all columns. The included columns are of no use if the server has to go to the table to read any missing columns, they only make seeking slower. – Panagiotis Kanavos Jan 18 '23 at 16:45
  • 15 million rows are there. – SUVAM ROY Jan 18 '23 at 16:45
  • 1
    If you want to supply the plans, [Paste the Plan](https://www.brentozar.com/pastetheplan/) is a much better way. *Also, it looks like your ADS is out of date, as query plans aren't in preview any more.* – Thom A Jan 18 '23 at 16:46
  • @Larnu I am getting this error while pasting the plan "The supplied XML did not parse correctly. Are you sure you have a valid query plan XML text?" Can I send you the plan in SO chat please? – SUVAM ROY Jan 18 '23 at 16:52
  • Pop the XML into a [Paste Bin](https://pastebin.com/). – Thom A Jan 18 '23 at 16:52
  • @Larnu Extremely sorry mate...my org blocks Paste Bin perhaps anyother way? – SUVAM ROY Jan 18 '23 at 16:54
  • It seems not all columns are in the nonclustered INCLUDE; since it does key lookup on the PK in the second plan. If you either add them to include or remove them from select, it should work better – siggemannen Jan 20 '23 at 20:46
  • @siggemannen Okay let me check....is there anyother way to improve apart from including all columns ? – SUVAM ROY Jan 22 '23 at 06:17

0 Answers0