In-memory table range scan/ seek

sakuraime 2,326 Reputation points
2020-10-23T04:51:44.04+00:00

Suppose I have a table
CREATE TABLE dbo.ShoppingCart (
ShoppingCartId INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED,
UserId INT NOT NULL INDEX ix_UserId NONCLUSTERED
HASH WITH (BUCKET_COUNT=1000000),
mytext varchar(100)
) WITH (MEMORY_OPTIMIZED=ON)
GO

then I do a insert to this table ( around 300K records) . and then I do a query
select * from dbo.ShoppingCart where ShoppingCartId < 100

It give me a table scan inside the execution plan .
34456-tablescan.jpg

why ? Inmemory table can have range seek ?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,694 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. tibor_karaszi@hotmail.com 4,311 Reputation points
    2020-10-23T06:32:48.367+00:00

    SQL Server can definitely do a range scan on a "regular index" (BW-Tree) for a in-mem table. We have to have a full repro, or at the very least the plan at pastetheplan.com in order to answer why it doesn't do that in your case. All we have is an image of the execution plan operators. Likely it has to do with calculated selectivity.

    0 comments No comments

  2. m 4,271 Reputation points
    2020-10-23T06:46:35.533+00:00

    Hi @sakuraime ,

    ...It give me a table scan inside the execution plan.why ?

    It maybe caused by index of ShoppingCartId is not created successfully, or be deleted by mistake.

    1.What version do you use? please use code as next to have a check :

    select @@version   
    

    2.If ShoppingCartId is nolustered index, it will be use index seek. Please check wether you delete the index, if there is no index on the column, it will use table scan.

    Same code on myside:
    If select by ShoppingCartId, index seek;
    If select by mytext , table scan

    use test1021  
    
    --only create one memory-optimized filegroup per database  
    ALTER DATABASE test1021 ADD FILEGROUP imoltp_mod CONTAINS MEMORY_OPTIMIZED_DATA    
    
    --add one or more containers to the MEMORY_OPTIMIZED_DATA filegroup  
    ALTER DATABASE test1021 ADD FILE (name='test1021_imoltp_mod1', filename='C:\data\test1021') TO FILEGROUP imoltp_mod    
    
    
    CREATE TABLE dbo.ShoppingCart (  
    ShoppingCartId INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED,  
    UserId INT NOT NULL INDEX ix_UserId NONCLUSTERED  
    HASH WITH (BUCKET_COUNT=1000000),  
    mytext varchar(100)  
    ) WITH (MEMORY_OPTIMIZED=ON)  
    GO  
    
    
    DECLARE @num int  
    SET @num = 1  
    WHILE @num <= 3000000  
    BEGIN  
    INSERT INTO dbo.ShoppingCart (UserId,mytext) VALUES(@num,'A' + CONVERT(varchar(10), @num))  
    SET @num = @num + 1  
    END  
    

    34486-20201023indexseek.jpg

    Inmemory table can have range seek ?

    Yes. If seek means scan. Memory-optimized nonclustered indexes support range scans and ordered scans.

    BR,
    Mia


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  3. m 4,271 Reputation points
    2020-10-24T01:22:22.377+00:00

    Hi @sakuraime ,

    Is the reply helpful?

    BR,
    Mia


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.