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 |
vendor_name varchar(250) default '' not null,
? Is an empty string better than null? I'd consider a constraint rejecting empty values.tuple_index
. I suspect Serg is correct and joining with a temporary table would work better.