Memory table's primary key must be HASH, why?

1.5k Views Asked by At

I want to use a memory table as a set of queues.

So, there'll be a memory table with int column a and b.
with queries like:
SELECT b FROM table WHERE a=? ORDER BY b DESC LIMIT 1000

I tried this:

create table `test_table` (
    `a` int(11) not null,
    `b` int(11) not null,
    primary key (`a`,`b`) using btree
) engine=memory

but the primary key is still HASH:

show index from `test_table`

Table       Non_unique  Key_name  Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type  Comment
----------  ----------  --------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------  -------
test_table           0  PRIMARY              1  a            (NULL)          (NULL)    (NULL)  (NULL)          HASH
test_table           0  PRIMARY              2  b            (NULL)               0    (NULL)  (NULL)          HASH
  1. Does that means, I need to create another key(a) for such queries?

  2. Why primary key can not be a BTREE index? What's the difference if I change the primary key to just a regular key?

1

There are 1 best solutions below

0
On

You can modify the index type by using alter query.

ALTER TABLE test_table DROP PRIMARY KEY, ADD PRIMARY KEY USING BTREE (a,b);

OR Using below create query,

create table test_table (a int(11) not null,b int(11) not null,primary key USING BTREE(a,b)) engine=memory

For further details about memory engine, please go through below link. http://dev.mysql.com/doc/refman/5.0/en/memory-storage-engine.html