![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Search this Thread | Display Modes |
| |||
| Hi, I have a query that is extremely inefficient involving a join based on a range condition, as follows: mysql> select * from sorted; +----+----------+ | id | position | +----+----------+ | 0 | 2426 | | 1 | 2782 | | 2 | 3095 | | 3 | 3522 | | 4 | 5363 | | 5 | 6166 | | 6 | 7834 | | 7 | 8304 | | 8 | 8732 | | 9 | 9131 | | 10 | 10711 | | 11 | 11413 | | 12 | 12275 | | 13 | 12474 | | 14 | 13420 | | 15 | 15360 | | 16 | 15925 | | 17 | 16405 | | 18 | 16701 | | 19 | 16962 | +----+----------+ 20 rows in set (0.00 sec) mysql> show index from sorted; +--------+------------+----------+--------------+------------- +-----------+-------------+----------+--------+------+------------ +---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +--------+------------+----------+--------------+------------- +-----------+-------------+----------+--------+------+------------ +---------+ | sorted | 1 | pos | 1 | position | A | NULL | NULL | NULL | | BTREE | | +--------+------------+----------+--------------+------------- +-----------+-------------+----------+--------+------+------------ +---------+ 1 row in set (0.00 sec) mysql> explain select s1.id as id1, s2.id as id2 from sorted s1, sorted s2 where (s2.position between s1.position + 10 and s1.position + 1000); +----+-------------+-------+------+---------------+------+--------- +------+------+------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+--------- +------+------+------------------------------------------------+ | 1 | SIMPLE | s1 | ALL | NULL | NULL | NULL | NULL | 20 | | | 1 | SIMPLE | s2 | ALL | pos | NULL | NULL | NULL | 20 | Range checked for each record (index map: 0x1) | +----+-------------+-------+------+---------------+------+--------- +------+------+------------------------------------------------+ 2 rows in set (0.00 sec) it seems like mysql should know to only scan a small subset of rows of s2 for each row of s1. There is however no discussion in the documentation on using non-constant range constraints. Has anyone else encountered this and found a solution, such that the 'explain' command would show 20 rows scanned for s1 and about 2 for s2? Thanks in advance! Henry |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |