473,809 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql messes up ORDER BY optimisation?!



I have a pretty long query that ends with

ORDER BY R.r_recent_hits DESC LIMIT 0, 1

I also have an index on R.r_recent_hits . I did an explain select and
got this:

ALL - which means (from the manual):

A full table scan will be done for each combination of rows from the
previous tables. This is normally not good if the table is the first
table not marked const, and usually very bad in all other cases.

My question is, why in the world would MySql choose to do this type of
join when there is a perfectly good index there! And I have run analyse
on all of the tables.

It seems like it would be a no-brainer, as soon as the optimizer sees
ORDER BY, just use the given index for that column, that will get you
the highest value easily. Does any one know why it would not
automatically do that? The query essentially does nothing but selects
the highest value. If i suggest what index to use by saying USE INDEX,
it works fine. But, I don't think i should have to.

if you are interested, here is the query:

EXPLAIN SELECT H.accumulated_h its, R.num_messages, R.r_recent_hits ,
T.topic_scope, R.thread_id, U.username, U.user_id, T.title,
UNIX_TIMESTAMP( T.post_time) AS post_time, T.topic_id, T.description FROM
threads as R, topics AS T , users AS U, hit_counter AS H WHERE
T.topic_id=R.to pic_id AND H.object_type=' THREAD' AND
H.object_id=R.t hread_id AND U.user_id=T.use r_id ORDER BY R.r_recent_hits
DESC LIMIT 0, 1

-d
Jul 20 '05 #1
1 1378
Doug wrote:
My question is, why in the world would MySql choose to do this type of
join when there is a perfectly good index there! And I have run analyse
on all of the tables.


According to the MySQL manual, it should be able to use an index for a
DESC sort, as long as you don't also have an ASC sort based on the same
table.

It could be that it had to do a table scan on the table anyway, because
of your other conditions in the WHERE clause or the join conditions.
Once it determines that a full table scan is necessary, it might as well
skip the use of the index, which is now effectively extra work.

Do you have indexes defined on threads.topic_i d and thread.thread_i d?
You might also benefit from indexes on topics.topic_id ,
hit_counter.obj ect_type, hit_counter.obj ect_id, users.user_id, and
topics.user_id.

See also:
http://dev.mysql.com/doc/mysql/en/OR...imization.html

Regards,
Bill K.
Jul 20 '05 #2

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

Similar topics

11
15199
by: Bruce A. Julseth | last post by:
Newbie Question: Is there a way to import MS Access into MySQL? Maybe create a CSV or something. If so, what would be the SQL to do this? Thanks... Bruce
1
2120
by: Boris Kroeplien | last post by:
Hi all. I am administrating an sgi/apache webserver. Whether we will upgrade our website to contain a mysql-database and a php-module depends on the amount of administrative work necessary for maintenance (how many patches need to be applied per month). I searched the web, but I didn't find a list with all the security holes and patches for php and mysql. I search for something like the "SGI wiretap mailing list" or a
1
1647
by: Doug | last post by:
I have a pretty long query that ends with ORDER BY R.r_recent_hits DESC LIMIT 0, 1 I also have an index on R.r_recent_hits. I did an explain select and got this: ALL - which means (from the manual): A full table scan will be done for each combination of rows from the
0
3535
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
2
1955
by: gamalt | last post by:
hi i am trying to follow the steps in installing mysql database. i should to install the , mysql package (binary) under /usr/local/. i tried the commad rpm -i --prefix=/usr/local/ MYSQL-server-4.0.15-i386.rpm MYSQL-client-4 (do not remeber the name). when i execute i get "NOKEY, keyid 5072e1f" which i do not know what it means. and i get an error message too Error "package MYSQL-server is not relectotable".
2
3764
by: gamalt | last post by:
the log of my system says " no mysql pid file found. looked for /var/lib/mysql/localhost.localdomain.pid" how to deal with problem? thanks
0
1485
by: Chris | last post by:
Hi, I am currently designing a simple service orders database. I have played around with MySQL a bit but this is the first time I'm using it in anger, I have a few design queries to make sure I am implementing the database correctly. Currently I have a number of tables, the first being the package table (lists the different service names - such as small, medium etc). Note in the tables I have trimmed a lot of the unnecessary stuff to...
2
1339
by: Jonathan | last post by:
Hi! I am trying to get my C++ connecting and communicating with a mySQL server on my developer PC. I did a next-next-finish installation of mySQL and installed the developer libraries as well. I started programming in MS VC++ and can communicate with my server. However, every time I connect with a user that has no password (blank) I am able to communicate with the server and query some test tables. As soon as I set the password on the...
11
17580
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time to load making any kind linkage with my Access data virtually useless. I have the MySQL driver setup in as a USER DSN. The MySQL data is sitting out on a server and the Access database is running locally. The network connection is very...
2
1969
by: My SQL | last post by:
Hi Is it possible for me to have the daily updation and insert queries performed on the MySQL DB saved? This will help me track when a particular record was modified in the DB. Is there any other way to track as to when the changes were made to the DB? I also want to add who made the changes but this will come at a
0
9601
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
10376
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
10379
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
10115
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
9199
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...
0
6881
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
5550
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...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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

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.