Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Sun Oct 19, 2008 2:37 am 
Offline
Senior Member

Joined: Thu Sep 11, 2008 10:49 pm
Posts: 70
ICQ: 4155271
Website: http://mikeage.net
WLM: msn@mikeage.net
Yahoo Messenger: m_i_k_e_miller
AOL: MikeageCM
Location: Israel
I have an SQL query that sometimes (but certainly not always) appears in my slow query log, and I'm not sure how it can be optimized. Can anyone offer some tips?
Code:
mysql> explain select max(timestamp) as timestamp, dcdate, px_feeds.id from px_items, px_feeds where px_items.feed_id = px_feeds.id and in_blogroll = '1' and username = 'XXX' group by px_feeds.id order by timestamp;
+----+-------------+----------+------+----------------+-------------+---------+-------------------------+------+----------------------------------------------+
| id | select_type | table    | type | possible_keys  | key         | key_len | ref                     | rows | Extra                                        |
+----+-------------+----------+------+----------------+-------------+---------+-------------------------+------+----------------------------------------------+
|  1 | SIMPLE      | px_feeds | ALL  | PRIMARY,id_idx | NULL        | NULL    | NULL                    |  260 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | px_items | ref  | feed_id_idx    | feed_id_idx | 4       | mikeage_fof.px_feeds.id |  145 |                                              |
+----+-------------+----------+------+----------------+-------------+---------+-------------------------+------+----------------------------------------------+


My slow query log often says things like:
Code:
# Time: 081019  8:28:41
# User@Host: mikeage_fof[mikeage_fof] @ localhost []
# Query_time: 22  Lock_time: 0  Rows_sent: 71  Rows_examined: 10363
use mikeage_fof;
select max(timestamp) as timestamp, dcdate,
                        px_feeds.id from px_items, px_feeds where
                        px_items.feed_id = px_feeds.id and in_blogroll = '1' and
                        username = 'XXX' group
                        by px_feeds.id order by timestamp;


Top
   
 Post subject:
PostPosted: Sun Oct 19, 2008 4:07 am 
Offline
Junior Member

Joined: Fri Jun 27, 2008 12:24 am
Posts: 31
Adding an index for the username may help but the biggest part of your problem is a lack of RAM available for mysql operations. The query itself should be rather quick if you can allocate enough memory to the tmp_table_size variable in my.cnf and even faster if you can allocate enough to sort_buffer_size.


Top
   
 Post subject:
PostPosted: Sun Oct 19, 2008 4:14 am 
Offline
Senior Member

Joined: Thu Sep 11, 2008 10:49 pm
Posts: 70
ICQ: 4155271
Website: http://mikeage.net
WLM: msn@mikeage.net
Yahoo Messenger: m_i_k_e_miller
AOL: MikeageCM
Location: Israel
I don't think an index on username is all that important, as I only have two usernames :)

Right now, I have sort_buffer set to 64K. Any suggestions how I can find a better value?

I don't have tmp_table_size set explicitely, but tuning-primer.sh reports:
Code:
TEMP TABLES
Current max_heap_table_size = 48 M
Current tmp_table_size = 32 M
Of 13896 temp tables, 20% were created on disk
Created disk tmp tables ratio seems fine


Is there a way I can tell how big tmp_table_size must be to fit this into RAM?

Thanks


Top
   
 Post subject:
PostPosted: Sun Oct 19, 2008 4:47 pm 
Offline
Senior Member

Joined: Thu Sep 11, 2008 10:49 pm
Posts: 70
ICQ: 4155271
Website: http://mikeage.net
WLM: msn@mikeage.net
Yahoo Messenger: m_i_k_e_miller
AOL: MikeageCM
Location: Israel
Will optimizing the query this way help?

Code:
select max(timestamp)as timestamp ,feed_id as id from px_items where feed_id in (select id from px_feeds where in_blogroll = '1' and username = 'XXX') group  by feed_id order by timestamp;


Running from the command line seems to suggest that this is faster, but I don't have the theoretical background to confirm it.


Top
   
 Post subject:
PostPosted: Mon Oct 20, 2008 7:25 pm 
Offline
Senior Newbie

Joined: Sun May 18, 2008 8:52 am
Posts: 17
Website: http://www.arie-online.net
Yahoo Messenger: arie_wijayanto
Location: Indonesia - Bogor
try to use this script to optimize your mysql conf

http://www.day32.com/MySQL/tuning-primer.sh


Top
   
 Post subject:
PostPosted: Tue Oct 21, 2008 11:48 am 
Offline
Senior Member

Joined: Thu Sep 11, 2008 10:49 pm
Posts: 70
ICQ: 4155271
Website: http://mikeage.net
WLM: msn@mikeage.net
Yahoo Messenger: m_i_k_e_miller
AOL: MikeageCM
Location: Israel
Thanks, except that (a) I'm already using tuning-primer.sh to adjust my my.cnf (as mentioned above) and (b) the question was about a query, not about the server

Thanks anyway :)


Top
   
 Post subject:
PostPosted: Tue Oct 21, 2008 11:40 pm 
Offline
Senior Member

Joined: Fri Sep 12, 2008 3:17 am
Posts: 166
Website: http://independentchaos.com
disregard this post.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group