Garayed.com  

Go Back   Garayed.com > mySQL
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-13-2007, 07:29 PM
hank_rb
 
Posts: n/a
Default efficient join based on non-constant range condition?

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 07:12 AM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.