LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-08-2009, 01:00 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
Slow mysql query, indexing doesn't help. Suggestions?


I'm working on a really elaborate osCommerce project. There's one query that's bogging down pretty badly and indexing does not help one bit. I'm wondering if the query is CPU-bound or perhaps I/O bound. It looks like this:

Code:
select count(*) as total
from orders o
left join orders_total ot on (o.orders_id = ot.orders_id), orders_status s
where o.orders_status = s.orders_status_id and ot.class = 'ot_total';
I've tried adding a variety of indexes:
* The orders_id fields are indexed in both tables involved in the left join.
* The orders_status field is indexed in the orders table.
* the orders_status_id field is indexed in orders_total
* the class field is indexed in the orders_total table.

The problem appears to be that indexing the status_id and class fields doesn't help at all. With a maximum of 3-6 distinct values in those fields, indexing provides no performance gain and I end up with a table scan.

Explaining the query yields the following:
Code:
id   select_type   table   type     possible_keys                      key       key_len   ref                       rows     Extra
1    SIMPLE        ot      ref      idx_orders_total_orders_id,class   class     34        const                     359765   Using where
1    SIMPLE        o       eq_ref   PRIMARY,orders_status              PRIMARY   4         newmig2.ot.orders_id      1
1    SIMPLE        s       eq_ref   PRIMARY                            PRIMARY   4         newmig2.o.orders_status   1        Using index
Any thoughts on how to make this faster? I find myself wondering if the query is CPU- or I/O bound. Is there any hope here?

More information: The orders table has about 300,000 records in it. The orders_total table has about 600,000 records in it.

orders.MYD is 74MB
orders.MYI is 8.9MB

orders_total.MYD is 30MB
orders_total.MYI is 15MB
 
Old 10-08-2009, 07:43 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If this is a serious Commerce DB (ie money etc), then you should not be using ISAM. Use a relational engine eg InnoDB so you can use SQL txns ie

START TRANSACTION

SQL STATEMENT
SQL STATEMENT
.
.
COMMIT (OR ROLLBACK ON ERROR)
http://dev.mysql.com/doc/refman/5.0/en/commit.html
 
Old 10-08-2009, 07:49 PM   #3
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Thanks for the tip. That's good to know...but did you mean to post it here? It doesn't really answer the question I posted.
 
Old 10-08-2009, 08:00 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I should have prefixed that as FYI, and prob OT as well


Of course it may also solve the index issue, as it's a different engine.
Incidentally, I read somewhere that although MySQL allows multiple indexes on tables, a given SQL Statement will only use one idx per table during a query, although all indexes are updated as reqd (note the phrase 'possible_keys' in the Explain), unlike eg Oracle or Sybase.

iirc, you can tell MySQL which index to use; have a look at the docs I linked to.

Last edited by chrism01; 10-08-2009 at 08:02 PM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
recoll indexing error while indexing Mail/ [SOLVED] ajnabi Linux - Software 1 09-14-2009 12:44 AM
Indexing on Mysql baddah Programming 1 08-20-2009 01:27 PM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 07:30 AM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration