If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with select distinct

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-18-10, 21:38
kenw232 kenw232 is offline
Registered User
 
Join Date: Jul 2010
Posts: 5
Help with select distinct

Hello, I'm running MySQL 5.1.46 on Linux. It works fine. I have a larger database that has one 700MB table. Its slow because of the way to do my selects. I'm not very good at optimizing things so I don't know how to improve it.

For example when I do:
select distinct channel_num from Track where account_num = '123'

I get this for "show processlist" and of course it takes like 500 seconds:
Copying to tmp table

How can I improve this?

channel_num along with some text fields are pretty much what makes up the table.
Reply With Quote
  #2 (permalink)  
Old 07-18-10, 21:42
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
what indexes are defined on the table?
Reply With Quote
  #3 (permalink)  
Old 07-18-10, 23:00
kenw232 kenw232 is offline
Registered User
 
Join Date: Jul 2010
Posts: 5
Theres a bunch of indexes, but I don't know if they're helping or hurting the problem. For example theres a field account_num, video_num, channel_num and some dates which all have a unique index. Theres 11 indexes on this table. Is that too much?
Reply With Quote
  #4 (permalink)  
Old 07-19-10, 04:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by kenw232 View Post
Is that too much?
for a SELECT, no

please do a SHOW CREATE TABLE for this table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-19-10, 13:45
kenw232 kenw232 is offline
Registered User
 
Join Date: Jul 2010
Posts: 5
The table is called Track. This is it below.

Track | CREATE TABLE `Track` (
`track_num` int(8) NOT NULL AUTO_INCREMENT,
`track_type` varchar(32) DEFAULT NULL,
`account_num` int(8) DEFAULT NULL,
`video_num` int(8) DEFAULT NULL,
`channel_num` int(8) DEFAULT NULL,
`channel_name` varchar(255) DEFAULT NULL,
`channel_name_internal_label` varchar(255) DEFAULT NULL,
`video_title` varchar(254) DEFAULT NULL,
`video_name_internal_label` varchar(255) DEFAULT NULL,
`video_date` date DEFAULT NULL,
`full_datetime` varchar(64) DEFAULT NULL,
`ip` varchar(32) DEFAULT NULL,
`video_filename` varchar(254) DEFAULT NULL,
`referrer` varchar(254) DEFAULT NULL,
`embed_autostart` int(1) DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`track_num`),
KEY `key_account_video_num` (`account_num`,`video_num`),
KEY `key_account_num` (`account_num`),
KEY `key_video_num` (`video_num`),
KEY `key_account_num_channel` (`account_num`,`channel_name`),
KEY `key_account_num_video_date` (`account_num`,`video_date`),
KEY `key_account_num_type` (`account_num`,`track_type`),
KEY `key_channel_num` (`channel_num`)
) ENGINE=MyISAM AUTO_INCREMENT=3947519 DEFAULT CHARSET=latin1
Reply With Quote
  #6 (permalink)  
Old 07-19-10, 13:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
see the index called key_account_num_channel?

that would really work well for this query if it was indeed an index on account_num and channel_num

unfortunately it's on account_num and channel_name

so you will have to add yet another index --

ALTER TABLE
ADD INDEX key_account_num_channel_num ( account_num , channel_num )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-19-10, 13:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
p.s. some of your indexes are redundant, but that only hurts INSERTs, UPDATEs, and DELETEs
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On