0

I have table of the following structure:

create table my_table
(
    id          int auto_increment
        primary key,
    name        varchar(255)                             null,
    vendor_name varchar(250) default ''                  not null,
    constraint id
        unique (id, name, vendor_name),
)
create index name
    on my_table (name);

create index vendor_name
    on my_table (vendor_name);

create index tuple_index
    on my_table (name, vendor_name);


It currently has about 2 000 000 rows.

The select I run is:

SELECT *
FROM my_table
WHERE (name, vendor_name) in ((str1, str2), (str3, str4), ..., (str6000, str6001))

The run is taking about 30 seconds to return about 300 rows (only that many matched and it's fine).

I have an index on id (default one), index on name, index on vendor_name, and a combined index on name,vendor_name.

Is it normal for "selects" on string columns to take such a long time?

I tried to EXPLAIN the query but didn't get much from it:

| id | select_type | table    | type | possible_keys                | key | key_len | ref | rows    | Extra       |
| -- | ----------- | -------- | ---- | ---------------------------- | --- | ------- | --- | ------- | ----------- |
|  1 | SIMPLE      | my_table | ALL  | name,vendor_name,tuple_index |     |         |     | 1947772 | Using where |

4
  • 3
    You'd be better off creating a temp table of pairs and using a join
    – Serg
    Commented Jul 4 at 12:35
  • Strings are computationally more expensive than integers or numeric, especially in your case are two composite of string columns. And that you have 2m rows. Doesn't matter if it only return 300 rows, it'll need to do a full scan anyway. Indexing might help, only way to know for sure is to compare the execution with and without it -- just beware of over-indexing, it could slower down the performance. Commented Jul 4 at 12:36
  • vendor_name varchar(250) default '' not null,? Is an empty string better than null? I'd consider a constraint rejecting empty values.
    – jarlh
    Commented Jul 4 at 13:47
  • @AlfinE.R. The real question is why it isn't using tuple_index. I suspect Serg is correct and joining with a temporary table would work better.
    – Barmar
    Commented Jul 4 at 19:15

0

Browse other questions tagged or ask your own question.