Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3227188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:32:43+00:00 2026-05-17T16:32:43+00:00

I have temporary memory tables A and B. each contain 1 column of integer

  • 0

I have temporary memory tables A and B. each contain 1 column of integer values
I need to find all values which are in A but not in B.

The problem is that its very slow due to the fact (I think) that memory tables use hash and not ordered keys.

How can I perform it efficiently? Currently I’m using
SELECT val FROM tableA WHERE val NOT IN (SELECT val FROM tableB)

The definition of each table :
CREATE TABLE tableA (val INT, PRIMARY KEY USING HASH (val)) ENGINE = MEMORY;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:32:43+00:00Added an answer on May 17, 2026 at 4:32 pm
    select
     a.val
    from
     tableA a
    left outer join tableB b on a.val = b.val
    where
     b.val is null;
    

    some additional testing on 250K rows reveals that there’s not much between them:

    call load_test_data();
    
    call test_memory_tables_hash(); -- 0:00:00.597
    call test_memory_tables_hash(); -- 0:00:00.362
    
    
    call load_test_data();
    
    call test_memory_tables_btree(); -- 0:00:00.460
    call test_memory_tables_btree(); -- 0:00:00.429
    

    full testing script:

    drop table if exists tableA;
    create table tableA
    (
    val int unsigned not null primary key
    )
    engine=innodb;
    
    drop table if exists tableB;
    create table tableB
    (
    val int unsigned not null primary key
    )
    engine=innodb;
    
    
    drop procedure if exists load_test_data;
    
    delimiter #
    
    create procedure load_test_data()
    proc_main:begin
    
    declare i int unsigned default 0;
    declare rnd int unsigned default 0;
    declare max int unsigned default 250000;
    
      truncate table tableA;
      truncate table tableB;
    
      set autocommit = 0;
    
      while i < max do
        if i % 2 = 0 then insert into tableA values (i); end if;
        if i % 3 = 0 then insert into tableB values (i); end if;
        set i = i+1;
      end while;
    
      commit;
    
    end proc_main #
    
    delimiter ;
    
    drop procedure if exists test_memory_tables_hash;
    
    delimiter #
    
    create procedure test_memory_tables_hash()
    proc_main:begin
    
    create temporary table mem_tableA 
    (
      val int unsigned not null, index using hash(val)
    ) 
    engine=memory select val from tableA;
    
    create temporary table mem_tableB
    (
      val int unsigned not null, index using hash(val)
    ) 
    engine=memory;
    
    insert into mem_tableA select val from tableA;
    insert into mem_tableB select val from tableB;
    
    select
     a.val
    from
     mem_tableA a
    left outer join mem_tableB b on a.val = b.val
    where
     b.val is null
    order by
     a.val desc
    limit 64;
    
    drop temporary table if exists mem_tableA;
    drop temporary table if exists mem_tableB;
    
    end proc_main #
    
    delimiter ;
    
    delimiter ;
    
    drop procedure if exists test_memory_tables_btree;
    
    delimiter #
    
    create procedure test_memory_tables_btree()
    proc_main:begin
    
    create temporary table mem_tableA 
    (
      val int unsigned not null, index using btree(val)
    ) 
    engine=memory select val from tableA;
    
    create temporary table mem_tableB
    (
      val int unsigned not null, index using btree(val)
    ) 
    engine=memory;
    
    insert into mem_tableA select val from tableA;
    insert into mem_tableB select val from tableB;
    
    select
     a.val
    from
     mem_tableA a
    left outer join mem_tableB b on a.val = b.val
    where
     b.val is null
    order by
     a.val desc
    limit 64;
    
    drop temporary table if exists mem_tableA;
    drop temporary table if exists mem_tableB;
    
    end proc_main #
    
    delimiter ;
    
    
    call load_test_data();
    
    call test_memory_tables_hash();
    -- 0:00:00.597
    call test_memory_tables_hash();
    -- 0:00:00.362
    
    
    call load_test_data();
    
    call test_memory_tables_btree();
    -- 0:00:00.460
    call test_memory_tables_btree();
    -- 0:00:00.429
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've never used the Global Temporary Tables however I have some questions how they
I use temporary tables in an SQLite based iPhone application. Performance was not as
I have a stored procedure inside which I create a temporary table that typically
Using the .net framework you have the option to create temporary files with Path.GetTempFileName();
I've discovered this folder in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files and have a few questions. What
Have just started using Visual Studio Professional's built-in unit testing features, which as I
So we have a piece of software which has a poorly written SQL statement
I have the following code but it keeps presenting errors. The first part of
I have managed to get myself confused about some elements of memory management. I
I have a simple app (this is my first one) which loads an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.