473,769 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do these two queries exhibit such different behavior?

Consider the output of these two explain statements:

mysql> explain select count(*)
from cities,addresse s,venues,events
where cities.latitude <= 30.2741903768
and cities.latitude >= 30.2596976232
and cities.longitud e <= -97.734387326
and cities.longitud e >= -97.751168674
and events.venue = venues.id
and venues.address = addresses.id
and addresses.city = cities.id
and events.submit_d ate >= '1976-12-03 18:33:27'
and events.active = 1
order by events.start,ev ents.end limit 1000;

+-----------+-------+-------------------------------+-----------+---------+-----------------+------+---------------------------------------------+
| table | type | possible_keys | key |
key_len | ref | rows | Extra
|
+-----------+-------+-------------------------------+-----------+---------+-----------------+------+---------------------------------------------+
| cities | range | id,latlong,lati tude,longitude | longitude |
4 | NULL | 2 | where used; Using temporary; Using
filesort |
| addresses | ref | id,city | city |
4 | cities.id | 16 |
|
| venues | ref | id,address | address |
4 | addresses.id | 20 |
|
| events | ref | submit_date,ven ue,active,sva | venue |
5 | venues.id,const | 2 | where used
|
+-----------+-------+-------------------------------+-----------+---------+-----------------+------+---------------------------------------------+
4 rows in set (0.00 sec)

mysql> explain select count(*)
from cities,addresse s,venues,events
where cities.latitude <= 42.6937078406
and cities.latitude >= 41.9690701594
and cities.longitud e <= -82.5528748628
and cities.longitud e >= -83.5387911372
and events.venue = venues.id
and venues.address = addresses.id
and addresses.city = cities.id
and events.submit_d ate >= '1976-12-03 18:36:37'
and events.active = 1
order by events.start,ev ents.end limit 1000;

+-----------+--------+-------------------------------+--------+---------+----------------+-------+----------------------------+
| table | type | possible_keys | key |
key_len | ref | rows | Extra |
+-----------+--------+-------------------------------+--------+---------+----------------+-------+----------------------------+
| events | ref | submit_date,ven ue,active,sva | active |
1 | const | 16135 | where used; Using filesort |
| venues | eq_ref | id,address | id |
4 | events.venue | 1 | |
| addresses | eq_ref | id,city | id |
4 | venues.address | 1 | |
| cities | eq_ref | id,latlong,lati tude,longitude | id |
4 | addresses.city | 1 | where used |
+-----------+--------+-------------------------------+--------+---------+----------------+-------+----------------------------+
4 rows in set (0.00 sec)

Note that the only differences between them are the specific values
compared to the latitude, longitude and submit_date columns. Why then
does the first query perform so few comparisons and the second perform
so many? I've tried the same queries several times over the past
couple days. The server has been restarted, the MYI files have been
myisamchk'd. The tables have been flushed. (Is there a way to tell
the server to rebuild its indexes?) To show that explain isn't just
screwed up, the first query takes less that a tenth of a second on my
system while the second takes nearly six seconds. Both return a count
of slightly more than 100.

Here are the table definitions for the events and cities tables:

CREATE TABLE events (
id int(10) unsigned NOT NULL auto_increment,
start date NOT NULL default '0000-00-00',
end date NOT NULL default '0000-00-00',
time varchar(20) NOT NULL default '',
info varchar(255) NOT NULL default '',
event varchar(128) NOT NULL default '',
venue int(10) unsigned NOT NULL default '0',
active tinyint(3) unsigned NOT NULL default '1',
submitter int(10) unsigned NOT NULL default '0',
submit_date date NOT NULL default '0000-00-00',
update_date date NOT NULL default '0000-00-00',
price varchar(32) NOT NULL default '',
UNIQUE KEY id (id),
KEY submit_date (submit_date),
KEY update_date (update_date),
KEY venue (venue,active),
KEY event (event,active),
KEY date (start,active,e nd),
KEY active (active),
KEY sva (submit_date,ve nue,active)
) TYPE=MyISAM;

CREATE TABLE cities (
id int(10) unsigned NOT NULL auto_increment,
city varchar(128) NOT NULL default '',
state char(2) NOT NULL default '',
country varchar(128) NOT NULL default '',
latitude float NOT NULL default '-255',
longitude float NOT NULL default '-255',
UNIQUE KEY id (id),
KEY city (city),
KEY state (state),
KEY country (country),
KEY latlong (latitude,longi tude),
KEY latitude (latitude),
KEY longitude (longitude)
) TYPE=MyISAM;

All other involved tables just chain between the events and cities
tables. All tables are TYPE=MyISAM.

Based on some inputs I received to a similar post last November, I've
tried the following suggestions:

* reorganized the lat/long comparisons to compare both latitudes
then both longitudes

* recast the lat/long comparisons to use the BETWEEN function
instead of comparison operators

* added separate latitude and longitude indexes to the cities
table instead of just the composite latlong index.

At one point I thought that STRAIGHT_JOIN was going to be my
salvation. Alas, while it seems to solve the problem some of the
time, it also appears to just move the problem around (other similar
queries perform badly at times, and for no apparent reason).
Accordingly, I've dumped STRAIGHT_JOIN and just let the MySQL query
optimizer do its thing without any help from me.

My server is 3.23.41-log as reported by mysqladmin, protocol version
10. Connections are to localhost via a unix domain socket.

I can pretty easily upgrade to the latest micro release (.58). Is
there anything to suggest that the query optimizer is somehow better
in 4.0.18?

Thanks,

Skip Montanaro
Got gigs? http://www.musi-cal.com/submit.html
Got spam? http://www.spambayes.org/
sk**@pobox.com
Jul 20 '05 #1
0 1513

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

Similar topics

6
1488
by: r.e.s. | last post by:
Should one expect the following execution times to be so different? ... Module_A takes about 4 seconds to run: x = (stuff_1) (stuff_2) Module_B takes about 80 seconds to run: x = (stuff_1) def f(y):
0
352
by: Skip Montanaro | last post by:
Consider the output of these two explain statements: mysql> explain select count(*) from cities,addresses,venues,events where cities.latitude <= 30.2741903768 and cities.latitude >= 30.2596976232 and cities.longitude <= -97.734387326 and cities.longitude >= -97.751168674 and events.venue = venues.id and venues.address = addresses.id
1
1201
by: nicolas_riesch | last post by:
I am writing asp pages in python, running on IIS 5 on Windows. I notice that variables outside functions keep their value across queries. I don't know if it is normal. Here is a little script that demonstrates it: ------------------ problem.asp ---------------------
5
1924
by: My SQL | last post by:
Hi, I started the general query log with mysqld --log On my machine this command works fine and every select, update and all statements are logged. I tried the same command on another machine and many many queries are missing, I am running apache, PHP on that machine and all the queries come through the web on localhost.
5
4042
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I think - it might have been XP) to 2003. The database is impressive, both in what it does and the obtuse and inconsistent ways it works. There are several hundred queries, for example, with no indication of where they are used or if they are in fact...
1
2348
by: Ersin Gençtürk | last post by:
We couldn't find why these errors happen.They doesn't appear everytime.And they appear different pages at different times.Is there somebody know why these happenes ? First user gets this error : Server : WEB System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an
1
2099
by: loosecannon_1 | last post by:
Hello everyone, I am hoping someone can help me with this problem. I will say up front that I am not a SQL Server DBA, I am a developer. I have an application that sends about 25 simultaneous queries to a SQL Server 2000 Standard Edition SP4 running on Windows 2000 Server with 2.5 GB of memory. About 11 of these queries are over views (all over the same table) and these queries are all done from JDBC but I am not sure that matters. ...
20
1389
by: Tommy Vercetti | last post by:
Hi - Great group! I have 2 queries about undefined behavior: 1) Is the following code undefined? float myfunction(float f) {
9
377
by: Bill E. | last post by:
I'm creating an MS Access application that connects to a SQL Server 2005 database using pass-through queries and ADO to call stored procedures. I ran a SQL trace and started clicking through various forms to look at how things were being handled on the database. I noticed an alarming number of connections were opened as if there was absolutely no connection pooling. After several clicks I had 20 new connections. It appeared that a new...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.