Details
Description
The queries were provided by Stephane Varoqui here: http://pastebin.com/1pqidvq6
The dataset is lots.tgz, uploaded to ftp.askmonty.org/private/
EXPLAIN outputs and query time:
MariaDB [lots]> explain SELECT count(*) AS amount FROM lots WHERE contractNumber='1478876' AND lots.tsClosed IS NULL; +----+-------------+-------+-------------+-------------------------+-------------------------+---------+------+------+--------------------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------------+-------------------------+-------------------------+---------+------+------+--------------------------------------------------------------------+ | 1 | SIMPLE | lots | index_merge | tsClosed,contractNumber | contractNumber,tsClosed | 5,5 | NULL | 3257 | Using intersect(contractNumber,tsClosed); Using where; Using index | +----+-------------+-------+-------------+-------------------------+-------------------------+---------+------+------+--------------------------------------------------------------------+ 1.85 sec.
MariaDB [lots]> explain SELECT count(*) AS amount FROM lots ignore index(tsClosed) WHERE contractNumber='1478876' AND lots.tsClosed IS NULL; +----+-------------+-------+------+----------------+----------------+---------+-------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+----------------+----------------+---------+-------+-------+-------------+ | 1 | SIMPLE | lots | ref | contractNumber | contractNumber | 5 | const | 28600 | Using where | +----+-------------+-------+------+----------------+----------------+---------+-------+-------+-------------+ 0.30 sec
MariaDB [lots]> explain SELECT count(*) AS amount FROM lots ignore index(contractNumber) WHERE contractNumber='1478876' AND lots.tsClosed IS NULL; +----+-------------+-------+------+---------------+----------+---------+-------+--------+------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+----------+---------+-------+--------+------------------------------------+ | 1 | SIMPLE | lots | ref | tsClosed | tsClosed | 5 | const | 243422 | Using index condition; Using where | +----+-------------+-------+------+---------------+----------+---------+-------+--------+------------------------------------+ 4.50 sec
As one can see, index_merge/intersect is about 1.85/0.30=6 times slower than ref access on contractNumber.
Gliffy Diagrams
Attachments
Activity
- All
- Comments
- Work Log
- History
- Activity
- Transitions
Re: index_merge/intersection is used when ref(const) is faster
Let's explore the dataset: here's numbers of matching records for both parts of the WHERE:
Total rows: 2 137 152
lots.tsClosed IS NULL - 544 288 (estimate: 243 422)
contractNumber='1478876' - 30 000 (estimate: 28 600)
contractNumber='1478876' AND lots.tsClosed IS NULL - 10 000 (index_merge's estimate: 3257)