tags:

views:

41

answers:

2

Hi there,

When i execute this query it seems to cause the mysqld.exe process to use 50-100% of my cpu.

Any ideas why.

SELECT m.draw_id , m.mobile, m.fly_buys, m.signup_date,
  IF(m.club = 1, 'Yes', 'No') AS club,
  IF(w.code != '', w.code, 
     IF(f.code != '', f.code, 
        IF(t.code != '', t.code, 'Unknown') ) ) AS code,
  IF(w.code != '', 'General', 
     IF(f.code != '', '5 pt', 
        IF(t.code != '', '10 pt', 'Unknown') ) ) AS code_type
  FROM action_6_members m
  LEFT JOIN action_6_5pts f ON f.member_id = m.id
  LEFT JOIN action_6_10pts t ON t.member_id = m.id
  LEFT JOIN action_6_weekly w ON w.member_id = m.id
  WHERE `draw_id` = '3' ORDER BY m.id DESC limit 0, 20;

Note: this does not return a massive amount of data (10 rows).
Note: although there are only 10 rows returned
action_6_5pts contains 40k rows
action_6_10_pts contains 25k rows
action_6_weekly contains 3.5million rows.


Update
So i have added an index and it is now working better.

Here is the basic table schematics.

action_6_5pts/10pts/weekly are all

code | member_id

CREATE TABLE `action_6_5pts` (
  `code` varchar(255) NOT NULL,
  `member_id` int(11) NOT NULL,
  PRIMARY KEY (`code`),
  KEY `member_id` (`member_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

action_6_members is id | flybuys | bla | bla | bla

CREATE TABLE `action_6_members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `draw_id` int(11) NOT NULL,
  `mobile` varchar(255) NOT NULL,
  `fly_buys` varchar(255) NOT NULL,
  `signup_date` datetime NOT NULL,
  `club` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1

so,

i need to return the code type for the member, and the actual code.

This information is not stored anywhere and the only way i can see to get it is to do a join on the tables based on the member_id of each user.

Is there a faster/better way of doing this without altering the table structure?

+2  A: 

because your query is quite beefy

40k x 25k x 3.5m

joined rows need to be considered. Have you indexed on all the columns you are using in your join?

http://www.wolframalpha.com/input/?i=40000+x+25000+x+3500000

thats a lot of data.

hvgotcodes
Just added an index on all 4 tables and it has made it work awesomely now.Can you think of any way to rewrite this query?
Hailwood
nothing comes to mind, but indexing is key. I have had queries go from tens of minutes to seconds...make sure you index the columns on which you are joining...
hvgotcodes
thanx for the check. I hit the reputation cap for the first time with something like 23 seconds to spare...that is unbelievable.
hvgotcodes
A: 

Your question is self-answered. Joins on such big tables are heavy-loaders.

Eton B.