1

I have two tables. One of those tables has this schema:

CREATE TABLE `object_master_70974_` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `id_object` int(10) unsigned NOT NULL DEFAULT '0',
 `id_master` int(10) unsigned NOT NULL DEFAULT '0',
 `id_slave` int(10) unsigned NOT NULL DEFAULT '0',
 `id_field` bigint(20) unsigned NOT NULL DEFAULT '0',
 `id_slave_field` bigint(20) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `id_object`    (`id_object`,`id_master`,`id_slave`,`id_field`,`id_slave_field`),
 KEY `id_object_2` (`id_object`,`id_master`,`id_field`,`id_slave_field`),
 KEY `id_object_3` (`id_object`,`id_slave`,`id_field`),
 KEY `id_object_4` (`id_object`,`id_slave_field`),
 KEY `id_object_5` (`id_object`,`id_master`,`id_slave`,`id_field`),
 KEY `id_object_6` (`id_object`,`id_master`,`id_slave`,`id_slave_field`),
 KEY `id_master` (`id_master`,`id_slave_field`),
 KEY `id_object_7` (`id_object`,`id_field`)
) ENGINE=InnoDB AUTO_INCREMENT=17827 DEFAULT CHARSET=utf8;

As you can see, there is an overlapping index KEY id_object_5 (id_object,id_master,id_slave,id_field) and there is no index that would cover these three fields: id_object, id_master, id_field. However, when I run these two queries:

SELECT f1.id 
FROM object_70974_ f1  
LEFT JOIN object_master_70974_ mss0 ON mss0.id_object IN (70974,71759)  
AND mss0.id_master = 71100 AND mss0.id_slave = 70912 AND mss0.id_field = f1.id

and

SELECT f1.id 
FROM object_70974_ f1  
LEFT JOIN object_master_70974_ mss0 ON mss0.id_object IN (70974,71759)  
AND mss0.id_master = 71100 AND mss0.id_field = f1.id

they both return the same number of rows (since in fact id_slave field does not really matter) - 3530, however, the first query is slower than the second query by one second - 8 and 7 seconds respectively. So, I guess I have to ask two questions - 1) why does the second query run faster, even though it does not use index and 2) why does the first query run so slowly and why does not it use an index (obviously). In short, what the heck is going on?

EDIT

This is the result of EXPLAIN command (identical for both queries):

"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "Extra"
"1" "SIMPLE"    "f1"    "index" \N  "attr_80420_"   "5" \N  "3340"  "Using index"
"1" "SIMPLE"    "mss0"  "ref"   "id_object,id_object_2,id_object_3,id_object_4,id_object_5,id_object_6,id_master,id_object_7"   "id_master" "4" "const" "3529"  "Using where"

EDIT

It's extremely interesting, because if I DROP id_master index (which for some strange reason is used by both queries), then it starts to use id_object_5 index.

EDIT

And, yes, with id_master index being dropped, both queries start to run super-fast. So, I guess there is some trouble with optimizer.

EDIT

I even have a guess what trouble faces the optimizer - it may be incorrectly treats id_slave_field field name in the key, as if it were two fields instead - id_slave and id_field. In this case it becomes reasonable, why it firstly used this key in both queries.

EDIT

Schema of object_70974_

CREATE TABLE `object_70974_` (
   `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
   `id_inherit` BIGINT(20) NOT NULL DEFAULT '0',
   `id_obj` INT(10) UNSIGNED NOT NULL DEFAULT '0',
   `if_control` TINYINT(1) NOT NULL DEFAULT '0',
   `id_order` BIGINT(20) NOT NULL DEFAULT '0',
   `if_archive` TINYINT(1) NOT NULL DEFAULT '0',
   `id_group` BIGINT(20) NOT NULL DEFAULT '0',
   `if_hist` SMALLINT(6) NOT NULL DEFAULT '0',
   `if_garbage` TINYINT(1) NOT NULL DEFAULT '0',
   `id_color` CHAR(6) DEFAULT NULL,
   `id_text` TINYINT(4) NOT NULL DEFAULT '0',
   `if_default` TINYINT(1) NOT NULL DEFAULT '0',
   `id_parent` BIGINT(20) NOT NULL DEFAULT '0',
   .... a long list of other fields
   PRIMARY KEY (`id`),
   KEY `id_order` (`id_order`)
) ENGINE=INNODB AUTO_INCREMENT=3636 DEFAULT CHARSET=utf8;
17
  • 2
    Don't guess at the data access being used - use built in tools to have the server tell you how it's processing both queries. For mysql, I believe it's called EXPLAIN Jul 31, 2015 at 14:50
  • Explain shows that both queries use an index.
    – Jacobian
    Jul 31, 2015 at 14:52
  • Also, if mysql is like most database systems, it can use any index where you're querying based on the leftmost n columns (n between 1 and number of columns in index) - so I'd expect id_object_2 could be used for your second query Jul 31, 2015 at 14:52
  • What is really strange because there is no overlapping index containing id_object,id_master, and id_field
    – Jacobian
    Jul 31, 2015 at 14:52
  • 1
    8 versus 7 is not much of a difference. It does not know mss0.id_slave = 70912 does not make a difference so it has to check. Why are you optimizing something with no real meat?
    – paparazzo
    Jul 31, 2015 at 15:17

1 Answer 1

1

Why does the SELECT mention f1 at all? It is essentially useless. This would give the same answer, possibly except for some end case:

SELECT  mss0.id_field
    FROM  object_master_70974_ mss0
    WHERE  mss0.id_object IN (70974, 71759)
      AND  mss0.id_master = 71100
      AND  mss0.id_slave = 70912

The optimal index for that is

INDEX(id_master, id_slave, id_object)

where master and slave can be in either order, but id_object is last. Build the 'best' index by starting with any WHERE clause that have = (constant).

Don't use LEFT unless you are want to see NULLs for the 'right' table when there is no match. I think this is part of the problem -- the optimizer was forced to start with f1 when it would be a lot better to start with the other table.

8 vs 7 seconds could be caching.

Note in the EXPLAIN that it needs to hit 3K rows in each table.

8
  • That is totally incorrect. If you look closer, then you will see, that I'm using LEFT JOIN and SELECT from two tables. The result of your query will be completely different from mine.
    – Jacobian
    Aug 1, 2015 at 4:53
  • As for f1, as I said multiple times, this query is a part of a larger and real one, where f1 is absolutely necessary.
    – Jacobian
    Aug 1, 2015 at 4:55
  • Then perhaps the best index would be INDEX(id_master, id_slave, id_field, id_object). id_object last; the rest in any order.
    – Rick James
    Aug 1, 2015 at 5:03
  • And can you, please, explain why the order of fields does not matter. I always though that it really matters. Probably, you have some reference to documentation. I ask this question, because I though that in my query (the first one), it should be like INDEX (id_object, id_master, id_slave, id_field)
    – Jacobian
    Aug 1, 2015 at 5:10
  • I'd better ask, why id_object should not come first in the index.
    – Jacobian
    Aug 1, 2015 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.