473,608 Members | 2,443 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 1492

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

Similar topics

6
1472
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
1194
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
1918
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
4020
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
2344
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
2090
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
1363
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
8059
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8000
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
8495
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...
1
8145
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
8330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5475
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
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1328
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.