472,783 Members | 984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

MySQL uses only part of the key when I add an OR

I'm trying to understand why MySQL isn't using all parts of the primary
key when I add in an OR statement in the query.

I have a people database, and a distances database. The people database
lists a person's name and their location, the distances database
contains the distance between any two points in the country. There are
an roughly 15000^2 entries in the distances table, and currently about
100 entries in the people table. The schemas are as follows:

table distances
(
sourceId smallint unsigned not null,
destinationId smallint unsigned not null,
distance smallint unsigned not null, -- distance in kilometres
primary key(sourceId, destinationId)
) ENGINE = MYISAM ROW_FORMAT = FIXED PACK_KEYS = 1;

table people
(
personId int unsigned not null auto_increment primary key,
personName varchar(50) not null
) ENGINE = MYISAM;

The following queries aim at finding all people located within some
user specified radius centred around some user specified point. The
user has the option of specifying multiple points, eg: find me all
people located within (100 kms of point
A) or (300kms of point B).

In the next query, with the explain output attached, the user has
entered one centre point which he wishes to search around.

explain select * from people,distances as d1 where
people.locationId = d1.sourceId and d1.destinationId = 2 and
d1.distance <= 1000

+----+-------------+-------+--------+---------------+---------+---------+-----------------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref
| rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------------------------+------+-------------+
| 1 | SIMPLE | people | ALL | sourceId | NULL | NULL | NULL | 103 | |
| 1 | SIMPLE | d1 | eq_ref | PRIMARY | PRIMARY | 4 |
db.people.locationId,const | 1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+-----------------------------+------+-------------+

SQL is using both parts of the key, as expected.

However, when the user specifies two centre points he wishes to search
around:

explain select * from people,distances as d1 where
people.locationId = d1.destinationId and d1.sourceId = 2 and
d1.distance <= 1000
or
people.locationId = d1.destinationId and d1.sourceId = 3 and
d1.distance <= 2000

+----+-------------+-------+-------+---------------+----------+---------+-------------------------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref
| rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+-------------------------+-------+-------------+
| 1 | SIMPLE | d1 | range | PRIMARY | PRIMARY | 2 | NULL | 30731 |
Using where |
| 1 | SIMPLE | people | ref | sourceId | sourceId | 2 |
db.d1.destinationId | 11 | Using where |
+----+-------------+-------+-------+---------------+----------+---------+-------------------------+-------+-------------+

The optimiser chooses to join people to d1. It can't use the
people.locationId constraint in scanning the d1 table, so it only uses
the constant constraint. (Side note: why does the 'ref' column read
'NULL' - isn't mysql using the constants
2 & 3 and matching them to the key?)

In an effort to get mysql to reverse the join order (and thus make it
possible to use both parts of the keys), I used straight_join:
explain select straight_join * from people,distances as d1 where
people.locationId = d1.destinationId and d1.sourceId = 2 and
d1.distance <= 1000
or
people.locationId = d1.destinationId and d1.sourceId = 3 and
d1.distance <= 2000
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref
| rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
| 1 | SIMPLE | people | ALL | sourceId | NULL | NULL | NULL | 101 | |
| 1 | SIMPLE | d1 | range | PRIMARY | PRIMARY | 2 | NULL | 30731 |
Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+

Still mysql is only using part of the key (I assume the d1.sourceId
part of the key), where I think it could use
both parts of the key, and reduce the number of rows down to 2, so that
mysql refers to both the constant and the
db.people.locationId constraints, as in the ver first query. I think a
possible problem is that mysql doesn't know
that each OR statement defines exactly one entry in the distance table,
but I'm just guessing.

Any ideas?

Thanks

Taras

Jun 4 '06 #1
0 1305

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aditya Ivaturi | last post by:
We have a CMS which is written is based on php & mysql. Recently we received a request to support multiple languages so that sites in that particular laguage can be created. I did some search on...
20
by: Mr Dygi | last post by:
Hi, PHP 4.3.4 installed manually from package *.zip and Apache 2. I have a problem with this simple code: <?php $link = mysql_connect("127.0.0.1","","") or die("Could not connect: " ....
0
by: Stefan Hinz | last post by:
Degan, jumping in to try and solve some problems that look pretty obvious to me ... > #options for default service (mysqld2) > (mysqld2) It should be , not (mysqld2).
15
by: B. Pigman | last post by:
There have been many questions as to the viability of MySQL's assertion that it can dictate what constitutes a derived work in order to use the GPL against developers who don't wish their software...
8
by: Fred | last post by:
Hello, Our website is currently developed in ASP/Mysql 4. The dedicated servers on which it is currently hosted arrive at saturation. Here is their configuration: - 1 server PIV 2,8Ghz 1GB...
13
by: Jim Hubbard | last post by:
I have a client that desires a new application for use at several stores in different cities. He wants "real time" access to all store data and the ability to continue to run locally even if the...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
1
by: Ted | last post by:
In MS SQL I used the following to create a stored procedure. USE AdventureWorks; GO IF OBJECT_ID ( 'HumanResources.usp_My_Search', 'P' ) IS NOT NULL DROP PROCEDURE HumanResources.usp_My_Search;...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.