Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2013, 03:24 PM   PM User | #1
Eben
New Coder

 
Join Date: May 2006
Posts: 48
Thanks: 1
Thanked 0 Times in 0 Posts
Eben is an unknown quantity at this point
Optimizing MySQL query

Hi
today i received email from my hosting account saying that i need to tweak my query

Quote:
Problematic query:
SELECT `id`,`nick`,`msg`,`uid`,`show_pic`,`time`,`ip`,`time_updated`, (SELECT COUNT(c.msg_id) FROM `the_ans` c where c.msg_id = d.id) AS counter, (SELECT c.msg FROM `the_ans` c WHERE c.msg_id =d.id ORDER BY `time` DESC LIMIT 1) as lastmsg FROM `the_data` d ORDER BY `time_updated` DESC LIMIT 26340 ,15

EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY d ALL 34309 Using filesort
3 DEPENDENT SUBQUERY c ALL 43659 Using where; Using filesort
2 DEPENDENT SUBQUERY c ALL 43659 Using where

This query examines 65,396,669,012,829 rows, which is unacceptable in shared hosting
tbh, i don't understand their explanation..
what the query actually does is to get 15 posts order by time updated,
for each post i grab the latest comment,
count all commnts for each post.

posts table - 'the_data'
commnts table = 'the_ans'

i'm not a mysql guru and i don't know how to improve this query
any help will be appreciated
thx

the query

PHP Code:
SELECT `id` , `nick` , `msg` , `uid` , `show_pic` , `time` , `ip` , `time_updated` , (
 
SELECT COUNTc.msg_id )
FROM `the_ansc
WHERE c
.msg_id d.id
) AS counter, (
 
SELECT c.msg
FROM 
`the_ansc
WHERE c
.msg_id d.id
ORDER BY 
`timeDESC
LIMIT 1
) AS lastmsg
FROM 
`the_datad
ORDER BY 
`time_updatedDESC
LIMIT 26340 
15 
__________________
Firebug for Internet Explorer
Eben is offline   Reply With Quote
Old 01-21-2013, 08:09 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What indexes do you have on the two tables?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
count, mysql, subselect

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:22 PM.


Advertisement
Log in to turn off these ads.